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 +1 -1
- data/lib/church/io.rb +13 -0
- data/lib/church/math.rb +6 -37
- data/lib/church/utils.rb +7 -0
- data/lib/church/version.rb +1 -1
- data/lib/church.rb +1 -0
- data/spec/io_spec.rb +18 -2
- data/spec/math_spec.rb +12 -75
- data/spec/utils_spec.rb +10 -0
- metadata +4 -1
data/lib/church/array.rb
CHANGED
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
|
-
|
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
|
-
|
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
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
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
|
data/lib/church/utils.rb
ADDED
data/lib/church/version.rb
CHANGED
data/lib/church.rb
CHANGED
data/spec/io_spec.rb
CHANGED
@@ -2,7 +2,7 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
include Church
|
4
4
|
|
5
|
-
describe
|
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
|
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 '
|
6
|
-
it "should
|
7
|
-
expect(
|
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 '
|
28
|
-
it "should
|
29
|
-
expect(
|
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 '
|
50
|
-
|
51
|
-
|
52
|
-
|
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
|
data/spec/utils_spec.rb
ADDED
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.
|
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
|