Class: Funkr::ADT
Overview
Very rought Algebraic Data Types. A class inheriting from ADT can declare constructors with #adt
Direct Known Subclasses
Constant Summary collapse
- MATCHER =
Funkr::Matchers::SafeMatcher
Class Method Summary collapse
-
.adt(*constructs) ⇒ Object
Declare ADT constructors, for example : class Maybe < ADT; adt :just, :nothing; end.
- .matcher ⇒ Object
Instance Method Summary collapse
-
#initialize(const, *data) ⇒ ADT
constructor
A new instance of ADT.
-
#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.
- #to_s ⇒ Object
- #unsafe_const ⇒ Object
- #unsafe_data ⇒ Object
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 |
.matcher ⇒ Object
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_s ⇒ Object
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_const ⇒ Object
34 |
# File 'lib/funkr/adt/adt.rb', line 34 def unsafe_const; @const; end |
#unsafe_data ⇒ Object
35 |
# File 'lib/funkr/adt/adt.rb', line 35 def unsafe_data; @data; end |