hmap 0.1.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b91ede0c8bd571c77da15568b4418141005fd68b
4
+ data.tar.gz: 5d3be03169ec696f3a4c5787a54eed3a7a82e4f1
5
+ SHA512:
6
+ metadata.gz: 4490aad3efaa67f76f58fd9af4a61e5cf26c919140ecef9b11ac637b37d4b763b191efc84c218d7e6e3e7e0441deaff432201014756754e64384d16e6daadf7c
7
+ data.tar.gz: 2b443f56320051e2f824eb147de39ed6ecb8b9559041cee6692ed6986ad57b13078503c40b3206e409f4a682c3779977d4d13f110b24d3073380967f05a474fb
@@ -1,19 +1,6 @@
1
- # Copyright © 2011, José Pablo Fernández
1
+ require_relative 'hmap/hmap'
2
2
 
3
- class Hash
4
- def hmap(*attrs, &block)
5
- map(*attrs, &block).inject({}) do |new_hash, pair|
6
- if pair.is_a? Hash
7
- new_hash.merge(pair)
8
- elsif pair.is_a? Array
9
- if pair.length != 2
10
- raise "When using hmap, when you return arrays, they must have two and only two elements; #{pair} doesn't"
11
- end
12
- new_hash[pair[0]] = pair[1]
13
- new_hash
14
- else
15
- raise "When using hmap you need to return arrays or hashes, #{pair} isn't either."
16
- end
17
- end
18
- end
3
+
4
+ module Hmap
5
+ VERSION = '1.0.0'
19
6
  end
