abstractivator 0.0.21 → 0.0.22
Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ed70b3e745e17ec7a24ef81a0f8734a88408f6ee
|
4
|
+
data.tar.gz: 7f463093462a219c2f055da69e14f867ad45d5cd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 36cc3d351e63c62cb1a84cd073747a734a145b1712111fad30431df3f82c59b5eb7cf4eca6c096d8eb824131c2b953a28eedcf0b83cc8ba1bc327ff8260aa1f2
|
7
|
+
data.tar.gz: aabc33b65cd3f7133f78828e2f14e379c360f64accbccd2df9d6f399594d88730b8ccfd1d29904ec763f83a12f212435fd74d5873889e56606088174dccc8e9d
|
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'set'
|
2
|
+
require 'abstractivator/proc_ext'
|
2
3
|
|
3
4
|
module Enumerable
|
4
5
|
|
@@ -48,7 +49,7 @@ module Enumerable
|
|
48
49
|
end
|
49
50
|
|
50
51
|
def hash_map(get_key=->x{x}, &get_value)
|
51
|
-
Hash[self.map{|x| [
|
52
|
+
Hash[self.map{|x| [Proc.loose_call(get_key, [x]), get_value ? get_value.call(x) : x]}]
|
52
53
|
end
|
53
54
|
|
54
55
|
def outer_join(right, get_left_key, get_right_key, default_value)
|
@@ -30,7 +30,10 @@ class Proc
|
|
30
30
|
end
|
31
31
|
|
32
32
|
def self.loose_call(x, args, &block)
|
33
|
-
x
|
33
|
+
x = x.to_proc if x.respond_to?(:to_proc)
|
34
|
+
x.respond_to?(:call) or return x
|
35
|
+
args = args.take(x.arity).pad_right(x.arity) if x.arity >= 0
|
36
|
+
x.call(*args, &block)
|
34
37
|
end
|
35
38
|
end
|
36
39
|
|
@@ -141,4 +141,24 @@ describe Enumerable do
|
|
141
141
|
expect(result).to eql [:x, 1, 2, 3]
|
142
142
|
end
|
143
143
|
end
|
144
|
+
|
145
|
+
describe '#hash_map' do
|
146
|
+
let!(:xs) { [Object, Hash, Array] }
|
147
|
+
it 'turns an array into a map' do
|
148
|
+
result = xs.hash_map(proc{|x| x.to_s[0]}) {|x| x.to_s }
|
149
|
+
expect(result).to eql({ 'O' => 'Object', 'H' => 'Hash', 'A' => 'Array' })
|
150
|
+
end
|
151
|
+
it 'the value transformer can be omitted' do
|
152
|
+
result = xs.hash_map(proc{|x| x.to_s[0]})
|
153
|
+
expect(result).to eql({ 'O' => Object, 'H' => Hash, 'A' => Array })
|
154
|
+
end
|
155
|
+
it 'the key transformer can be omitted' do
|
156
|
+
result = xs.hash_map(&:to_s)
|
157
|
+
expect(result).to eql({ Object => 'Object', Hash => 'Hash', Array => 'Array' })
|
158
|
+
end
|
159
|
+
it 'the key transformer is called loosely' do
|
160
|
+
result = xs.hash_map(:to_s)
|
161
|
+
expect(result).to eql({ 'Object' => Object, 'Hash' => Hash, 'Array' => Array })
|
162
|
+
end
|
163
|
+
end
|
144
164
|
end
|
@@ -34,7 +34,10 @@ context 'in the world of functional programming' do
|
|
34
34
|
|
35
35
|
describe 'Proc::loose_call' do
|
36
36
|
it 'returns the first argument if it is not a proc' do
|
37
|
-
expect(Proc.loose_call(
|
37
|
+
expect(Proc.loose_call('a', ['b', 'c'])).to eql 'a'
|
38
|
+
end
|
39
|
+
it 'attempts to convert the first argument to proc' do
|
40
|
+
expect(Proc.loose_call(:to_s, ['5'])).to eql '5'
|
38
41
|
end
|
39
42
|
it 'calls the proc with an appropriate number of arguments' do
|
40
43
|
events = []
|
@@ -48,6 +51,9 @@ context 'in the world of functional programming' do
|
|
48
51
|
it 'pads with nils' do
|
49
52
|
expect(Proc.loose_call(->(a, b) {[a, b]}, [1])).to eql [1, nil]
|
50
53
|
end
|
54
|
+
it 'works with variable-arity procs' do
|
55
|
+
expect(Proc.loose_call(->(*args) {args}, [1, 2])).to eql [1, 2]
|
56
|
+
end
|
51
57
|
end
|
52
58
|
|
53
59
|
describe 'Proc#loosen_args' do
|