ramda-ruby 0.6.0 → 0.7.0

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: 3be47564e590c02c9edbfe24ce475676fc9e85b3
4
- data.tar.gz: e27d96aa0d83e50b482cb2ada7d09c60dbbf2152
3
+ metadata.gz: a23c9651684d48d741ff366c825cc13eb8b88b03
4
+ data.tar.gz: f784df378ef790ed8ac918986294b983500c2c8d
5
5
  SHA512:
6
- metadata.gz: 610df76cc6e853a7850ff2f7ef3a886b864ad93f807fbd032353d073c7eba6e361ad6f3790c6b234ac6797bf4f87aec643ab8a908ab6058d40f8529c1662d98a
7
- data.tar.gz: 726d0b3245a24232e8949218e15ad50cff16356a35a675d1b89721f31d558d35bca5048da75840b182fa156f24aa4de40625c424d792fb23690d352c8752ba47
6
+ metadata.gz: 5578f41adcd7d4978b8c3a3be2d80e4ab2f61d9e45e0f6ac46fa1b30354a53475fef02e18c06c32816f5bf8384959b6a9a1147b6df51b67519d3179f63de678b
7
+ data.tar.gz: 356cfffa686a4e1853353600e72076e25073262442497695a518be6699f9b91c344dc3a898dc22c1ad14a55abd0db22be1779c29e2ad7af626b92fbd3658a265
data/CHANGELOG.md CHANGED
@@ -1,7 +1,20 @@
1
1
  Not Released
2
2
  ---------------
3
3
 
4
+ Release 0.7.0
5
+ ---------------
6
+
7
+ Added:
8
+
9
+ * apply
10
+ * has
11
+ * has_in
12
+ * lift
13
+ * lift_n
14
+ * path_eq
15
+
4
16
  Release 0.6.0
17
+ ---------------
5
18
 
6
19
  Added:
7
20
 
data/ROADMAP.md CHANGED
@@ -2,15 +2,6 @@
2
2
 
3
3
  * Find out suitable documentation format
4
4
 
5
- ### release 0.7.0
6
-
7
- * apply
8
- * has
9
- * has_in
10
- * lift
11
- * lift_n
12
- * path_eq
13
-
14
5
  ### release 0.8.0
15
6
 
16
7
  * assoc
data/docs/FUNCTIONS.md CHANGED
@@ -14,6 +14,7 @@
14
14
  * any_pass
15
15
  * ap
16
16
  * append
17
+ * apply
17
18
  * assoc
18
19
  * binary
19
20
  * bind
@@ -53,6 +54,8 @@
53
54
  * group_by
54
55
  * gt
55
56
  * gte
57
+ * has
58
+ * has_in
56
59
  * head
57
60
  * identity
58
61
  * if_else
@@ -70,6 +73,8 @@
70
73
  * last
71
74
  * last_index_of - returns nil if index doesn't exist
72
75
  * length
76
+ * lift
77
+ * lift_n
73
78
  * lt
74
79
  * lte
75
80
  * map
@@ -89,6 +94,7 @@
89
94
  * or
90
95
  * partition
91
96
  * path
97
+ * path_eq
92
98
  * pick
93
99
  * pick_all
94
100
  * pipe
data/lib/ramda.rb CHANGED
@@ -21,6 +21,7 @@ module Ramda
21
21
  :__,
22
22
  :always,
23
23
  :ap,
24
+ :apply,
24
25
  :binary,
25
26
  :bind,
26
27
  :comparator,
@@ -35,6 +36,8 @@ module Ramda
35
36
  :identity,
36
37
  :invoker,
37
38
  :juxt,
39
+ :lift,
40
+ :lift_n,
38
41
  :memoize,
39
42
  :n_ary,
40
43
  :of,
@@ -122,6 +125,8 @@ module Ramda
122
125
  :clone,
123
126
  :dissoc,
124
127
  :eq_props,
128
+ :has,
129
+ :has_in,
125
130
  :keys,