@@ -0,0 +1,65 @@
1
+ class Hash
2
+
3
+ class << self
4
+
5
+ # create Hash from given keys and mapped values
6
+ def map(obj, &block)
7
+ res = obj.map do |*args|
8
+ block.call(*args)
9
+ end
10
+
11
+ Hash[obj.zip res]
12
+ end
13
+
14
+
15
+ # map an Enumerable into a Hash, like Hash[obj.map ... ]
16
+ def hmap(obj, &block)
17
+ Hash[
18
+ obj.map do |*args|
19
+ block.call(*args)
20
+ end.compact
21
+ ]
22
+ end
23
+
24
+ end # class << self
25
+
26
+
27
+ # map the block's results back to a hash
28
+ def hmap(&block)
29
+ clone.hmap!(&block)
30
+ end
31
+
32
+ def hmap!(&block)
33
+ data = Hash.hmap(self, &block)
34
+ clear
35
+ merge! data
36
+ end
37
+
38
+
39
+ # map keys, but preserve associated values
40
+ # ie. http://apidock.com/rails/v4.2.7/Hash/transform_keys
41
+ def kmap(&block)
42
+ clone.kmap!(&block)
43
+ end
44
+
45
+ def kmap!(&block)
46
+ hmap! do |k, v|
47
+ [ block.arity <= 1 ? block.call(k) : block.call(k, v), v ]
48
+ end
49
+ end
50
+
51
+
52
+ # map values, but preserve associated keys
53
+ # ie. http://apidock.com/rails/v4.2.7/Hash/transform_values
54
+ def vmap(&block)
55
+ clone.vmap!(&block)
56
+ end
57
+
58
+ def vmap!(&block)
59
+ each do |k, v|
60
+ self[k] = block.arity <= 1 ? block.call(v) : block.call(k, v)
61
+ end
62
+ end
63
+
64
+
65
+ end
@@ -0,0 +1,279 @@
1
+ require 'minitest/autorun'
2
+ require 'hmap'
3
+
4
+
5
+ class HmapTest < Minitest::Test
6
+
7
+ def test_Hash_map
8
+ data = {
9
+ c: 2,
10
+ b: 3,
11
+ a: 1,
12
+ }
13
+
14
+ assert_equal(
15
+ data,
16
+ Hash.map(data.keys) {|x| data[x]}
17
+ )
18
+
19
+ assert_equal(
20
+ {},
21
+ Hash.map([]) {|x| nil}
22
+ )
23
+
24
+ assert_equal(
25
+ {
26
+ 1 => nil,
27
+ 2 => nil,
28
+ 3 => nil,
29
+ },
30
+ Hash.map([1,2,3]) {|x| nil}
31
+ )
32
+
33
+ assert_equal(
34
+ {
35
+ 1 => false,
36
+ 2 => false,
37
+ 3 => true,
38
+ },
39
+ Hash.map([1,2,3]) {|x| x > 2 }
40
+ )
41
+
42
+ assert_equal(
43
+ { 1 => [ 2, 3 ] },
44
+ Hash.map([1]) {|x| [ 2, 3 ] }
45
+ )
46
+ end
47
+
48
+
49
+ def test_Hash_hmap
50
+ data = {
51
+ c: 2,
52
+ b: 3,
53
+ a: 1,
54
+ }
55
+
56
+ assert_equal(
57
+ data,
58
+ Hash.hmap(data) {|k, v| [k, v]}
59
+ )
60
+
61
+ assert_equal(
62
+ { a: 2, b: 6, c: 4 },
63
+ Hash.hmap(data) {|k, v| [k, v * 2]}
64
+ )
65
+
66
+ assert_equal(
67
+ { 1 => 2, 2 => 4 },
68
+ Hash.hmap([1, 2]) {|x| [ x, x * 2 ] }
69
+ )
70
+
71
+ assert_equal(
72
+ data,
73
+ Hash.hmap(data.keys) {|x| [x, data[x]]}
74
+ )
75
+
76
+ assert_equal(
77
+ { nil => 3 },
78
+ Hash.hmap([1,2,3]) {|x| [nil, x]}
79
+ )
80
+
81
+ assert_equal(
82
+ {},
83
+ Hash.hmap([]) {|x| nil}
84
+ )
85
+
86
+ assert_equal(
87
+ {},
88
+ Hash.hmap([1, 2, 3]) {|x| nil}
89
+ )
90
+ end
91
+
92
+
93
+ def test_hmap
94
+ data = {
95
+ c: 2,
96
+ b: 3,
97
+ a: 1,
98
+ }
99
+
100
+ assert_equal(
101
+ {
102
+ c: 4,
103
+ b: 6,
104
+ a: 2,
105
+ },
106
+ data.hmap {|k, v| [k, v * 2]}
107
+ )
108
+
109
+ assert_equal(
110
+ {
111
+ cc: 4,
112
+ bb: 6,
113
+ aa: 2,
114
+ },
115
+ data.hmap {|k, v| [(k.to_s * 2).to_sym, v * 2]}
116
+ )
117
+
118
+ assert_equal(
119
+ {},
120
+ {}.hmap {|k, v| [k, v * 2]}
121
+ )
122
+
123
+ assert_equal(
124
+ {},
125
+ {a: 1}.hmap {|k, v| nil}
126
+ )
127
+
128
+
129
+ assert_equal(
130
+ {
131
+ c: 4,
132
+ b: 6,
133
+ a: 2,
134
+ },
135
+ data.hmap! {|k, v| [k, v * 2]}
136
+ )
137
+ assert_equal(
138
+ {
139
+ c: 4,
140
+ b: 6,
141
+ a: 2,
142
+ },
143
+ data
144
+ )
145
+ end
146
+
147
+
148
+ def test_kmap
149
+ data = {
150
+ 'a' => 1,
151
+ 'b' => 2,
152
+ 'c' => 3,
153
+ }
154
+
155
+ assert_equal(
156
+ data,
157
+ data.kmap(&:to_s)
158
+ )
159
+
160
+ assert_equal(
161
+ {
162
+ 'aa' => 1,
163
+ 'bb' => 2,
164
+ 'cc' => 3,
165
+ },
166
+ data.kmap {|k| k * 2}
167
+ )
168
+
169
+ assert_equal(
170
+ {
171
+ 'aa' => 1,
172
+ 'bb' => 2,
173
+ 'cc' => 3,
174
+ },
175
+ data.kmap {|k, v| k * 2}
176
+ )
177
+
178
+ assert_equal(
179
+ {
180
+ 'aa' => 1,
181
+ 'bb' => 2,
182
+ 'cc' => 3,
183
+ },
184
+ data.kmap! {|k, v| k * 2}
185
+ )
186
+ assert_equal(
187
+ {
188
+ 'aa' => 1,
189
+ 'bb' => 2,
190
+ 'cc' => 3,
191
+ },
192
+ data
193
+ )
194
+ end
195
+
196
+
197
+ def test_vmap
198
+ data = {
199
+ a: 1,
200
+ b: 2,
201
+ c: 3,
202
+ }
203
+
204
+ assert_equal(
205
+ {
206
+ a: 1.0,
207
+ b: 2.0,
208
+ c: 3.0,
209
+ },
210
+ data.vmap(&:to_f)
211
+ )
212
+
213
+ assert_equal(
214
+ {
215
+ a: 2,
216
+ b: 4,
217
+ c: 6,
218
+ },
219
+ data.vmap {|v| v * 2}
220
+ )
221
+
222
+ assert_equal(
223
+ {
224
+ a: 2,
225
+ b: 4,
226
+ c: 6,
227
+ },
228
+ data.vmap {|k, v| v * 2}
229
+ )
230
+
231
+ assert_equal(
232
+ {
233
+ a: :a,
234
+ b: :b,
235
+ c: :c,
236
+ },
237
+ data.vmap {|k, v| k}
238
+ )
239
+
240
+
241
+ assert_equal(
242
+ {
243
+ a: 2,
244
+ b: 4,
245
+ c: 6,
246
+ },
247
+ data.vmap! {|v| v * 2}
248
+ )
249
+ assert_equal(
250
+ {
251
+ a: 2,
252
+ b: 4,
253
+ c: 6,
254
+ },
255
+ data
256
+ )
257
+ end
258
+
259
+
260
+ def test_default_value
261
+ data = Hash.new 123
262
+
263
+ assert_equal(
264
+ 123,
265
+ data.kmap {|v| nil }[:abc]
266
+ )
267
+
268
+ assert_equal(
269
+ 123,
270
+ data.vmap {|v| nil }[:abc]
271
+ )
272
+
273
+ assert_equal(
274
+ 123,
275
+ data.hmap {|v| nil }[:abc]
276
+ )
277
+ end
278
+
279
+ end
metadata CHANGED
@@ -1,73 +1,89 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: hmap
3
- version: !ruby/object:Gem::Version
4
- hash: 27
5
- prerelease:
6
- segments:
7
- - 0
8
- - 1
9
- - 0
10
- version: 0.1.0
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
11
5
  platform: ruby
12
- authors:
13
- - "J. Pablo Fern\xC3\xA1ndez"
6
+ authors:
7
+ - Daniel Pepper
14
8
  autorequire:
15
9
  bindir: bin
16
10
  cert_chain: []
17
-
18
- date: 2011-02-19 00:00:00 +01:00
19
- default_executable:
20
- dependencies: []
21
-
22
- description: A map for hashes that returs a hash
11
+ date: 2018-12-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: byebug
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Improved Hash mapping functions
23
56
  email:
24
- - pupeno@pupeno.com
25
57
  executables: []
26
-
27
58
  extensions: []
28
-
29
59
  extra_rdoc_files: []
30
-
31
- files:
32
- - Gemfile
33
- - Rakefile
34
- - hmap.gemspec
60
+ files:
35
61
  - lib/hmap.rb
36
- - lib/hmap/version.rb
37
- - test/hmap_test.rb
38
- has_rdoc: true
39
- homepage: ""
40
- licenses: []
41
-
62
+ - lib/hmap/hmap.rb
63
+ - test/test_hmap.rb
64
+ homepage: https://github.com/dpep/rb_hmap
65
+ licenses:
66
+ - MIT
67
+ metadata: {}
42
68
  post_install_message:
43
69
  rdoc_options: []
44
-
45
- require_paths:
70
+ require_paths:
46
71
  - lib
