funkr 0.0.24 → 0.0.25
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/funkr/adt/adt.rb +6 -6
- data/lib/funkr/types/maybe.rb +27 -27
- data/lib/funkr/types/simple_record.rb +9 -8
- data/lib/funkr/version.rb +1 -1
- metadata +2 -2
data/lib/funkr/adt/adt.rb
CHANGED
@@ -13,7 +13,7 @@ module Funkr
|
|
13
13
|
end
|
14
14
|
|
15
15
|
# Declare ADT constructors, for example :
|
16
|
-
#
|
16
|
+
# class Maybe < ADT; adt :just, :nothing; end
|
17
17
|
def self.adt(*constructs)
|
18
18
|
build_adt(constructs)
|
19
19
|
build_matcher(constructs)
|
@@ -22,11 +22,11 @@ module Funkr
|
|
22
22
|
def self.matcher; @matcher; end
|
23
23
|
|
24
24
|
# Match your ADT against its constructors, for example :
|
25
|
-
#
|
26
|
-
#
|
27
|
-
#
|
28
|
-
#
|
29
|
-
#
|
25
|
+
# a = Maybe.just("hello")
|
26
|
+
# a.match do |on|
|
27
|
+
# on.just{|x| puts x}
|
28
|
+
# on.nothing{ }
|
29
|
+
# end
|
30
30
|
def match(&block)
|
31
31
|
self.class.matcher.match_with(normal_form, &block)
|
32
32
|
end
|
data/lib/funkr/types/maybe.rb
CHANGED
@@ -23,18 +23,18 @@ module Funkr
|
|
23
23
|
### Categories
|
24
24
|
|
25
25
|
include Functor
|
26
|
-
|
27
|
-
# Maybe.nothing.map{|x| something(x)} # => nothing
|
28
|
-
# Maybe.just(x).map{|x| something(x)} # => just something(x)
|
29
|
-
#
|
26
|
+
|
30
27
|
# {Funkr::Categories::Functor#map see functor map}
|
28
|
+
#
|
29
|
+
# Maybe.nothing.map{|x| something(x)} # => nothing
|
30
|
+
# Maybe.just(x).map{|x| something(x)} # => just something(x)
|
31
31
|
def map(&block)
|
32
32
|
# This implementation isn't safe but is a bit faster than the
|
33
33
|
# safe one. A safe implementation would be as follow :
|
34
|
-
#
|
35
|
-
#
|
36
|
-
#
|
37
|
-
#
|
34
|
+
# self.match do |on|
|
35
|
+
# on.just {|v| self.class.just(yield(v))}
|
36
|
+
# on.nothing { self }
|
37
|
+
# end
|
38
38
|
if self.just? then self.class.just(yield(unsafe_content))
|
39
39
|
else self end
|
40
40
|
end
|
@@ -42,27 +42,27 @@ module Funkr
|
|
42
42
|
include Applicative
|
43
43
|
extend Applicative::ClassMethods
|
44
44
|
|
45
|
-
# Maybe can be made an applicative functor, for example :
|
46
|
-
# f = Maybe.curry_lift_proc{|x,y| x + y}
|
47
|
-
# a = Maybe.just(3)
|
48
|
-
# b = Maybe.just(4)
|
49
|
-
# c = Maybe.nothing
|
50
|
-
# f.apply(a).apply(b) => Just 7
|
51
|
-
# f.apply(a).apply(c) => Nothing
|
52
|
-
#
|
53
45
|
# {Funkr::Categories::Applicative#apply see applicative apply}
|
46
|
+
#
|
47
|
+
# Maybe can be made an applicative functor, for example :
|
48
|
+
# f = Maybe.curry_lift_proc{|x,y| x + y}
|
49
|
+
# a = Maybe.just(3)
|
50
|
+
# b = Maybe.just(4)
|
51
|
+
# c = Maybe.nothing
|
52
|
+
# f.apply(a).apply(b) => Just 7
|
53
|
+
# f.apply(a).apply(c) => Nothing
|
54
54
|
def apply(to)
|
55
55
|
# This implementation isn't safe but is a bit faster than the
|
56
56
|
# safe one. A safe implementation would be as follow :
|
57
|
-
#
|
58
|
-
#
|
59
|
-
#
|
60
|
-
#
|
61
|
-
#
|
57
|
+
# self.match do |f_on|
|
58
|
+
# f_on.just do |f|
|
59
|
+
# to.match do |t_on|
|
60
|
+
# t_on.just {|t| self.class.unit(f.call(t)) }
|
61
|
+
# t_on.nothing { to }
|
62
|
+
# end
|
62
63
|
# end
|
64
|
+
# f_on.nothing { self }
|
63
65
|
# end
|
64
|
-
# f_on.nothing { self }
|
65
|
-
# end
|
66
66
|
if self.just? and to.just? then
|
67
67
|
self.class.unit(self.unsafe_content.call(to.unsafe_content))
|
68
68
|
else self.class.nothing end
|
@@ -103,10 +103,10 @@ module Funkr
|
|
103
103
|
def bind(&block)
|
104
104
|
# This implementation isn't safe but is a bit faster than the
|
105
105
|
# safe one. A safe implementation would be as follow :
|
106
|
-
#
|
107
|
-
#
|
108
|
-
#
|
109
|
-
#
|
106
|
+
# self.match do |on|
|
107
|
+
# on.just {|v| yield(v)}
|
108
|
+
# on.nothing {self}
|
109
|
+
# end
|
110
110
|
if self.just? then yield(self.unsafe_content)
|
111
111
|
else self end
|
112
112
|
end
|
@@ -11,16 +11,17 @@ module Funkr
|
|
11
11
|
# mistyping it or by combining it with a different structure
|
12
12
|
# - you have easy access to fields : named AND positional
|
13
13
|
#
|
14
|
-
# usage :
|
15
|
-
#
|
16
|
-
#
|
17
|
-
#
|
18
|
-
#
|
19
|
-
#
|
14
|
+
# usage :
|
15
|
+
#
|
16
|
+
# r = SimpleRecord.new( name: "Paul", age: 27 )
|
17
|
+
# r.name # => "Paul"
|
18
|
+
# r.age # => 27
|
19
|
+
# name, age = r # => [ "Paul", 27 ]
|
20
|
+
# r.with(age: 29) # => [ "Paul", 29 ]
|
20
21
|
#
|
21
22
|
# other usage :
|
22
|
-
#
|
23
|
-
#
|
23
|
+
# class Person < SimpleRecord; fields :name, :age; end
|
24
|
+
# Person.new( name: Paul ) => Error, missing :age
|
24
25
|
class SimpleRecord < Array
|
25
26
|
|
26
27
|
class << self; attr_accessor :fields_list; end
|
data/lib/funkr/version.rb
CHANGED