church 0.0.11 → 0.0.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/church/hash.rb +4 -0
- data/lib/church/io.rb +7 -1
- data/lib/church/lambda.rb +1 -0
- data/lib/church/math.rb +3 -0
- data/lib/church/utils.rb +1 -0
- data/lib/church/version.rb +1 -1
- data/spec/io_spec.rb +4 -8
- data/spec/lambda_spec.rb +1 -1
- data/spec/math_spec.rb +1 -1
- data/spec/utils_spec.rb +5 -0
- metadata +1 -1
data/lib/church/hash.rb
CHANGED
@@ -1,8 +1,11 @@
|
|
1
1
|
module Church
|
2
|
+
# Returns the keys of a Hash as an array
|
2
3
|
KEYS = -> hash { MAP[[*hash], &:first] }
|
3
4
|
|
5
|
+
# Returns the values of a Hash as an array
|
4
6
|
VALUES = -> hash { MAP[[*hash], &:last] }
|
5
7
|
|
8
|
+
# Inverts the keys and values of its hash argument
|
6
9
|
INVERT = -> hash {
|
7
10
|
ks, vs = KEYS[hash], VALUES[hash]
|
8
11
|
sz = SIZE[ks]
|
@@ -15,6 +18,7 @@ module Church
|
|
15
18
|
})[]
|
16
19
|
}
|
17
20
|
|
21
|
+
# Merges the keys and values of the two hashes into a new hash
|
18
22
|
MERGE = -> a, b {
|
19
23
|
all = [*a] + [*b]
|
20
24
|
sz = SIZE[all]
|
data/lib/church/io.rb
CHANGED
@@ -1,16 +1,21 @@
|
|
1
1
|
module Church
|
2
|
+
# Returns the equivalent of Fixnum#chr
|
2
3
|
CHR = -> o { '' << o }
|
3
4
|
|
5
|
+
# Returns the equivalent of String#ord
|
4
6
|
ORD = -> c {
|
5
7
|
(order = -> i {
|
6
8
|
'' << i == c ? i : order[i + 1]
|
7
9
|
})[1]
|
8
10
|
}
|
9
11
|
|
12
|
+
# Prints the object
|
10
13
|
PRINT = -> obj { $> << obj }
|
11
14
|
|
15
|
+
# Prints the object with a newline
|
12
16
|
PUTS = -> obj { $> << obj << "\n" }
|
13
17
|
|
18
|
+
# Returns an array of the characters of a string
|
14
19
|
CHARS = -> str {
|
15
20
|
sz = SIZE[str]
|
16
21
|
ret = []
|
@@ -22,5 +27,6 @@ module Church
|
|
22
27
|
})[]
|
23
28
|
}
|
24
29
|
|
25
|
-
|
30
|
+
# Joins the collection into a string on a specified delimiter
|
31
|
+
JOIN = -> coll, delim { coll * delim }
|
26
32
|
end
|
data/lib/church/lambda.rb
CHANGED
data/lib/church/math.rb
CHANGED
@@ -1,8 +1,11 @@
|
|
1
1
|
module Church
|
2
|
+
# Returns the square root of its argument
|
2
3
|
SQRT = -> n { n ** 2 ** -1 }
|
3
4
|
|
5
|
+
# Returns its argument rounded down
|
4
6
|
FLOOR = -> n { n - n % 1 }
|
5
7
|
|
8
|
+
# Returns the primality of its argument
|
6
9
|
PRIME = -> n {
|
7
10
|
n = n < 0 ? -n : n
|
8
11
|
n < 3 ? n == 2 : n % 2 == 0 ? false :
|
data/lib/church/utils.rb
CHANGED
data/lib/church/version.rb
CHANGED
data/spec/io_spec.rb
CHANGED
@@ -3,18 +3,14 @@ require 'spec_helper'
|
|
3
3
|
include Church
|
4
4
|
|
5
5
|
describe 'CHR' do
|
6
|
-
|
7
|
-
|
8
|
-
expect(CHR[ord]).to eq ord.chr
|
9
|
-
end
|
6
|
+
it "should return its argument's corresponding character" do
|
7
|
+
expect(CHR[83]).to eq 83.chr
|
10
8
|
end
|
11
9
|
end
|
12
10
|
|
13
11
|
describe 'ORD' do
|
14
|
-
|
15
|
-
|
16
|
-
expect(ORD[chr]).to be chr.ord
|
17
|
-
end
|
12
|
+
it "should return its argument's corresponding ordinal value" do
|
13
|
+
expect(ORD['q']).to be 'q'.ord
|
18
14
|
end
|
19
15
|
end
|
20
16
|
|
data/spec/lambda_spec.rb
CHANGED
data/spec/math_spec.rb
CHANGED
data/spec/utils_spec.rb
CHANGED