126
131
  # :keys_in,
127
132
  :merge,
@@ -152,6 +157,7 @@ module Ramda
152
157
  :lte,
153
158
  :max,
154
159
  :min,
160
+ :path_eq,
155
161
  :prop_eq,
156
162
  :sort_by,
157
163
  :union,
@@ -58,14 +58,24 @@ module Ramda
58
58
  # [a -> b] -> [a] -> [b]
59
59
  # Apply f => f (a -> b) -> f a -> f b
60
60
  #
61
- curried_method(:ap) do |fns, xs|
62
- if xs.respond_to?(:ap)
63
- xs.ap(fns)
61
+ curried_method(:ap) do |apply_f, apply_x|
62
+ if apply_x.respond_to?(:ap)
63
+ apply_x.ap(apply_f)
64
64
  else
65
- fns.flat_map { |fn| xs.map(&fn) }
65
+ apply_f.flat_map { |fn| apply_x.map(&fn) }
66
66
  end
67
67
  end
68
68
 
69
+ # Applies function fn to the argument list args. This is useful
70
+ # for creating a fixed-arity function from a variadic function.
71
+ # fn should be a bound function if context is significant.
72
+ #
73
+ # (*... -> a) -> [*] -> a
74
+ #
75
+ curried_method(:apply) do |fn, xs|
76
+ fn.call(*xs)
77
+ end
78
+
69
79
  # Wraps a function of any arity (including nullary) in a function that
70
80
  # accepts exactly 2 parameters. Any extraneous parameters will not be
71
81
  # passed to the supplied function.
@@ -156,7 +166,7 @@ module Ramda
156
166
  # (* -> a) -> (* -> a)
157
167
  #
158
168
  curried_method(:curry) do |fn|
159
- curried_method_body(fn.arity, &fn)
169
+ curried_method_body(:curry, fn.arity, &fn)
160
170
  end
161
171
 
162
172
  # Returns a curried equivalent of the provided function, with the
@@ -185,7 +195,7 @@ module Ramda
185
195
  # Number -> (* -> a) -> (* -> a)
186
196
  #
187
197
  curried_method(:curry_n) do |arity, fn|
188
- curried_method_body(arity, &fn)
198
+ curried_method_body(:curry_n, arity, &fn)
189
199
  end
190
200
 
191
201
  # Returns the empty value of its argument's type. Ramda defines the empty
@@ -245,6 +255,27 @@ module Ramda
245
255
  fns.map { |fn| fn.call(a, *bs) }
246
256
  end
247
257
 
258
+ # "lifts" a function of arity > 1 so that it may "map over" a list,
259
+ # Function or other object that satisfies the FantasyLand Apply spec.
260
+ #
261
+ # (*... -> *) -> ([*]... -> [*])
262
+ #
263
+ # https://stackoverflow.com/questions/36558598/cant-wrap-my-head-around-lift-in-ramda-js
264
+ curried_method(:lift) do |fn, a, *xs|
265
+ # ([a] + xs).reduce([curry(fn)], &ap)
266
+ lift_n(fn.arity, fn, a, *xs)
267
+ end
268
+
269
+ # "lifts" a function to be the specified arity, so that it may
270
+ # "map over" that many lists, Functions or other objects that
271
+ # satisfy the FantasyLand Apply spec.
272
+ #
273
+ # Number -> (*... -> *) -> ([*]... -> [*])
274
+ #
275
+ curried_method(:lift_n) do |arity, fn, a, *xs|
276
+ ([a] + xs).reduce([curry_n(arity, fn)], &ap)
277
+ end
278
+
248
279
  # Creates a new function that, when invoked, caches the result of calling
249
280
  # fn for a given argument set and returns the result. Subsequent calls to
250
281
  # the memoized fn with the same argument set will not result in an
@@ -5,18 +5,24 @@ module Ramda
5
5
  # Curried Method
6
6
  module CurriedMethod
