church 0.0.8 → 0.0.10

Sign up to get free protection for your applications and to get access to all the features.
data/lib/church/array.rb CHANGED
@@ -47,7 +47,7 @@ module Church
47
47
  ret = coll[0]
48
48
  i = 1
49
49
 
50
- (reducer = -> {
50
+ sz == 1 ? ret : (reducer = -> {
51
51
  ret = fn[ret, coll[i]]
52
52
  (i += 1) == sz ? ret : reducer[]
53
53
  })[]
data/lib/church/io.rb CHANGED
@@ -10,4 +10,17 @@ module Church
10
10
  PRINT = -> obj { $> << obj }
11
11
 
12
12
  PUTS = -> obj { $> << obj << "\n" }
13
+
14
+ CHARS = -> str {
15
+ sz = SIZE[str]
16
+ ret = []
17
+ i = 0
18
+
19
+ (chars_p = -> {
20
+ ret << str[i]
21
+ (i += 1) == sz ? ret : chars_p[]
22
+ })[]
23
+ }
24
+
25
+ JOIN = -> str, delim { str * delim }
13
26
  end
data/lib/church/math.rb CHANGED
@@ -1,42 +1,11 @@
1
1
  module Church
2
- CMP = -> a, b {
3
- a > b ? 1 : a < b ? -1 : 0
4
- }
5
-
6
- ADD = -> a, b {
7
- b == 0 ? a : ADD[a + CMP[b, 0], b + CMP[0, b]]
8
- }
9
-
10
- SUB = -> a, b {
11
- b == 0 ? a : SUB[a + (0 <=> b), b + (0 <=> b)]
12
- }
2
+ SQRT = -> n { n ** 2 ** -1 }
13
3
 
14
- MUL = -> a, b {
15
- (mul_p = -> c, d {
16
- d == b ? c + a * CMP[0, b] : mul_p[c + a * CMP[b, 0], d + CMP[b, 0]]
17
- })[a * CMP[b, 0], 0]
18
- }
19
-
20
- DIV = -> a, b {
21
- div_p = -> d {
22
- d = ADD[d, CMP[b, 0]]
23
- a = SUB[a, MUL[b, CMP[b, 0]]]
24
- a >= MUL[b, CMP[b, 0]] ? div_p[d] : d
25
- }
26
- b > a ? 0 : div_p[0]
27
- }
28
-
29
- MOD = -> a, b {
30
- MOD_p = -> {
31
- a = SUB[a, MUL[b, CMP[b, 0]]]
32
- a > b ? MOD_p[] : (b < 0 ? SUB[a, b] : a)
33
- }
34
- MOD_p[]
35
- }
4
+ FLOOR = -> n { n - n % 1 }
36
5
 
37
- POW = -> a, b {
38
- (POW_p = -> c, d {
39
- d == b ? c : POW_p[MUL[a, c], ADD[d, 1]]
40
- })[1, 0]
6
+ PRIME = -> n {
7
+ n = n < 0 ? -n : n
8
+ n < 3 ? n == 2 : n % 2 == 0 ? false :
9
+ REDUCE[[1] + MAP[[*3..FLOOR[SQRT[n]]], &-> m { n % m }], &:*] > 0
41
10
  }
42
11
  end
@@ -0,0 +1,7 @@
1
+ module Church::Utils
2
+ ROT13 = -> chr {
3
+ ord = ORD[chr]
4
+ ord > 64 && ord < 91 ? CHR[(ord - 78) % 26 + 65] :
5
+ ord > 96 && ord < 123 ? CHR[(ord - 110) % 26 + 97] : chr
6
+ }
7
+ end
@@ -1,3 +1,3 @@
1
1
  module Church
2
- VERSION = "0.0.8"
2
+ VERSION = "0.0.10"
3
3
  end
data/lib/church.rb CHANGED
@@ -2,4 +2,5 @@ require 'church/array'
2
2
  require 'church/hash'
3
3
  require 'church/io'
4
4
  require 'church/math'
5
+ require 'church/utils'
5
6
  require 'church/version'
data/spec/io_spec.rb CHANGED
@@ -2,7 +2,7 @@ require 'spec_helper'
2
2
 
3
3
  include Church
4
4
 
5
- describe "CHR" do
5
+ describe 'CHR' do
6
6
  (1..122).each do |ord|
7
7
  it "should return its argument's corresponding character" do
8
8
  expect(CHR[ord]).to eq ord.chr
@@ -10,10 +10,26 @@ describe "CHR" do
10
10
  end
11
11
  end
12
12
 
13
- describe "ORD" do
13
+ describe 'ORD' do
14
14
  ("\1".."z").each do |chr|
15
15
  it "should return its argument's corresponding ordinal value" do
16
16
  expect(ORD[chr]).to be chr.ord
17
17
  end
18
18
  end
19
19
  end
20
+
21
+ describe 'CHARS' do
22
+ it "should return an array of the string's characters" do
23
+ expect(CHARS['Ruby']).to eq %w[R u b y]
24
+ end
25
+ end
26
+
27
+ describe 'JOIN' do
28
+ it "should join a string on a delimiter" do
29
+ expect(JOIN[[1, 2, 3], ',']).to eq '1,2,3'
30
+ end
31
+
32
+ it "should be able to join on an empty delimeter" do
33
+ expect(JOIN[[1, 2, 3], '']).to eq '123'
34
+ end
35
+ end
data/spec/math_spec.rb CHANGED
@@ -1,87 +1,24 @@
1
1
  require 'spec_helper'
2
+ require 'prime'
2
3
 
3
4
  include Church
4
5
 
5
- describe 'ADD' do
6
- it "should work with an LHS of 0" do
7
- expect(ADD[0, 2]).to be 2
8
- end
9
-
10
- it "should work with an RHS of 0" do
11
- expect(ADD[3, 0]).to be 3
12
- end
13
-
14
- it "should work with a negative LHS" do
15
- expect(ADD[-2, 4]).to be 2
16
- end
17
-
18
- it "should work with a negative RHS" do
19
- expect(ADD[2, -4]).to be -2
20
- end
21
-
22
- it "should work with two negatives" do
23
- expect(ADD[-2, -4]).to be -6
6
+ describe 'SQRT' do
7
+ it "should return its argument's square root" do
8
+ expect(SQRT[5]).to eq Math.sqrt 5
24
9
  end
25
10
  end
26
11
 
27
- describe 'SUB' do
28
- it "should work with an LHS of 0" do
29
- expect(SUB[0, 2]).to be -2
30
- end
31
-
32
- it "should work with an RHS of 0" do
33
- expect(SUB[3, 0]).to be 3
34
- end
35
-
36
- it "should work with a negative LHS" do
37
- expect(SUB[-2, 4]).to be -6
38
- end
39
-
40
- it "should work with a negative RHS" do
41
- expect(SUB[2, -4]).to be 6
42
- end
43
-
44
- it "should work with two negatives" do
45
- expect(SUB[-2, -4]).to be 2
12
+ describe 'FLOOR' do
13
+ it "should round its argument down" do
14
+ expect(FLOOR[2.5]).to eq 2
46
15
  end
47
16
  end
48
17
 
49
- describe 'MUL' do
50
- it "should work with an LHS of 0" do
51
- expect(MUL[0, 2]).to be 0
52
- end
53
-
54
- it "should work with an RHS of 0" do
55
- expect(MUL[3, 0]).to be 0
56
- end
57
-
58
- it "should work with a negative LHS" do
59
- expect(MUL[-2, 4]).to be -8
60
- end
61
-
62
- it "should work with a negative RHS" do
63
- expect(MUL[2, -4]).to be -8
64
- end
65
-
66
- it "should work with two negatives" do
67
- expect(MUL[-2, -4]).to be 8
68
- end
69
- end
70
-
71
- describe 'DIV' do
72
- it "should work with an LHS of 0" do
73
- expect(DIV[0, 2]).to be 0
74
- end
75
-
76
- it "should work with a negative RHS" do
77
- expect(DIV[4, -2]).to be -2
78
- end
79
-
80
- it "should divide numbers into themselves once" do
81
- expect(DIV[3, 3]).to be 1
82
- end
83
-
84
- it "should truncate" do
85
- expect(DIV[3, 4]).to be 0
18
+ describe 'PRIME' do
19
+ (-25..25).each do |i|
20
+ it "should return its argument's primality" do
21
+ expect(PRIME[i]).to be i.prime?
22
+ end
86
23
  end
87
24
  end
@@ -0,0 +1,10 @@
1
+ require 'spec_helper'
2
+
3
+ include Church::Utils
4
+
5
+ describe 'ROT13' do
6
+ it "should rotate a lowercase letter 13 places" do
7
+ expect(ROT13['a']).to eq 'n'
8
+ expect(ROT13['n']).to eq 'a'
9
+ end
10
+ 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.0.8
4
+ version: 0.0.10
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -61,12 +61,14 @@ files:
61
61
  - lib/church/hash.rb
62
62
  - lib/church/io.rb
63
63
  - lib/church/math.rb
64
+ - lib/church/utils.rb
64
65
  - lib/church/version.rb
65
66
  - spec/array_spec.rb
66
67
  - spec/hash_spec.rb
67
68
  - spec/io_spec.rb
68
69
  - spec/math_spec.rb
69
70
  - spec/spec_helper.rb
71
+ - spec/utils_spec.rb
70
72
  homepage: https://github.com/threeifbywhiskey/church
71
73
  licenses:
72
74
  - WTFPL
@@ -99,3 +101,4 @@ test_files:
99
101
  - spec/io_spec.rb
100
102
  - spec/math_spec.rb
101
103
  - spec/spec_helper.rb
104
+ - spec/utils_spec.rb