Class: Funkr::ADT

Inherits:
Object show all
Defined in:
lib/funkr/adt/adt.rb

Overview

Very rought Algebraic Data Types. A class inheriting from ADT can declare constructors with #adt

Direct Known Subclasses

Types::Failable, Types::Maybe

Constant Summary collapse

MATCHER =
Funkr::Matchers::SafeMatcher

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(const, *data) ⇒ ADT

Returns a new instance of ADT.



11
12
13
# File 'lib/funkr/adt/adt.rb', line 11

def initialize(const, *data)
  @const, @data = const, data
end

Class Method Details

.adt(*constructs) ⇒ Object

Declare ADT constructors, for example :

class Maybe < ADT;  adt :just, :nothing; end


17
18
19
20
# File 'lib/funkr/adt/adt.rb', line 17

def self.adt(*constructs)
  build_adt(constructs)
  build_matcher(constructs)
end

.matcherObject



22
# File 'lib/funkr/adt/adt.rb', line 22

def self.matcher; @matcher; end

Instance Method Details

#match(&block) ⇒ Object

Match your ADT against its constructors, for example :

a = Maybe.just("hello")
a.match do |on|
  on.just{|x| puts x}
  on.nothing{ }
end


30
31
32
# File 'lib/funkr/adt/adt.rb', line 30

def match(&block)
  self.class.matcher.match_with(normal_form, &block)
end

#to_sObject



37
38
39
40
41
42
# File 'lib/funkr/adt/adt.rb', line 37

def to_s
  format("{%s%s%s}",
         @const,
         @data.any? ? " : " : "",
         @data.map(&:inspect).join(", ") )
end

#unsafe_constObject



34
# File 'lib/funkr/adt/adt.rb', line 34

def unsafe_const; @const; end

#unsafe_dataObject



35
# File 'lib/funkr/adt/adt.rb', line 35

def unsafe_data; @data; end