7
7
  def curried_method(name, &block)
8
- define_singleton_method(name, &curried_method_body(block.arity, &block))
8
+ define_singleton_method(name, &curried_method_body(name, block.arity, &block))
9
9
  end
10
10
 
11
- def curried_method_body(arity, &block)
11
+ # rubocop:disable Metrics/MethodLength
12
+ def curried_method_body(name, arity, &block)
12
13
  Ramda::Internal::FunctionWithArity.new.call(arity) do |*args|
13
- if args.include?(Ramda.__)
14
- replace_placeholder(args, &block).curry
15
- else
16
- args.empty? ? block : yield(*args)
14
+ begin
15
+ if args.include?(Ramda.__)
16
+ replace_placeholder(args, &block).curry
17
+ else
18
+ args.empty? ? block : yield(*args)
19
+ end
20
+ rescue StandardError => e
21
+ raise e, [name, e.exception].join(' -> '), e.backtrace
17
22
  end
18
23
  end.curry
19
24
  end
25
+ # rubocop:enable Metrics/MethodLength
20
26
 
21
27
  def replace_placeholder(basic_args)
22
28
  Ramda::Internal::FunctionWithArity.new.call(basic_args.count(Ramda.__)) do |*new_args|
data/lib/ramda/list.rb CHANGED
@@ -295,7 +295,7 @@ module Ramda
295
295
  # Filterable f => (a -> Boolean) -> f a -> [f a, f a]
296
296
  #
297
297
  curried_method(:partition) do |fn, xs|
298
- R.juxt([filter, reject]).call(fn, xs)
298
+ ::Ramda.juxt([filter, reject]).call(fn, xs)
299
299
  end
300
300
 
301
301
  # Returns a new list by plucking the same named property off all objects
data/lib/ramda/object.rb CHANGED
@@ -39,6 +39,23 @@ module Ramda
39
39
  clone(obj).tap { |o| o.delete(prop) }
40
40
  end
41
41
 
42
+ # Returns whether or not an object has an own property with the specified name
43
+ #
44
+ # s -> {s: x} -> Boolean
45
+ #
46
+ curried_method(:has) do |key, obj|
47
+ obj.key?(key)
48
+ end
49
+
50
+ # Returns whether or not an object or its prototype chain has a property
51
+ # with the specified name
52
+ #
53
+ # s -> {s: x} -> Boolean
54
+ #
55
+ curried_method(:has_in) do |key, obj|
56
+ obj.respond_to?(key)
57
+ end
58
+
42
59
  # Reports whether two objects have the same value, in R.equals terms,
43
60
  # for the specified property. Useful as a curried predicate.
44
61
  #
@@ -119,6 +119,18 @@ module Ramda
119
119
  [a, b].min
120
120
  end
121
121
 
122
+ # Determines whether a nested path on an object has a specific value,
123
+ # in R.equals terms. Most likely used to filter a list.
124
+ #
125
+ # [Idx] -> a -> {a} -> Boolean
126
+ # Idx = String | Int
127
+ #
128
+ curried_method(:path_eq) do |path, val, obj|
129
+ path.reduce(obj) do |acc, key|
130
+ acc.fetch(key, nil) if acc.is_a?(Hash) || acc.is_a?(Array)
131
+ end == val
132
+ end
133
+
122
134
  # Returns true if the specified object property is equal, in R.equals terms,
123
135
  # to the given value; false otherwise.
124
136
  #
data/lib/ramda/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Ramda
2
- VERSION = '0.6.0'.freeze
2
+ VERSION = '0.7.0'.freeze
3
3
  end
@@ -43,6 +43,26 @@ describe Ramda::Function do
43
43
  expect(r.ap([R.concat('tasty '), R.to_upper], ['pizza', 'salad']))
44
44
  .to eq(['tasty pizza', 'tasty salad', 'PIZZA', 'SALAD'])
45
45
  end
