church 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/church/array.rb +17 -0
- data/lib/church/version.rb +1 -1
- data/spec/array_spec.rb +16 -0
- metadata +1 -1
data/lib/church/array.rb
CHANGED
@@ -81,6 +81,7 @@ module Church
|
|
81
81
|
})[]
|
82
82
|
}
|
83
83
|
|
84
|
+
# Zips each element of coll with its index within the collection.
|
84
85
|
INDEXED = -> coll {
|
85
86
|
sz = SIZE[coll]
|
86
87
|
ret = []
|
@@ -92,6 +93,19 @@ module Church
|
|
92
93
|
})[]
|
93
94
|
}
|
94
95
|
|
96
|
+
# Reverses the collection.
|
97
|
+
REVERSE = -> coll {
|
98
|
+
sz = SIZE[coll]
|
99
|
+
ret = coll[0, 0]
|
100
|
+
i = sz
|
101
|
+
|
102
|
+
(reverser = -> {
|
103
|
+
ret << coll[i - 1]
|
104
|
+
(i -= 1) == 0 ? ret : reverser[]
|
105
|
+
})[]
|
106
|
+
}
|
107
|
+
|
108
|
+
# Sorts the collection.
|
95
109
|
SORT = -> coll {
|
96
110
|
x, *xs = *coll
|
97
111
|
coll == [] ? []
|
@@ -99,4 +113,7 @@ module Church
|
|
99
113
|
[x] +
|
100
114
|
SORT[FILTER[xs, &-> y { y >= x }]]
|
101
115
|
}
|
116
|
+
|
117
|
+
# Sorts the collection in reverse order.
|
118
|
+
RSORT = -> coll { REVERSE[SORT[coll]] }
|
102
119
|
end
|
data/lib/church/version.rb
CHANGED
data/spec/array_spec.rb
CHANGED
@@ -43,12 +43,28 @@ describe 'FILTER' do
|
|
43
43
|
end
|
44
44
|
end
|
45
45
|
|
46
|
+
describe 'REVERSE' do
|
47
|
+
it "should reverse an array" do
|
48
|
+
expect(REVERSE[[1, 2, 3]]).to eq [3, 2, 1]
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should reverse a string" do
|
52
|
+
expect(REVERSE["Ruby"]).to eq "ybuR"
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
46
56
|
describe 'SORT' do
|
47
57
|
it "should sort an array" do
|
48
58
|
expect(SORT[[4, 3, 2, 2, 1, 3]]).to eq [1, 2, 2, 3, 3, 4]
|
49
59
|
end
|
50
60
|
end
|
51
61
|
|
62
|
+
describe 'RSORT' do
|
63
|
+
it "should sort an array in reverse order" do
|
64
|
+
expect(RSORT[[2, 1, 3]]).to eq [3, 2, 1]
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
52
68
|
describe 'INDEXED' do
|
53
69
|
it "should zip a collection with indices" do
|
54
70
|
expect(INDEXED[[1, 2, 3]]).to eq [[1, 0], [2, 1], [3, 2]]
|