functions 0.0.15 → 0.0.16

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2ce638922c8e6bfddfd46e70897d221b1c39b642
4
- data.tar.gz: 1747fa2b179e25fe8e64633bd1f391d4bf7e6942
3
+ metadata.gz: f9f07df0e596091bea7d6f8f57d4c877faf3d9bd
4
+ data.tar.gz: 0642fda991a8aa220b809d22c474f9af47b4ed24
5
5
  SHA512:
6
- metadata.gz: 98543846cf5ec5408dbbc7770d299b86d3343c7a20e51264ef83c3d52202e4e43d644d9e737110841beb9d249a11a2085695b040f84326c6c6db37f4e185e72f
7
- data.tar.gz: db078d333e4b467cf2cf833e9c1eb26e1a7819899ca84d7e6a4b3206e81f0a29fcfc0ff12b61ee045303a5436e8e12546d29e8bf70d70f5f82d410147311d5a9
6
+ metadata.gz: ea82380d54e0fbb0469c88f9d009662ef803e3d841f9638a894a30d964752b7f94eb0b68c7c04f86d965333cb4182786c322a4fec9d744fc47f41ab82786977e
7
+ data.tar.gz: 6c3c534d39be490690e25761d09193355bc02a1496404fa222871eb5f71290b7e878f510839e690fb849eb6a927633e063cd8f5627c9327ddfc9290d9bab18a0
@@ -0,0 +1,19 @@
1
+ require_relative '../lib/functions'
2
+
3
+ folders = {
4
+ 'main[2]' => { 'child[3]' => 'leaf'}
5
+ }
6
+
7
+ def multiply_folder(k,v)
8
+ match = k.match(/(.*)\[(\d+)\]/)
9
+ k, count = match ? [match[1], match[2].to_i] : [k, nil]
10
+ if count
11
+ (1..count).map { |i| ["#{k}_#{i}",v] }
12
+ else
13
+ [[k,v]]
14
+ end
15
+ end
16
+
17
+ multiplied = folders.multi_map_hash_recursive &multiply_folder
18
+
19
+ puts multiplied
@@ -8,8 +8,74 @@ class Hash
8
8
  self.each_with_object({}) { |(k, a), h| b = bs[k]; h[k] = [a, b] if b; h }
9
9
  end
10
10
 
11
- def map_hash
11
+ def map_values
12
12
  Hash[self.map{|k, v| [k, yield(v)] }]
13
13
  end
14
14
 
15
- end
15
+ def map_values_recurse(&b)
16
+ Hash[self.map{|k, v| [k, v.is_a?(Hash) ? v.map_values_recurse(&b) : b.(v)] }]
17
+ end
18
+
19
+ # definition of maarten
20
+ # def map_values &blk
21
+ # res = {}
22
+ # each {|k,v| res[k] = blk.call(v)}
23
+ # res
24
+ # end
25
+
26
+ # TODO discuss if we should not map onto array as values can colide
27
+ def map_keys
28
+ Hash[self.map{|k, v| [yield(k), v] }]
29
+ end
30
+
31
+ def map_keys_recurse(&b)
32
+ Hash[self.map{|k, v| [b.(k), v.is_a?(Hash) ? v.map_keys_recurse(&b) : v ] }]
33
+ end
34
+
35
+ # def map_keys
36
+ # each_with_object({}) { |h, (k,v)| h[yield(k)] = v }
37
+ # end
38
+
39
+ # def map_keys &blk
40
+ # each_with_object({}) {|h, (k,v)| h[blk.(k)] = v}
41
+ # end
42
+
43
+ # definition of maarten
44
+ # def map_keys &blk
45
+ # res = {}
46
+ # each {|k,v| res[blk.call(k)] = v}
47
+ # res
48
+ # end
49
+
50
+ # map a hash into a hash
51
+ def map_hash
52
+ Hash[ self.map{|k, v| yield(k,v) } ]
53
+ end
54
+
55
+ # map a hash into a hash recursively.
56
+ # the mapping function needs to check itself wether or not the value is hash and act appropriately
57
+ def map_recursive(&b)
58
+ Hash[ self.map{|k, v| b.(k, v.is_a?(Hash) ? v.map_recursive(&b) : v) } ]
59
+ end
60
+
61
+ # map a hash into a hash recursively with a seperate key mapping and value mapping function
62
+ def map_keys_and_values(km, vm)
63
+ Hash[ self.map{|k, v| [ km.(k), v.is_a?(Hash) ? v.map_keys_and_values(km, vm) : vm.(v) ] } ]
64
+ end
65
+
66
+ # map a hash into a hash recursively where one key value mapping can be mapped onto multi key value mappings
67
+ def multi_map(&b)
68
+ Hash[ self.map{|k, v| b.(k, ( v.is_a?(Hash) ? v.multi_map(&b) : v) ) }.flatten(1) ]
69
+ end
70
+
71
+ # def map_hash &blk
72
+ # res = {}
73
+ # each do |k,v|
74
+ # kk, vv = blk.call(k,v)
75
+ # res[kk] = vv
76
+ # end
77
+ # res
78
+ # end
79
+
80
+ end
81
+
@@ -1,3 +1,3 @@
1
1
  module Functions