46
+
47
+ xit 'with monads' do
48
+ res = r.ap(r.ap([R.multiply], M::Maybe(10)), M::Maybe(20))
49
+ expect(res).to eq(M::Maybe::Some.new(200))
50
+ end
51
+ end
52
+
53
+ context '#apply' do
54
+ def max(*args)
55
+ args.max
56
+ end
57
+ it 'from docs' do
58
+ nums = [1, 2, 3, -99, 42, 6, 7]
59
+ expect(r.apply(method(:max), nums)).to be(42)
60
+ end
61
+
62
+ it 'is curried' do
63
+ nums = [1, 2, 3, -99, 42, 6, 7]
64
+ expect(r.apply.call(method(:max)).call(nums)).to be(42)
65
+ end
46
66
  end
47
67
 
48
68
  context '#binary' do
@@ -235,6 +255,61 @@ describe Ramda::Function do
235
255
  end
236
256
  end
237
257
 
258
+ context '#lift' do
259
+ def madd3
260
+ r.lift(->(a, b, c) { a + b + c })
261
+ end
262
+
263
+ def madd4
264
+ r.lift(->(a, b, c, d) { a + b + c + d })
265
+ end
266
+
267
+ def madd5
268
+ r.lift(->(a, b, c, d, e) { a + b + c + d + e })
269
+ end
270
+
271
+ it 'from docs' do
272
+ expect(madd3.call([1, 2, 3], [1, 2, 3], [1])).to eq([3, 4, 5, 4, 5, 6, 5, 6, 7])
273
+ expect(madd5.call([1, 2], [3], [4, 5], [6], [7, 8])).to eq([21, 22, 22, 23, 22, 23, 23, 24])
274
+ end
275
+
276
+ it 'can lift functions with any arity' do
277
+ expect(madd3.call([1, 10], [2], [3])).to eq([6, 15])
278
+ expect(madd4.call([1, 10], [2], [3], [40])).to eq([46, 55])
279
+ expect(madd5.call([1, 10], [2], [3], [40], [500, 1000])).to eq([546, 1046, 555, 1055])
280
+ end
281
+
282
+ it 'example with ap' do
283
+ madd3 = ->(a, b, c) { a + b + c }
284
+
285
+ expect(
286
+ R.ap(
287
+ R.ap(
288
+ R.ap(
289
+ [R.curry(madd3)],
290
+ [1, 2, 3]
291
+ ),
292
+ [1, 2, 3]
293
+ ),
294
+ [1]
295
+ )
296
+ ).to eq([3, 4, 5, 4, 5, 6, 5, 6, 7])
297
+ end
298
+
299
+ xit 'with monads' do
300
+ addm = r.lift(R.add)
301
+ expect(addm.call(M::Maybe(3), M::Maybe(5))).to eq(M::Maybe(8))
302
+ # expect(addM.call(M::Maybe(3), M::Maybe(nil))).to eq(M::Maybe::None.new)
303
+ end
304
+ end
305
+
306
+ context '#lift_n' do
307
+ it 'from docs' do
308
+ madd3 = r.lift_n(3, ->(*args) { R.sum(args) })
309
+ expect(madd3.call([1, 2, 3], [1, 2, 3], [1])).to eq([3, 4, 5, 4, 5, 6, 5, 6, 7])
310
+ end
311
+ end
312
+
238
313
  context '#memoize' do
239
314
  it 'from docs' do
240
315
  count = 0
@@ -30,5 +30,14 @@ describe Ramda::Internal::CurriedMethod do
30
30
  # expect(instance.g(R.__, 2).call(1, 3)).to eq(6)
31
31
  # expect(instance.g(R.__, 2).call(R.__, 3).call(1)).to eq(6)
32
32
  end
33
+
34
+ it 'exception handler' do
35
+ instance.curried_method(:g) do |a, b, c|
36
+ a + b + c
37
+ end
38
+
39
+ expect { instance.g(1, '', 2) }
40
+ .to raise_error(/g -> String can't be coerced/)
41
+ end
33
42
  end
34
43
  end
@@ -24,6 +24,32 @@ describe Ramda::Object do
24
24
  end
25
25
  end
26
26
 
27
+ context '#has' do
28
+ it 'from docs' do
29
+ has_name = R.has(:name)
30
+ expect(has_name.call(name: 'alice')).to be_truthy
31
+ expect(has_name.call(name: 'bob')).to be_truthy
32
+ expect(has_name.call({})).to be_falsey
33
+ end
34
+
35
+ it 'with placeholder' do
36
+ point = { x: 0, y: 0 }
37
+ point_has = R.has(R.__, point)
38
+ expect(point_has.call(:x)).to be_truthy
39
+ expect(point_has.call(:y)).to be_truthy
40
+ expect(point_has.call(:z)).to be_falsey
41
+ end
42
+ end
43
+
44
+ context '#has_in' do
45
+ it 'from docs' do
46
+ square = Struct.new(:width, :area).new(2, 2)
47
+ expect(r.has_in(:width, square)).to be_truthy
48
+ expect(r.has_in(:area, square)).to be_truthy
49
+ expect(r.has_in(:duration, square)).to be_falsey
50
+ end
51
+ end
52
+
27
53
  context '#eq_props' do
28
54
  it 'from docs' do
29
55
  o1 = { a: 1, b: 2, c: 3, d: 4 }
@@ -141,6 +141,26 @@ describe Ramda::Relation do
141
141
  end
142
142
  end
143
143
 
144
+ context '#path_eq' do
145
+ it 'with hash' do
146
+ user1 = { address: { zipCode: 90_210 } }
147
+ user2 = { address: { zipCode: 55_555 } }
148
+ user3 = { name: 'Bob' }
149
+ users = [user1, user2, user3]
150
+ is_famous = R.path_eq([:address, :zipCode], 90_210)
151
+ expect(R.filter(is_famous, users)).to eq([user1])
152
+ end
153
+
154
+ it 'with array' do
155
+ col1 = [[11_111], [22_222], [33_333]]
156
+ col2 = [[44_444], [55_555], [66_666]]
157
+ col3 = [[77_777], [88_888], [99_999]]
158
+ cols = [col1, col2, col3]
159
+ predicate = R.path_eq([1, 0], 55_555)
160
+ expect(R.filter(predicate, cols)).to eq([col2])
161
+ end
162
+ end
163
+
144
164
  context '#sort_by' do
145
165
  it 'from docs 1' do
146
166
  sort_by_first_item = r.sort_by(R.prop(0))
data/spec/ramda_spec.rb CHANGED
@@ -23,6 +23,7 @@ describe Ramda do
23
23
  r(:any_pass)
24
24
  r(:ap)
25
25
  r(:append)
26
+ r(:apply)
26
27
  r(:assoc)
27
28
  r(:binary)
28
29
  r(:bind)
@@ -62,6 +63,8 @@ describe Ramda do
62
63
  r(:group_by)
63
64
  r(:gt)
64
65
  r(:gte)
66
+ r(:has)
67
+ r(:has_in)
65
68
  r(:head)
66
69
  r(:identity)
67
70
  r(:if_else)
@@ -79,6 +82,8 @@ describe Ramda do
79
82
  r(:last)
80
83
  r(:last_index_of)
81
84
  r(:length)
85
+ r(:lift)
86
+ r(:lift_n)
82
87
  r(:lt)
83
88
  r(:lte)
84
89
  r(:map)
@@ -98,6 +103,7 @@ describe Ramda do
98
103
  r(:or)
99
104
  r(:partition)
100
105
  r(:path)
106
+ r(:path_eq)
101
107
  r(:pick)
102
108
  r(:pick_all)
103
109
  r(:pipe)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ramda-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vadim Lazebny
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-06-21 00:00:00.000000000 Z
11
+ date: 2017-06-23 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Ruby version of Ramda Js library.
14
14
  email: