church 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/church/array.rb +11 -5
- data/lib/church/utils.rb +6 -0
- data/lib/church/version.rb +1 -1
- data/spec/array_spec.rb +12 -0
- data/spec/spec_helper.rb +2 -0
- data/spec/utils_spec.rb +7 -0
- metadata +1 -1
data/lib/church/array.rb
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
module Church
|
2
|
-
# Returns the size of an array or string
|
2
|
+
# Returns the size of an array or string.
|
3
3
|
SIZE = -> coll {
|
4
4
|
coll == [] || coll == '' ? 0 : 1 + SIZE[coll[1..-1]]
|
5
5
|
}
|
6
6
|
|
7
|
-
# Maps fn over coll
|
7
|
+
# Maps fn over coll.
|
8
8
|
MAP = -> coll, &fn {
|
9
9
|
coll == [] ? [] : [fn[coll[0]]] + MAP[coll[1..-1], &fn]
|
10
10
|
}
|
11
11
|
|
12
|
-
# Maps fn over coll
|
12
|
+
# Maps fn over coll in-place, modifying the original collection.
|
13
13
|
MAP_BANG = -> coll, &fn {
|
14
14
|
sz = SIZE[coll]
|
15
15
|
i = 0
|
@@ -20,7 +20,7 @@ module Church
|
|
20
20
|
})[]
|
21
21
|
}
|
22
22
|
|
23
|
-
# Reduces fn over coll
|
23
|
+
# Reduces fn over coll.
|
24
24
|
REDUCE = -> coll, &fn {
|
25
25
|
sz = SIZE[coll]
|
26
26
|
ret = coll[0]
|
@@ -32,7 +32,7 @@ module Church
|
|
32
32
|
})[]
|
33
33
|
}
|
34
34
|
|
35
|
-
# Filters coll through fn
|
35
|
+
# Filters coll through fn.
|
36
36
|
FILTER = -> coll, &fn {
|
37
37
|
coll == [] ? [] : (fn[coll[0]] ? [coll[0]] : []) + FILTER[coll[1..-1], &fn]
|
38
38
|
}
|
@@ -83,4 +83,10 @@ module Church
|
|
83
83
|
|
84
84
|
# Sorts the collection in reverse order.
|
85
85
|
RSORT = COMPOSE[REVERSE, SORT]
|
86
|
+
|
87
|
+
# Returns the collection with the first n elements dropped.
|
88
|
+
DROP = -> coll, n { coll[n..-1] }
|
89
|
+
|
90
|
+
# Returns the first n elements of the collection.
|
91
|
+
TAKE = -> coll, n { coll[0, n] }
|
86
92
|
end
|
data/lib/church/utils.rb
CHANGED
data/lib/church/version.rb
CHANGED
data/spec/array_spec.rb
CHANGED
@@ -78,3 +78,15 @@ describe 'EACH' do
|
|
78
78
|
expect(x).to be 6
|
79
79
|
end
|
80
80
|
end
|
81
|
+
|
82
|
+
describe 'DROP' do
|
83
|
+
it "should drop the first n elements of a collection" do
|
84
|
+
expect(DROP[[1, 2, 3, 4], 2]).to eq [3, 4]
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
describe 'TAKE' do
|
89
|
+
it "should take the first n elements of a collection" do
|
90
|
+
expect(TAKE[[1, 2, 3, 4], 2]).to eq [1, 2]
|
91
|
+
end
|
92
|
+
end
|
data/spec/spec_helper.rb
CHANGED
data/spec/utils_spec.rb
CHANGED