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 CHANGED
@@ -1,15 +1,15 @@
1
1
  module Church
2
- # Returns the size of an array or string using recursion.
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 recursively.
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 iteratively and in-place, modifying the original collection.
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 iteratively.
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 using recursion.
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
@@ -13,4 +13,10 @@ module Church::Utils
13
13
  n == 1 ? cs << 1 : collatz_p[cs << n]
14
14
  })[[n]]
15
15
  }
16
+
17
+ DIGITSUM = -> n {
18
+ (summer = -> sum {
19
+ (n /= 10) == 0 ? sum : summer[sum + n % 10]
20
+ })[n % 10]
21
+ }
16
22
  end
@@ -1,3 +1,3 @@
1
1
  module Church
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
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
@@ -1,6 +1,8 @@
1
1
  require 'simplecov'
2
2
  require 'simplecov-console'
3
3
 
4
+ SimpleCov.add_filter '/spec'
5
+
4
6
  SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
5
7
  SimpleCov::Formatter::HTMLFormatter,
6
8
  SimpleCov::Formatter::Console
data/spec/utils_spec.rb CHANGED
@@ -28,3 +28,10 @@ describe 'COLLATZ' do
28
28
  expect(SIZE[COLLATZ[194]]).to be 120
29
29
  end
30
30
  end
31
+
32
+ describe 'DIGITSUM' do
33
+ it "should return the digital sum of its argument" do
34
+ expect(DIGITSUM[12345]).to be 15
35
+ expect(DIGITSUM[0]).to be 0
36
+ end
37
+ 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.1
4
+ version: 0.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: