hmap 0.1.0 → 1.0.1
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.
- checksums.yaml +7 -0
- data/CHANGELOG.md +10 -0
- data/Gemfile +1 -4
- data/Gemfile.lock +42 -0
- data/LICENSE.txt +22 -0
- data/README.md +74 -0
- data/hmap.gemspec +12 -19
- data/lib/hmap/hmap.rb +60 -0
- data/lib/hmap/version.rb +1 -3
- data/lib/hmap.rb +3 -18
- metadata +77 -57
- data/Rakefile +0 -10
- data/test/hmap_test.rb +0 -35
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 6ad321ffc1bab03aa6a94f4e528c10b0c2cdc53a803a422f6327d12ab1abe9d4
|
4
|
+
data.tar.gz: 8faaec21ec0526bd3ea5fc3189045ec1cf2adcac7740352691e9b8a0a1cc6923
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e6eec1dd98f07ca4d3f4da63bc1c8a31bf95d0edddc0265b8f259cdd84d05ad904c7b5c2bd2df78c44e8d3957492019f31dba6faa038eeaf6b3e8f4836770b14
|
7
|
+
data.tar.gz: 800788420878ef4950b798bdee053380deeb981638594299ef26bf35b9bbb901fc731c73e734b3292f25f6bdef5ffc9c1f8947ead4ae949817a8f71d23e488af
|
data/CHANGELOG.md
ADDED
data/Gemfile
CHANGED
data/Gemfile.lock
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
hmap (1.0.1)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
byebug (11.1.3)
|
10
|
+
diff-lcs (1.5.1)
|
11
|
+
docile (1.4.0)
|
12
|
+
rspec (3.13.0)
|
13
|
+
rspec-core (~> 3.13.0)
|
14
|
+
rspec-expectations (~> 3.13.0)
|
15
|
+
rspec-mocks (~> 3.13.0)
|
16
|
+
rspec-core (3.13.0)
|
17
|
+
rspec-support (~> 3.13.0)
|
18
|
+
rspec-expectations (3.13.0)
|
19
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
20
|
+
rspec-support (~> 3.13.0)
|
21
|
+
rspec-mocks (3.13.1)
|
22
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
23
|
+
rspec-support (~> 3.13.0)
|
24
|
+
rspec-support (3.13.1)
|
25
|
+
simplecov (0.22.0)
|
26
|
+
docile (~> 1.1)
|
27
|
+
simplecov-html (~> 0.11)
|
28
|
+
simplecov_json_formatter (~> 0.1)
|
29
|
+
simplecov-html (0.12.3)
|
30
|
+
simplecov_json_formatter (0.1.4)
|
31
|
+
|
32
|
+
PLATFORMS
|
33
|
+
ruby
|
34
|
+
|
35
|
+
DEPENDENCIES
|
36
|
+
byebug (>= 11)
|
37
|
+
hmap!
|
38
|
+
rspec (>= 3.10)
|
39
|
+
simplecov (>= 0.22)
|
40
|
+
|
41
|
+
BUNDLED WITH
|
42
|
+
2.5.15
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Daniel Pepper
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
22
|
+
|
data/README.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
Hmap
|
2
|
+
======
|
3
|
+
|
4
|
+
Improved Hash mapping functions
|
5
|
+
|
6
|
+
### Install
|
7
|
+
```gem install hmap```
|
8
|
+
|
9
|
+
|
10
|
+
### Usage
|
11
|
+
#### Hash.map
|
12
|
+
Create a Hash using an Enumerable by mapping it's items into corresponding hash values
|
13
|
+
```ruby
|
14
|
+
Hash.map [:a, :bb, :ccc] {|v| v.length }
|
15
|
+
=> { a: 1, bb: 2, ccc: 3 }
|
16
|
+
```
|
17
|
+
|
18
|
+
#### hmap
|
19
|
+
Map an Enumerable into a Hash, like Hash[obj.map ... ]
|
20
|
+
```ruby
|
21
|
+
Hash.hmap [ 1, 2, 3 ] { |v| [ v, v * 2 ] }
|
22
|
+
=> { 1 => 2, 2 => 4, 3 => 6 }
|
23
|
+
|
24
|
+
{
|
25
|
+
'a' => 1,
|
26
|
+
'b' => 2,
|
27
|
+
'c' => 3,
|
28
|
+
}.hmap {|k, v| [ k * v, v ** 2 ] }
|
29
|
+
=> { 'a' => 1, 'bb' => 4, 'ccc' => 9 }
|
30
|
+
```
|
31
|
+
|
32
|
+
#### vmap
|
33
|
+
Transform the values of a Hash
|
34
|
+
```ruby
|
35
|
+
{
|
36
|
+
a: 1,
|
37
|
+
b: 2,
|
38
|
+
c: 3,
|
39
|
+
}.vmap {|v| v * 2 }
|
40
|
+
=> { a: 2, b: 4, c: 6 }
|
41
|
+
|
42
|
+
{
|
43
|
+
a: 1,
|
44
|
+
b: 2,
|
45
|
+
c: 3,
|
46
|
+
}.vmap &:to_s
|
47
|
+
=> { a: '2', b: '4', c: '6' }
|
48
|
+
```
|
49
|
+
|
50
|
+
#### kmap
|
51
|
+
Transform the keys of a Hash
|
52
|
+
```ruby
|
53
|
+
{
|
54
|
+
'a' => 1,
|
55
|
+
'b' => 2,
|
56
|
+
'c' => 3,
|
57
|
+
}.kmap {|k, v| k * v }
|
58
|
+
=> { 'a' => 1, 'bb' => 2, 'ccc' => 3 }
|
59
|
+
|
60
|
+
{
|
61
|
+
'a' => 1,
|
62
|
+
'b' => 2,
|
63
|
+
'c' => 3,
|
64
|
+
}.kmap &:to_sym
|
65
|
+
=> { a: 1, b: 2, c: 3 }
|
66
|
+
```
|
67
|
+
|
68
|
+
|
69
|
+
##
|
70
|
+
Thanks to [J. Pablo Fernández](https://github.com/pupeno/hmap)
|
71
|
+
|
72
|
+
----
|
73
|
+

|
74
|
+
[](https://codecov.io/gh/dpep/rb_hmap)
|
data/hmap.gemspec
CHANGED
@@ -1,23 +1,16 @@
|
|
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
1
|
Gem::Specification.new do |s|
|
8
|
-
s.
|
9
|
-
s.
|
10
|
-
s.
|
11
|
-
s.
|
12
|
-
s.
|
13
|
-
s.
|
14
|
-
s.summary =
|
15
|
-
s.
|
2
|
+
s.authors = ["Daniel Pepper"]
|
3
|
+
s.description = "Improved Hash mapping functions"
|
4
|
+
s.files = `git ls-files * ":!:spec"`.split("\n")
|
5
|
+
s.homepage = "https://github.com/dpep/rb_hmap"
|
6
|
+
s.license = "MIT"
|
7
|
+
s.name = File.basename(__FILE__, ".gemspec")
|
8
|
+
s.summary = "Hmap"
|
9
|
+
s.version = "1.0.1"
|
16
10
|
|
17
|
-
s.
|
11
|
+
s.required_ruby_version = ">= 3"
|
18
12
|
|
19
|
-
s.
|
20
|
-
s.
|
21
|
-
s.
|
22
|
-
s.require_paths = ["lib"]
|
13
|
+
s.add_development_dependency "byebug", ">= 11"
|
14
|
+
s.add_development_dependency "rspec", ">= 3.10"
|
15
|
+
s.add_development_dependency "simplecov", ">= 0.22"
|
23
16
|
end
|
data/lib/hmap/hmap.rb
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
module Hmap
|
2
|
+
# create Hash from given keys and mapped values
|
3
|
+
def map(obj, &block)
|
4
|
+
res = obj.map do |*args|
|
5
|
+
block.call(*args)
|
6
|
+
end
|
7
|
+
|
8
|
+
Hash[obj.zip res]
|
9
|
+
end
|
10
|
+
|
11
|
+
# map an Enumerable into a Hash, like Hash[obj.map ... ]
|
12
|
+
def hmap(obj, &block)
|
13
|
+
Hash[
|
14
|
+
obj.map do |*args|
|
15
|
+
block.call(*args)
|
16
|
+
end.compact
|
17
|
+
]
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.extended(base)
|
21
|
+
base.include(InstanceMethods)
|
22
|
+
end
|
23
|
+
|
24
|
+
module InstanceMethods
|
25
|
+
# map the block's results back to a hash
|
26
|
+
def hmap(&block)
|
27
|
+
clone.hmap!(&block)
|
28
|
+
end
|
29
|
+
|
30
|
+
def hmap!(&block)
|
31
|
+
data = Hash.hmap(self, &block)
|
32
|
+
clear
|
33
|
+
merge! data
|
34
|
+
end
|
35
|
+
|
36
|
+
# map keys, but preserve associated values
|
37
|
+
# ie. http://apidock.com/rails/v4.2.7/Hash/transform_keys
|
38
|
+
def kmap(&block)
|
39
|
+
clone.kmap!(&block)
|
40
|
+
end
|
41
|
+
|
42
|
+
def kmap!(&block)
|
43
|
+
hmap! do |k, v|
|
44
|
+
[ block.arity <= 1 ? block.call(k) : block.call(k, v), v ]
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
# map values, but preserve associated keys
|
49
|
+
# ie. http://apidock.com/rails/v4.2.7/Hash/transform_values
|
50
|
+
def vmap(&block)
|
51
|
+
clone.vmap!(&block)
|
52
|
+
end
|
53
|
+
|
54
|
+
def vmap!(&block)
|
55
|
+
each do |k, v|
|
56
|
+
self[k] = block.arity <= 1 ? block.call(v) : block.call(k, v)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
data/lib/hmap/version.rb
CHANGED
data/lib/hmap.rb
CHANGED
@@ -1,19 +1,4 @@
|
|
1
|
-
|
1
|
+
require_relative 'hmap/hmap'
|
2
|
+
require_relative 'hmap/version'
|
2
3
|
|
3
|
-
|
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
|
19
|
-
end
|
4
|
+
Hash.extend(Hmap)
|
metadata
CHANGED
@@ -1,73 +1,93 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: hmap
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
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.1
|
11
5
|
platform: ruby
|
12
|
-
authors:
|
13
|
-
-
|
14
|
-
autorequire:
|
6
|
+
authors:
|
7
|
+
- Daniel Pepper
|
8
|
+
autorequire:
|
15
9
|
bindir: bin
|
16
10
|
cert_chain: []
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
-
|
11
|
+
date: 2024-07-12 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: '11'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '11'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '3.10'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '3.10'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: simplecov
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0.22'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0.22'
|
55
|
+
description: Improved Hash mapping functions
|
56
|
+
email:
|
25
57
|
executables: []
|
26
|
-
|
27
58
|
extensions: []
|
28
|
-
|
29
59
|
extra_rdoc_files: []
|
30
|
-
|
31
|
-
|
60
|
+
files:
|
61
|
+
- CHANGELOG.md
|
32
62
|
- Gemfile
|
33
|
-
-
|
63
|
+
- Gemfile.lock
|
64
|
+
- LICENSE.txt
|
65
|
+
- README.md
|
34
66
|
- hmap.gemspec
|
35
67
|
- lib/hmap.rb
|
68
|
+
- lib/hmap/hmap.rb
|
36
69
|
- lib/hmap/version.rb
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
post_install_message:
|
70
|
+
homepage: https://github.com/dpep/rb_hmap
|
71
|
+
licenses:
|
72
|
+
- MIT
|
73
|
+
metadata: {}
|
74
|
+
post_install_message:
|
43
75
|
rdoc_options: []
|
44
|
-
|
45
|
-
require_paths:
|
76
|
+
require_paths:
|
46
77
|
- lib
|
47
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
-
|
49
|
-
requirements:
|
78
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
50
80
|
- - ">="
|
51
|
-
- !ruby/object:Gem::Version
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
version: "0"
|
56
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
-
none: false
|
58
|
-
requirements:
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '3'
|
83
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
59
85
|
- - ">="
|
60
|
-
- !ruby/object:Gem::Version
|
61
|
-
|
62
|
-
segments:
|
63
|
-
- 0
|
64
|
-
version: "0"
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
65
88
|
requirements: []
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
summary: A map for hashes that returns a hash
|
72
|
-
test_files:
|
73
|
-
- test/hmap_test.rb
|
89
|
+
rubygems_version: 3.5.15
|
90
|
+
signing_key:
|
91
|
+
specification_version: 4
|
92
|
+
summary: Hmap
|
93
|
+
test_files: []
|
data/Rakefile
DELETED
data/test/hmap_test.rb
DELETED
@@ -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
|