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 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
@@ -1,3 +1,3 @@
1
1
  module Church
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
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]]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: church
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: