church 0.1.5 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
data/lib/church/array.rb CHANGED
@@ -12,24 +12,22 @@ module Church
12
12
  # Maps fn over coll in-place, modifying the original collection.
13
13
  MAP_BANG = -> coll, &fn {
14
14
  sz = SIZE[coll]
15
- i = 0
16
15
 
17
- (mapper = -> {
16
+ (mapper = -> i {
18
17
  coll[i] = fn[coll[i]]
19
- (i += 1) == sz ? coll : mapper[]
20
- })[]
18
+ (i += 1) == sz ? coll : mapper[i]
19
+ })[0]
21
20
  }
22
21
 
23
22
  # Reduces fn over coll.
24
23
  REDUCE = -> coll, &fn {
25
24
  sz = SIZE[coll]
26
25
  ret = coll[0]
27
- i = 1
28
26
 
29
- sz == 1 ? ret : (reducer = -> {
27
+ sz == 1 ? ret : (reducer = -> i {
30
28
  ret = fn[ret, coll[i]]
31
- (i += 1) == sz ? ret : reducer[]
32
- })[]
29
+ (i += 1) == sz ? ret : reducer[i]
30
+ })[1]
33
31
  }
34
32
 
35
33
  # Filters coll through fn.
@@ -40,36 +38,31 @@ module Church
40
38
  # Performs fn with each element of coll in turn.
41
39
  EACH = -> coll, &fn {
42
40
  sz = SIZE[coll]
43
- i = 0
44
41
 
45
- (eacher = -> {
42
+ (eacher = -> i {
46
43
  fn[coll[i]]
47
- (i += 1) == sz ? coll : eacher[]
48
- })[]
44
+ (i += 1) == sz ? coll : eacher[i]
45
+ })[0]
49
46
  }
50
47
 
51
48
  # Zips each element of coll with its index within the collection.
52
49
  INDEXED = -> coll {
53
50
  sz = SIZE[coll]
54
- ret = []
55
- i = 0
56
51
 
57
- (indexer = -> {
52
+ (indexer = -> ret, i {
58
53
  ret << [coll[i], i]
59
- (i += 1) == sz ? ret : indexer[]
60
- })[]
54
+ (i += 1) == sz ? ret : indexer[ret, i]
55
+ })[[], 0]
61
56
  }
62
57
 
63
58
  # Reverses the collection.
64
59
  REVERSE = -> coll {
65
60
  sz = SIZE[coll]
66
- ret = coll[0, 0]
67
- i = sz
68
61
 
69
- (reverser = -> {
62
+ (reverser = -> ret, i {
70
63
  ret << coll[i - 1]
71
- (i -= 1) == 0 ? ret : reverser[]
72
- })[]
64
+ (i -= 1) == 0 ? ret : reverser[ret, i]
65
+ })[coll[0, 0], sz]
73
66
  }
74
67
 
75
68
  # Sorts the collection.
@@ -92,6 +85,12 @@ module Church
92
85
  # Sorts the collection in reverse order.
93
86
  RSORT = COMPOSE[REVERSE, SORT]
94
87
 
88
+ # Returns the collection's minimum.
89
+ MIN = -> coll { SORT[coll][ 0] }
90
+
91
+ # Returns the collection's maximum.
92
+ MAX = -> coll { SORT[coll][-1] }
93
+
95
94
  # Returns the collection with the first n elements dropped.
96
95
  DROP = -> coll, n { coll[n..-1] }
97
96
 
data/lib/church/hash.rb CHANGED
@@ -9,25 +9,21 @@ module Church
9
9
  INVERT = -> hash {
10
10
  ks, vs = KEYS[hash], VALUES[hash]
11
11
  sz = SIZE[ks]
12
- ret = {}
13
- i = 0
14
12
 
15
- (inverter = -> {
13
+ (inverter = -> ret, i {
16
14
  ret[vs[i]] = ks[i]
17
- (i += 1) == sz ? ret : inverter[]
18
- })[]
15
+ (i += 1) == sz ? ret : inverter[ret, i]
16
+ })[{}, 0]
19
17
  }
20
18
 
21
19
  # Merges the keys and values of the two hashes into a new hash
