funkr 0.0.2 → 0.0.3
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/categories/applicative.rb +1 -1
- data/lib/funkr/extensions.rb +2 -0
- data/lib/funkr/extensions/array.rb +93 -0
- data/lib/funkr/extensions/fixnum.rb +2 -0
- data/lib/funkr/version.rb +1 -1
- data/test/tests.rb +27 -9
- metadata +5 -3
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
require 'funkr/categories'
|
|
2
|
+
|
|
3
|
+
class Array
|
|
4
|
+
|
|
5
|
+
include Funkr::Categories
|
|
6
|
+
|
|
7
|
+
class << self
|
|
8
|
+
def unit(e); self.new([e]); end
|
|
9
|
+
alias pure unit
|
|
10
|
+
def mzero; self.new(); end
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
### Categories
|
|
14
|
+
|
|
15
|
+
# array est déjà un functor via map
|
|
16
|
+
# include Functor
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
include Applicative
|
|
20
|
+
extend Applicative::ClassMethods
|
|
21
|
+
|
|
22
|
+
def apply(to)
|
|
23
|
+
map do |f|
|
|
24
|
+
to.map{ |t| f.call(t)}
|
|
25
|
+
end.flatten(1)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
include Alternative
|
|
29
|
+
|
|
30
|
+
def or_else(&block)
|
|
31
|
+
if empty? then yield
|
|
32
|
+
else self end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
include Monoid
|
|
37
|
+
extend Monoid::ClassMethods
|
|
38
|
+
|
|
39
|
+
def mplus(m_y)
|
|
40
|
+
self + m_y
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
include Monad
|
|
45
|
+
extend Monad::ClassMethods
|
|
46
|
+
|
|
47
|
+
def bind(&block)
|
|
48
|
+
self.map(&block).flatten(1)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def self.box(value)
|
|
52
|
+
if value.nil? then self.mzero
|
|
53
|
+
else self.unit(value) end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def unbox()
|
|
57
|
+
if self.empty? then nil
|
|
58
|
+
else self.first end
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
# other extensions
|
|
63
|
+
class NoHead; end
|
|
64
|
+
|
|
65
|
+
def head; empty? ? NoHead : first; end
|
|
66
|
+
|
|
67
|
+
def tail; size > 1 ? self[1..-1] : []; end
|
|
68
|
+
|
|
69
|
+
def all_different?
|
|
70
|
+
return true if tail.empty?
|
|
71
|
+
!tail.include?(head) and tail.all_different?
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# http://www.haskell.org/ghc/docs/6.12.2/html/libraries/base-4.2.0.1/src/Data-List.html#group
|
|
75
|
+
def group_seq_by(&block)
|
|
76
|
+
if empty? then []
|
|
77
|
+
else
|
|
78
|
+
a,b = tail.span{|x| (yield head) == (yield x)}
|
|
79
|
+
[a.unshift(head)] + b.group_seq_by(&block)
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# http://www.haskell.org/ghc/docs/6.12.2/html/libraries/base-4.2.0.1/src/GHC-List.html#span
|
|
84
|
+
def span(&block)
|
|
85
|
+
if empty? then [[],[]]
|
|
86
|
+
elsif (yield head) then
|
|
87
|
+
a,b = tail.span(&block)
|
|
88
|
+
[a.unshift(head),b]
|
|
89
|
+
else [[], self]
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
end
|
data/lib/funkr/version.rb
CHANGED
data/test/tests.rb
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
require 'rubygems'
|
|
2
2
|
require 'funkr/types'
|
|
3
|
+
require 'funkr/extensions'
|
|
3
4
|
|
|
4
5
|
include Funkr::Types
|
|
5
6
|
|
|
@@ -32,18 +33,35 @@ f = Maybe.full_lift_proc{|x,y| x + y}
|
|
|
32
33
|
puts f.call(m,m)
|
|
33
34
|
puts f.call(m,n)
|
|
34
35
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
36
|
+
puts Maybe.mconcat([Maybe.just(10),
|
|
37
|
+
Maybe.just(20),
|
|
38
|
+
Maybe.nothing,
|
|
39
|
+
Maybe.just(30)])
|
|
39
40
|
|
|
40
41
|
puts(m <=> m)
|
|
41
42
|
puts(m <=> (m.map{|v| v+1}))
|
|
42
|
-
puts(m
|
|
43
|
+
puts(m < (m.map{|v| v+1}))
|
|
43
44
|
puts(m <=> n)
|
|
44
45
|
|
|
45
46
|
puts "\n> Boxing and unboxing"
|
|
46
|
-
puts m.unbox
|
|
47
|
-
puts n.unbox
|
|
48
|
-
puts (Maybe.box(12)).unbox
|
|
49
|
-
puts (Maybe.box(nil)).unbox
|
|
47
|
+
puts m.unbox.inspect
|
|
48
|
+
puts n.unbox.inspect
|
|
49
|
+
puts (Maybe.box(12)).unbox.inspect
|
|
50
|
+
puts (Maybe.box(nil)).unbox.inspect
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
a = [1,2,3]
|
|
54
|
+
b = [10,20,30]
|
|
55
|
+
|
|
56
|
+
puts "\n> Array full lift"
|
|
57
|
+
f = Array.full_lift_proc{|x,y| x + y}
|
|
58
|
+
puts f.call(a,b).inspect
|
|
59
|
+
puts f.call(a,[]).inspect
|
|
60
|
+
|
|
61
|
+
puts "\n> Array monad"
|
|
62
|
+
puts(a.bind do |x|
|
|
63
|
+
b.bind do |y|
|
|
64
|
+
Array.unit(x + y)
|
|
65
|
+
end
|
|
66
|
+
end.inspect)
|
|
67
|
+
|
metadata
CHANGED
|
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
|
5
5
|
segments:
|
|
6
6
|
- 0
|
|
7
7
|
- 0
|
|
8
|
-
-
|
|
9
|
-
version: 0.0.
|
|
8
|
+
- 3
|
|
9
|
+
version: 0.0.3
|
|
10
10
|
platform: ruby
|
|
11
11
|
authors:
|
|
12
12
|
- Paul Rivier
|
|
@@ -14,7 +14,7 @@ autorequire:
|
|
|
14
14
|
bindir: bin
|
|
15
15
|
cert_chain: []
|
|
16
16
|
|
|
17
|
-
date: 2011-03-
|
|
17
|
+
date: 2011-03-09 00:00:00 +01:00
|
|
18
18
|
default_executable:
|
|
19
19
|
dependencies: []
|
|
20
20
|
|
|
@@ -41,6 +41,8 @@ files:
|
|
|
41
41
|
- lib/funkr/categories/functor.rb
|
|
42
42
|
- lib/funkr/categories/monad.rb
|
|
43
43
|
- lib/funkr/categories/monoid.rb
|
|
44
|
+
- lib/funkr/extensions.rb
|
|
45
|
+
- lib/funkr/extensions/array.rb
|
|
44
46
|
- lib/funkr/extensions/fixnum.rb
|
|
45
47
|
- lib/funkr/types.rb
|
|
46
48
|
- lib/funkr/types/failable.rb
|