47
- required_ruby_version: !ruby/object:Gem::Requirement
48
- none: false
49
- requirements:
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
50
74
  - - ">="
51
- - !ruby/object:Gem::Version
52
- hash: 3
53
- segments:
54
- - 0
55
- version: "0"
56
- required_rubygems_version: !ruby/object:Gem::Requirement
57
- none: false
58
- requirements:
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
59
79
  - - ">="
60
- - !ruby/object:Gem::Version
61
- hash: 3
62
- segments:
63
- - 0
64
- version: "0"
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
65
82
  requirements: []
66
-
67
- rubyforge_project: hmap
68
- rubygems_version: 1.5.2
83
+ rubyforge_project:
84
+ rubygems_version: 2.6.11
69
85
  signing_key:
70
- specification_version: 3
71
- summary: A map for hashes that returns a hash
72
- test_files:
73
- - test/hmap_test.rb
86
+ specification_version: 4
87
+ summary: Hmap
88
+ test_files:
89
+ - test/test_hmap.rb
data/Gemfile DELETED
@@ -1,6 +0,0 @@
1
- # Copyright © 2011, José Pablo Fernández
2
-
3
- source "http://rubygems.org"
4
-
5
- # Specify your gem's dependencies in hmap.gemspec
6
- gemspec
data/Rakefile DELETED
@@ -1,10 +0,0 @@
1
- # Copyright © 2011, José Pablo Fernández
2
-
3
- require "bundler"
4
- Bundler::GemHelper.install_tasks
5
-
6
- require "rake/testtask"
7
- Rake::TestTask.new do |t|
8
- t.test_files = FileList["test/*.rb"]
9
- t.verbose = true
10
- end
@@ -1,23 +0,0 @@
1
- # -*- encoding: utf-8 -*-
2
- # Copyright © 2011, José Pablo Fernández
3
-
4
- $:.push File.expand_path("../lib", __FILE__)
5
- require "hmap/version"
6
-
7
- Gem::Specification.new do |s|
8
- s.name = "hmap"
9
- s.version = Hmap::VERSION
10
- s.platform = Gem::Platform::RUBY
11
- s.authors = ["J. Pablo Fernández"]
12
- s.email = ["pupeno@pupeno.com"]
13
- s.homepage = ""
14
- s.summary = %q{A map for hashes that returns a hash}
15
- s.description = %q{A map for hashes that returs a hash}
16
-
17
- s.rubyforge_project = "hmap"
18
-
19
- s.files = `git ls-files`.split("\n").grep(/^[^\.]/)
20
- s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
21
- s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
22
- s.require_paths = ["lib"]
23
- end
@@ -1,5 +0,0 @@
1
- # Copyright © 2011, José Pablo Fernández
2
-
3
- module Hmap
4
- VERSION = "0.1.0"
5
- end
@@ -1,35 +0,0 @@
1
- # Copyright © 2011, José Pablo Fernández
2
-
3
- require "hmap.rb"
4
- require "test/unit"
5
-
6
- class HashTest < Test::Unit::TestCase
7
- def test_empty
8
- assert_equal({}, {}.hmap { |a, b| [a, b] })
9
- end
10
-
11
- def test_raises_when_totally_invalid
12
- assert_raises RuntimeError do
13
- {:a => :b}.hmap { |a, _| a }
14
- end
15
- end
16
-
17
- def test_raises_when_invalid_array
18
- assert_raises RuntimeError do
19
- {:a => :b}.hmap { |a, _| [a] }
20
- end
21
- assert_raises RuntimeError do
22
- {:a => :b}.hmap { |a, b| [a, b, b] }
23
- end
24
- end
25
-
26
- def test_identity_with_hash
27
- hash = {:a => :b, :c => :d, :e => :f}
28
- assert_equal(hash, hash.hmap { |a, b| {a => b} })
29
- end
30
-
31
- def test_identity_with_array
32
- hash = {:a => :b, :c => :d, :e => :f}
33
- assert_equal(hash, hash.hmap { |a, b| [a, b] })
34
- end
35
- end