church 0.0.6 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/church/hash.rb +33 -0
- data/lib/church/version.rb +1 -1
- data/lib/church.rb +2 -1
- data/spec/hash_spec.rb +27 -0
- metadata +4 -1
data/lib/church/hash.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
module Church
|
2
|
+
KEYS = -> hash { MAP[[*hash], &:first] }
|
3
|
+
|
4
|
+
VALUES = -> hash { MAP[[*hash], &:last] }
|
5
|
+
|
6
|
+
INVERT = -> hash {
|
7
|
+
ks, vs = KEYS[hash], VALUES[hash]
|
8
|
+
sz = SIZE[ks]
|
9
|
+
ret = {}
|
10
|
+
i = 0
|
11
|
+
|
12
|
+
(inverter = -> {
|
13
|
+
ret[vs[i]] = ks[i]
|
14
|
+
(i += 1) == sz ? ret : inverter[]
|
15
|
+
})[]
|
16
|
+
}
|
17
|
+
|
18
|
+
MERGE = -> a, b {
|
19
|
+
all = [*a] + [*b]
|
20
|
+
sz = SIZE[all]
|
21
|
+
ret = {}
|
22
|
+
i = 0
|
23
|
+
|
24
|
+
(merger = -> {
|
25
|
+
ret[all[i][0]] = all[i][1]
|
26
|
+
(i += 1) == sz ? ret : merger[]
|
27
|
+
})[]
|
28
|
+
}
|
29
|
+
end
|
30
|
+
|
31
|
+
include Church
|
32
|
+
|
33
|
+
p INVERT[{a: 1, b: 2, c: 3}]
|
data/lib/church/version.rb
CHANGED
data/lib/church.rb
CHANGED
data/spec/hash_spec.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
include Church
|
4
|
+
|
5
|
+
describe 'KEYS' do
|
6
|
+
it "should return the hash's keys" do
|
7
|
+
expect(KEYS[{a: 1, b: 2, c: 3}]).to eq [:a, :b, :c]
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
describe 'VALUES' do
|
12
|
+
it "should return the hash's values" do
|
13
|
+
expect(VALUES[{a: 1, b: 2, c: 3}]).to eq [1, 2, 3]
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
describe 'INVERT' do
|
18
|
+
it "should invert the hash's keys and values" do
|
19
|
+
expect(INVERT[{a: 1, b: 2, c: 3}]).to eq({1 => :a, 2 => :b, 3 => :c})
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe 'MERGE' do
|
24
|
+
it "should merge two hashes" do
|
25
|
+
expect(MERGE[{a: 1, b: 2}, {c: 3, d: 4}]).to eq({a: 1, b: 2, c: 3, d: 4})
|
26
|
+
end
|
27
|
+
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.
|
4
|
+
version: 0.0.7
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -58,10 +58,12 @@ files:
|
|
58
58
|
- church.gemspec
|
59
59
|
- lib/church.rb
|
60
60
|
- lib/church/array.rb
|
61
|
+
- lib/church/hash.rb
|
61
62
|
- lib/church/io.rb
|
62
63
|
- lib/church/math.rb
|
63
64
|
- lib/church/version.rb
|
64
65
|
- spec/array_spec.rb
|
66
|
+
- spec/hash_spec.rb
|
65
67
|
- spec/io_spec.rb
|
66
68
|
- spec/math_spec.rb
|
67
69
|
- spec/spec_helper.rb
|
@@ -93,6 +95,7 @@ summary: Church provides top-level constant Procs to do various and sundry kinds
|
|
93
95
|
functional programming.
|
94
96
|
test_files:
|
95
97
|
- spec/array_spec.rb
|
98
|
+
- spec/hash_spec.rb
|
96
99
|
- spec/io_spec.rb
|
97
100
|
- spec/math_spec.rb
|
98
101
|
- spec/spec_helper.rb
|