22
20
  MERGE = -> a, b {
23
21
  all = [*a] + [*b]
24
22
  sz = SIZE[all]
25
- ret = {}
26
- i = 0
27
23
 
28
- (merger = -> {
24
+ (merger = -> ret, i {
29
25
  ret[all[i][0]] = all[i][1]
30
- (i += 1) == sz ? ret : merger[]
31
- })[]
26
+ (i += 1) == sz ? ret : merger[ret, i]
27
+ })[{}, 0]
32
28
  }
33
29
  end
data/lib/church/io.rb CHANGED
@@ -18,13 +18,11 @@ module Church
18
18
  # Returns an array of the characters of a string
19
19
  CHARS = -> str {
20
20
  sz = SIZE[str]
21
- ret = []
22
- i = 0
23
21
 
24
- (chars_p = -> {
22
+ (chars_p = -> ret, i {
25
23
  ret << str[i]
26
- (i += 1) == sz ? ret : chars_p[]
27
- })[]
24
+ (i += 1) == sz ? ret : chars_p[ret, i]
25
+ })[[], 0]
28
26
  }
29
27
 
30
28
  # Joins the collection into a string on a specified delimiter
@@ -0,0 +1,7 @@
1
+ module Church
2
+ RANGE = -> start, finish, step = 1 {
3
+ (ranger = -> ret {
4
+ start >= finish ? ret : ranger[ret << start += step]
5
+ })[[start]]
6
+ }
7
+ end
@@ -1,3 +1,3 @@
1
1
  module Church
2
- VERSION = "0.1.5"
2
+ VERSION = "0.1.6"
3
3
  end
data/lib/church.rb CHANGED
@@ -3,5 +3,6 @@ require 'church/array'
3
3
  require 'church/hash'
4
4
  require 'church/io'
5
5
  require 'church/math'
6
+ require 'church/range'
6
7
  require 'church/utils'
7
8
  require 'church/version'
data/spec/array_spec.rb CHANGED
@@ -73,6 +73,18 @@ describe 'RSORT' do
73
73
  end
74
74
  end
75
75
 
76
+ describe 'MIN' do
77
+ it "should return the collection's minimum" do
78
+ expect(MIN[[2, 1, 3]]).to be 1
79
+ end
80
+ end
81
+
82
+ describe 'MAX' do
83
+ it "should return the collection's minimum" do
84
+ expect(MAX[[3, 1, 2]]).to be 3
85
+ end
86
+ end
87
+
76
88
  describe 'INDEXED' do
77
89
  it "should zip a collection with indices" do
78
90
  expect(INDEXED[[1, 2, 3]]).to eq [[1, 0], [2, 1], [3, 2]]
@@ -0,0 +1,17 @@
1
+ require 'spec_helper'
2
+
3
+ include Church
4
+
5
+ describe 'RANGE' do
6
+ it "should work without a supplied step" do
7
+ expect(RANGE[1, 3]).to eq [1, 2, 3]
8
+ end
9
+
10
+ it "should work with a step" do
11
+ expect(RANGE[2, 6, 2]).to eq [2, 4, 6]
12
+ end
13
+
14
+ it "should work with a negative start" do
15
+ expect(RANGE[-1, 1]).to eq [-1, 0, 1]
16
+ end
17
+ end
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.1.5
4
+ version: 0.1.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -94,6 +94,7 @@ files:
94
94
  - lib/church/io.rb
95
95
  - lib/church/lambda.rb
96
96
  - lib/church/math.rb
97
+ - lib/church/range.rb
97
98
  - lib/church/utils.rb
98
99
  - lib/church/version.rb
99
100
  - spec/array_spec.rb
@@ -101,6 +102,7 @@ files:
101
102
  - spec/io_spec.rb
102
103
  - spec/lambda_spec.rb
103
104
  - spec/math_spec.rb
105
+ - spec/range_spec.rb
104
106
  - spec/spec_helper.rb
105
107
  - spec/utils_spec.rb
106
108
  homepage: https://github.com/threeifbywhiskey/church
@@ -135,5 +137,6 @@ test_files:
135
137
  - spec/io_spec.rb
136
138
  - spec/lambda_spec.rb
137
139
  - spec/math_spec.rb
140
+ - spec/range_spec.rb
138
141
  - spec/spec_helper.rb
139
142
  - spec/utils_spec.rb