2
- VERSION = "0.0.15"
2
+ VERSION = "0.0.16"
3
3
  end
@@ -57,10 +57,58 @@ describe "enumerable" do
57
57
  ad.zip_hash_inner(cb).should eq({})
58
58
  end
59
59
 
60
+ it "map_values" do
61
+ ab = {a: 1, b: 2}
62
+ ab.map_values { |x| x*2}.should == {a: 2, b: 4}
63
+ ab.map_values { |x| x.even? }.should == {a: false, b: true}
64
+ end
65
+
66
+ it "map_values_recurse" do
67
+ abcde = {a: 1, b: 2, c: { d: 1, e: 0 } }
68
+ abcde.map_values_recurse { |x| x*2}.should == {a: 2, b: 4, c: { d: 2, e: 0}}
69
+ abcde .map_values_recurse { |x| x.even? }.should == {a: false, b: true, c: { d:false, e: true}}
70
+ end
71
+
72
+ it "map_keys" do
73
+ ab = {a: 1, b: 2}
74
+ ab.map_keys { |k| k.to_s }.should == {"a" => 1, "b" => 2}
75
+ ab.map_keys { |k| k.to_s[0].ord }.should == { 97 => 1, 98 => 2 }
76
+ ab.map_keys { |k| k.to_s.length }.should == { 1 => 2 }
77
+ end
78
+
79
+ it "map_keys_recurse" do
80
+ abcde = {a: 1, b: 2, c: { d: 1, e: 0 } }
81
+ abcde.map_keys_recurse { |k| k.to_s }.should == {"a" => 1, "b" => 2, "c" => { "d" => 1, "e" => 0 }}
82
+ end
83
+
60
84
  it "map_hash" do
61
85
  ab = {a: 1, b: 2}
62
- ab.map_hash { |x| x*2}.should == {a: 2, b: 4}
63
- ab.map_hash { |x| x.even? }.should == {a: false, b: true}
86
+ ab.map_hash { |k,v| [k.to_s, v*2] }.should == {"a" => 2, "b" => 4}
87
+ ab.map_hash { |k,v| [k.to_s[0].ord, v.even?] }.should == {97 => false, 98 => true}
88
+ ab.map_hash { |k,v| [k.to_s.length, v.even?] }.should == {1 => true}
89
+ end
90
+
91
+ it "map_recursive" do
92
+ abcde = {a: 1, b: 2, c: { d: 1, e: 0 } }
93
+ abcde.map_recursive { |k,v| v.is_a?(Hash) ? [k.to_s, v] : [k.to_s, v*2] }.should == {"a" => 2, "b" => 4, "c" => { "d" => 2, "e" => 0 }}
94
+ abcde.map_recursive { |k,v| v.is_a?(Hash) ? [k.to_s[0].ord, v] : [k.to_s[0].ord, v.even?] }.should == {97 => false, 98 => true, 99 => { 100 => false, 101 => true }}
95
+ abcde.map_recursive { |k,v| v.is_a?(Hash) ? [k.to_s.length, v] : [k.to_s.length, v.even?] }.should == {1 => true, 1 => { 1=>true } }
96
+ end
97
+
98
+ it "map_keys_and_values" do
99
+
100
+ s = ->(x) { x.to_s }
101
+ s_ord = ->(x) { x.to_s[0].ord }
102
+ s_length = ->(x) { x.to_s.length }
103
+ times_2 = ->(x) { x * 2 }
104
+ even = ->(x) { x.even? }
105
+
106
+ abcde = {a: 1, b: 2, c: { d: 1, e: 0 } }
107
+
108
+ abcde.map_keys_and_values(s, times_2).should == {"a" => 2, "b" => 4, "c" => { "d" => 2, "e" => 0 }}
109
+ abcde.map_keys_and_values(s_ord, even).should == {97 => false, 98 => true, 99 => { 100 => false, 101 => true }}
110
+ abcde.map_keys_and_values(s_length, even).should == {1 => true, 1 => { 1=>true } }
111
+
64
112
  end
65
113
 
66
114
  it "counted_set" do
@@ -73,4 +121,23 @@ describe "enumerable" do
73
121
  %w(some words are longer then others).grouped_by { |x| x.length > 3 }.should eq([%w(some words longer then others),%w(are)])
74
122
  end
75
123
 
124
+ it "multimap" do
125
+
126
+ folders = { 'main[2]' => { 'child[3]' => 'leaf', 'simple' => 'leaf' } }
127
+
128
+ multiply_folder = -> (k,v) do
129
+ match = k.match(/(.*)\[(\d+)\]/)
130
+ k, count = match ? [match[1], match[2].to_i] : [k, nil]
131
+ if count
132
+ (1..count).map { |i| ["#{k}_#{i}",v] }
133
+ else
134
+ [[k,v]]
135
+ end
136
+ end
137
+
138
+ multiplied = folders.multi_map &multiply_folder
139
+ multiplied.length.should eq(2)
140
+ multiplied['main_1'].length.should eq(4)
141
+
142
+ end
76
143
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: functions
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.15
4
+ version: 0.0.16
5
5
  platform: ruby
6
6
  authors:
7
7
  - Koen Handekyn
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-11-05 00:00:00.000000000 Z
11
+ date: 2014-04-08 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: functional programming in ruby
14
14
  email:
@@ -17,12 +17,13 @@ executables: []
17
17
  extensions: []
18
18
  extra_rdoc_files: []
19
19
  files:
20
- - .gitignore
21
- - .rspec
20
+ - ".gitignore"
21
+ - ".rspec"
22
22
  - Gemfile
23
23
  - LICENSE.txt
24
24
  - README.md
25
25
  - Rakefile
26
+ - examples/multi_hash_map_recursive.rb
26
27
  - examples/prelude_lambda.rb
27
28
  - examples/spec/prelude_lambda_spec.rb
28
29
  - functions.gemspec
@@ -54,17 +55,17 @@ require_paths:
54
55
  - lib
55
56
  required_ruby_version: !ruby/object:Gem::Requirement
56
57
  requirements:
57
- - - '>='
58
+ - - ">="
58
59
  - !ruby/object:Gem::Version
59
60
  version: '0'
60
61
  required_rubygems_version: !ruby/object:Gem::Requirement
61
62
  requirements:
62
- - - '>='
63
+ - - ">="
63
64
  - !ruby/object:Gem::Version
64
65
  version: '0'
65
66
  requirements: []
66
67
  rubyforge_project:
67
- rubygems_version: 2.0.3
68
+ rubygems_version: 2.2.1
68
69
  signing_key:
69
70
  specification_version: 4
70
71
  summary: functional programming in ruby