funkr 0.0.25 → 0.0.26
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/funkr/extensions/array.rb +7 -4
- data/lib/funkr/extensions/enumerable.rb +7 -7
- data/lib/funkr/version.rb +1 -1
- metadata +2 -2
@@ -17,9 +17,10 @@ class Array
|
|
17
17
|
|
18
18
|
|
19
19
|
# Array can be made an applicative functor, for example :
|
20
|
-
#
|
21
|
-
#
|
22
|
-
#
|
20
|
+
#
|
21
|
+
# f = Array.curry_lift_proc{|x,y| x + y}
|
22
|
+
# f.apply([0,4]).apply([5,7]) => [5, 7, 9, 11]
|
23
|
+
# f.apply([0,4]).apply([]) => []
|
23
24
|
include Applicative
|
24
25
|
extend Applicative::ClassMethods
|
25
26
|
|
@@ -49,10 +50,12 @@ class Array
|
|
49
50
|
end
|
50
51
|
|
51
52
|
|
52
|
-
# Array is also a monad
|
53
53
|
include Monad
|
54
54
|
extend Monad::ClassMethods
|
55
55
|
|
56
|
+
# Array is also a monad
|
57
|
+
#
|
58
|
+
# [1,2].bind{|x| [3,4].bind{|y| x + y}} # => [4,5,5,6]
|
56
59
|
def bind(&block)
|
57
60
|
self.map(&block).flatten(1)
|
58
61
|
end
|
@@ -40,26 +40,27 @@ module Enumerable
|
|
40
40
|
return [ inc, [] ]
|
41
41
|
end
|
42
42
|
|
43
|
-
#
|
43
|
+
# builds up disjoint groups of n elements or less
|
44
44
|
def groups_of(n)
|
45
45
|
g = self.take(n)
|
46
46
|
return [] if g.empty?
|
47
47
|
[g] + self.drop(n).groups_of(n)
|
48
48
|
end
|
49
49
|
|
50
|
-
#
|
50
|
+
# builds up sliding groups of exactly n elements
|
51
51
|
def sliding_groups_of(n)
|
52
52
|
return [] if self.size < n
|
53
53
|
[ self.take(n) ] + self.drop(1).sliding_groups_of(n)
|
54
54
|
end
|
55
55
|
|
56
|
-
#
|
56
|
+
# find the position of a sequence
|
57
|
+
# [1,2,3,4,5,4,3,2,1].seq_index([4,3]) # => 5
|
57
58
|
def seq_index(seq)
|
58
59
|
self.sliding_groups_of(seq.size).index(seq)
|
59
60
|
end
|
60
61
|
|
61
|
-
#
|
62
|
-
#
|
62
|
+
# Takes a block predicate p(x,y) and builds an array of elements so
|
63
|
+
# that for any (a,b), a being before b in the list, p(a,b) holds.
|
63
64
|
def make_uniq_by(&block)
|
64
65
|
result = []
|
65
66
|
self.each do |e|
|
@@ -68,8 +69,7 @@ module Enumerable
|
|
68
69
|
return result
|
69
70
|
end
|
70
71
|
|
71
|
-
#
|
72
|
-
# codé en impératif parce que inject est trop lent :(
|
72
|
+
# compare 2 enumerables, and returns [ [missing] , [intersection], [added] ]
|
73
73
|
def diff_with(other, &block)
|
74
74
|
m, i, a = [], [], []
|
75
75
|
u_s = self.make_uniq_by(&block)
|
data/lib/funkr/version.rb
CHANGED