ramda-ruby 0.14.0 → 0.15.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: a9c61500382b8a30650338a2359340adaa39ed58
4
- data.tar.gz: b80d5832f21113ddb52436deecdde3971a44f2b4
3
+ metadata.gz: 6f28ea9a5acc282047805422a96e187bd6b97791
4
+ data.tar.gz: fde8d4617b4d1d43ba9ac481505a046abbe5c6b0
5
5
  SHA512:
6
- metadata.gz: 11b0927aa3a7d6170bebbf1f5e337d8df033701fa71261c01a55d5e9cbfb31cc72c2a78c7019dcdad7e503368a37165a1bae2cb15681a7f16027ed37f4347fe1
7
- data.tar.gz: ef83e01457589590d595e90d78c1a67b283489fd33a4532c21d10f497471994d7660d24868b604767d2cecd5a3b7b65add056cf57d80111448e2314e95d00389
6
+ metadata.gz: 61fb7fd43a5912c651d4f26441c2267530cf55f1b6473b91693fc8f7ba99eafc01cb739006d6d89c59cc1278e02d1464e5b95603e4c99c9e26af4bd1975af834
7
+ data.tar.gz: 208dbb9efa07a6c5b6a00609c410fb81ad98245a5b7b1158b09579cb6531e13acb6d5e30752227153fc28ff4ecf53410b052446cf330367a2ec63a5126d0e5ad
data/CHANGELOG.md CHANGED
@@ -1,6 +1,19 @@
1
1
  Not Released
2
2
  ---------------
3
3
 
4
+ Release 0.15.0
5
+ ---------------
6
+
7
+ Added:
8
+
9
+ * [add_index](http://ramdajs.com/docs/#addIndex)
10
+ * [identical](http://ramdajs.com/docs/#identical)
11
+ * [reduced](http://ramdajs.com/docs/#reduced)
12
+
13
+
14
+ Release 0.14.0
15
+ ---------------
16
+
4
17
  Added:
5
18
 
6
19
  * (pending) uncurry_n
data/README.md CHANGED
@@ -4,7 +4,7 @@ Ramda Ruby
4
4
  This is a ruby version of [Ramda Js](http://ramdajs.com) library.
5
5
 
6
6
  [![Gem Version](https://badge.fury.io/rb/ramda-ruby.svg)](http://badge.fury.io/rb/ramda-ruby)
7
- [![Functions](https://img.shields.io/badge/Functions-184-green.svg)](docs/FUNCTIONS.md)
7
+ [![Functions](https://img.shields.io/badge/Functions-187-green.svg)](docs/FUNCTIONS.md)
8
8
  [![Travis badge](https://travis-ci.org/lazebny/ramda-ruby.svg?branch=master)](https://travis-ci.org/lazebny/ramda-ruby)
9
9
  [![AppVeyor status](https://ci.appveyor.com/api/projects/status/ponccdax7aj4ufw2?svg=true)](https://ci.appveyor.com/project/lazebny/ramda-ruby)
10
10
  [![Coverage Status](https://coveralls.io/repos/lazebny/ramda-ruby/badge.png)](https://coveralls.io/r/lazebny/ramda-ruby)
data/ROADMAP.md CHANGED
@@ -1,15 +1,3 @@
1
- Release 0.13.0
2
- ---------------
3
-
4
- Release 0.14.0
5
- ---------------
6
-
7
- Releaes 0.15.0
8
- ---------------
9
- * add_index
10
- * identical
11
- * reduced
12
-
13
1
  Release 0.16.0
14
2
  ---------------
15
3
  * compose_k
data/docs/FUNCTIONS.md CHANGED
@@ -14,6 +14,7 @@ Function
14
14
  * [F](http://ramdajs.com/docs/#F)
15
15
  * [T](http://ramdajs.com/docs/#T)
16
16
  * [`__`](http://ramdajs.com/docs/) - first function call should have all arguments `g(__2, __)(1, 3)`, this means that it doesn't support `g(__, 2)(1)(3)`, `g(__, 2)(1, 3)` and `g(__, 2)(__, 3)(1)`
17
+ * [add_index](http://ramdajs.com/docs/#addIndex)
17
18
  * [always](http://ramdajs.com/docs/#always)
18
19
  * [ap](http://ramdajs.com/docs/#ap)
19
20
  * [apply](http://ramdajs.com/docs/#apply)
@@ -93,6 +94,7 @@ List
93
94
  * [range](http://ramdajs.com/docs/#range)
94
95
  * [reduce](http://ramdajs.com/docs/#reduce)
95
96
  * [reduce_right](http://ramdajs.com/docs/#reduceRight)
97
+ * [reduced](http://ramdajs.com/docs/#reduced)
96
98
  * [reject](http://ramdajs.com/docs/#reject)
97
99
  * [remove](http://ramdajs.com/docs/#remove)
98
100
  * [repeat](http://ramdajs.com/docs/#repeat)
@@ -196,6 +198,7 @@ Relation
196
198
  * [equals](http://ramdajs.com/docs/#equals)
197
199
  * [gt](http://ramdajs.com/docs/#gt)
198
200
  * [gte](http://ramdajs.com/docs/#gte)
201
+ * [identical](http://ramdajs.com/docs/#identical)
199
202
  * [intersection](http://ramdajs.com/docs/#intersection)
200
203
  * [lt](http://ramdajs.com/docs/#lt)
201
204
  * [lte](http://ramdajs.com/docs/#lte)
@@ -11,6 +11,29 @@ module Ramda
11
11
  extend ::Ramda::Internal::Dispatchable
12
12
  extend ::Ramda::Internal::ClassWhichRespondTo
13
13
 
14
+ # Creates a new list iteration function from an existing one by
15
+ # adding two new parameters to its callback function:
16
+ # the current index, and the entire list.
17
+ #
18
+ # This would turn, for instance, R.map function into one that
19
+ # more closely resembles Array.prototype.map.
20
+ # Note that this will only work for functions in which the
21
+ # iteration callback function is the first parameter, and where
22
+ # the list is the last parameter. (This latter might be unimportant
23
+ # if the list parameter is not used.)
24
+ #
25
+ # ((a ... -> b) ... -> [a] -> *) -> (a ..., Int, [a] -> b) ... -> [a] -> *)
26
+ #
27
+ curried_method(:add_index) do |f|
28
+ curried_method_body(:add_index, f.arity) do |*args|
29
+ idx = -1
30
+ orig_reducer = args[0]
31
+ xs = args[-1]
32
+ new_reducer = ->(*reducer_args) { orig_reducer.call(*reducer_args, idx += 1, xs) }
33
+ f.call(new_reducer, *args[1..-1])
34
+ end
35
+ end
36
+
14
37
  # Returns a function that always returns the given value. Note that
15
38
  # for non-primitives the value returned is a reference to the original
16
39
  # value.
@@ -0,0 +1,16 @@
1
+ module Ramda
2
+ module Internal
3
+ module Transducers
4
+ # Container for transducer
5
+ class Transducer
6
+ attr_accessor :reduced
7
+ attr_accessor :value
8
+
9
+ # def initialize(reduced: false, value: nil)
10
+ # @reduced = reduced
11
+ # @value = value
12
+ # end
13
+ end
14
+ end
15
+ end
16
+ end
data/lib/ramda/list.rb CHANGED
@@ -507,7 +507,11 @@ module Ramda
507
507
  # ((a, b) -> a) -> a -> [b] -> a
508
508
  #
509
509
  curried_method(:reduce) do |f, acc, xs|
510
- xs.reduce(acc, &f)
510
+ xs.reduce(acc) do |loc_acc, x|
511
+ res = f.call(loc_acc, x)
512
+ break res.value if res.is_a?(::Ramda::Internal::Transducers::Transducer) && res.reduced
513
+ res
514
+ end
511
515
  end
512
516
 
513
517
  # Returns a single item by iterating through the list, successively
@@ -523,7 +527,28 @@ module Ramda
523
527
  # ((a, b) -> a) -> a -> [b] -> a
524
528
  #
525
529
  curried_method(:reduce_right) do |f, acc_arg, xs|
526
- xs.reverse.reduce(acc_arg) { |acc, x| f.call(x, acc) }
530
+ xs.reverse.reduce(acc_arg) do |acc, x|
531
+ res = f.call(x, acc)
532
+ break res.value if res.is_a?(::Ramda::Internal::Transducers::Transducer) && res.reduced
533
+ res
534
+ end
535
+ end
536
+
537
+ # Returns a value wrapped to indicate that it is the final value of
538
+ # the reduce and transduce functions. The returned value should be
539
+ # considered a black box: the internal structure is not guaranteed
540
+ # to be stable.
541
+ #
542
+ # Note: this optimization is unavailable to functions not explicitly
543
+ # listed above.
544
+ #
545
+ # a -> *
546
+ #
547
+ curried_method(:reduced) do |x|
548
+ t = ::Ramda::Internal::Transducers::Transducer.new
549
+ t.reduced = true
550
+ t.value = x
551
+ t
527
552
  end
528
553
 
529
554
  # Returns a new list or string with the elements or characters in reverse order.
@@ -652,7 +677,12 @@ module Ramda
652
677
  # ((a, b) -> a) -> a -> [b] -> a
653
678
  #
654
679
  curried_method(:transduce) do |xf, rx, acc, xs|
655
- xs.reduce(acc, &xf.call(rx))
680
+ f = xf.call(rx)
681
+ xs.reduce(acc) do |loc_acc, x|
682
+ res = f.call(loc_acc, x)
683
+ break res.value if res.is_a?(::Ramda::Internal::Transducers::Transducer) && res.reduced
684
+ res
685
+ end
656
686
  end
657
687
 
658
688
  # Builds a list from a seed value. Accepts an iterator function, which
@@ -57,7 +57,7 @@ module Ramda
57
57
  # a -> b -> Boolean
58
58
  #
59
59
  curried_method(:equals) do |a, b|
60
- a == b
60
+ a.eql?(b)
61
61
  end
62
62
 
63
63
  # Returns true if the first argument is greater than the second; false otherwise.
@@ -77,6 +77,15 @@ module Ramda
77
77
  a >= b
78
78
  end
79
79
 
80
+ # Returns true if its arguments are identical, false otherwise.
81
+ # Values are identical if they reference the same memory.
82
+ #
83
+ # a -> a -> Boolean
84
+ #
85
+ curried_method(:identical) do |a, b|
86
+ a.equal?(b)
87
+ end
88
+
80
89
  # Combines two lists into a set (i.e. no duplicates) composed of those elements
81
90
  # common to both lists. The order is preserved from the original array.
82
91
  #
data/lib/ramda/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Ramda
2
- VERSION = '0.14.0'.freeze
2
+ VERSION = '0.15.0'.freeze
3
3
  end
@@ -25,6 +25,84 @@ describe Ramda::Function do
25
25
  end
26
26
  end
27
27
 
28
+ context '#add_index' do
29
+ describe 'unary functions like `map`' do
30
+ let(:times2) { proc { |x| x * 2 } }
31
+ let(:add_index_param) { proc { |x, idx| x + idx } }
32
+ let(:square_ends) do
33
+ proc do |x, idx, xs|
34
+ idx == 0 || idx == xs.size - 1 ? x * x : x
35
+ end
36
+ end
37
+ let(:map_indexed) { R.add_index(R.map) }
38
+
39
+ it 'works just like a normal map' do
40
+ expect(map_indexed.call(times2, [1, 2, 3, 4])).to eq([2, 4, 6, 8])
41
+ end
42
+
43
+ it 'passes the index as a second parameter to the callback' do
44
+ expect(map_indexed.call(add_index_param, [8, 6, 7, 5, 3, 0, 9]))
45
+ .to eq([8, 7, 9, 8, 7, 5, 15]); # [8 + 0, 6 + 1...]
46
+ end
47
+
48
+ it 'passes the entire list as a third parameter to the callback' do
49
+ expect(map_indexed.call(square_ends, [8, 6, 7, 5, 3, 0, 9])).to eq([64, 6, 7, 5, 3, 0, 81])
50
+ end
51
+
52
+ xit 'acts as a curried function' do
53
+ make_square_ends = map_indexed.call(square_ends)
54
+ expect(make_square_ends.call([8, 6, 7, 5, 3, 0, 9])).to eq([64, 6, 7, 5, 3, 0, 81])
55
+ end
56
+ end
57
+
58
+ describe 'binary functions like `reduce`' do
59
+ let(:reduce_indexed) { R.add_index(R.reduce); }
60
+ let(:times_indexed) { proc { |tot, num, idx| tot + (num * idx); }; }
61
+ let(:objectify) do
62
+ proc { |acc, elem, idx|
63
+ acc[elem] = idx
64
+ acc
65
+ }
66
+ end
67
+
68
+ it 'passes the index as a third parameter to the predicate' do
69
+ expect(reduce_indexed.call(times_indexed, 0, [1, 2, 3, 4, 5])).to eq(40)
70
+ expect(reduce_indexed.call(objectify, {}, [:a, :b, :c, :d, :e]))
71
+ .to eq(a: 0, b: 1, c: 2, d: 3, e: 4)
72
+ end
73
+
74
+ it 'passes the entire list as a fourth parameter to the predicate' do
75
+ list = [1, 2, 3]
76
+ reduce_indexed.call(lambda { |acc, _x, _idx, ls|
77
+ expect(ls).to eq(list)
78
+ acc
79
+ }, 0, list)
80
+ end
81
+ end
82
+
83
+ describe 'works with functions like `all` that do not typically have index applied' do
84
+ let(:all_indexed) { R.add_index(R.all) }
85
+ let(:super_diagonal) { all_indexed.call(R.gt) }
86
+
87
+ it 'passes the index as a second parameter' do
88
+ expect(super_diagonal.call([8, 6, 5, 4, 9]))
89
+ .to be_truthy # 8 > 0, 6 > 1, 5 > 2, 4 > 3, 9 > 5
90
+ expect(super_diagonal.call([8, 6, 1, 3, 9]))
91
+ .to be_falsey # 1 !> 2, 3 !> 3
92
+ end
93
+ end
94
+
95
+ describe 'works with arbitrary user-defined functions' do
96
+ let(:map_filter) { proc { |m, f, list| R.filter(R.compose(f, m), list); } }
97
+ let(:map_filter_indexed) { R.add_index(map_filter) }
98
+
99
+ it 'passes the index as an additional parameter' do
100
+ expect(map_filter_indexed.call(R.multiply, R.gt(R.__, 13), [8, 6, 7, 5, 3, 0, 9]))
101
+ .to eq([7, 5, 9]); # 2 * 7 > 13, 3 * 5 > 13, 6 * 9 > 13
102
+ end
103
+ end
104
+ end
105
+
28
106
  context '#always' do
29
107
  it 'from docs' do
30
108
  str = 'Tee'
@@ -537,6 +537,34 @@ describe Ramda::List do
537
537
  end
538
538
  end
539
539
 
540
+ context '#reduced' do
541
+ it 'wraps a value' do
542
+ # // white box test.
543
+ v = {}
544
+ expect(R.reduced(v).value).to be(v)
545
+ end
546
+
547
+ it 'flags value as reduced' do
548
+ # // white box test.
549
+ expect(R.reduced({}).reduced).to be_truthy
550
+ end
551
+
552
+ it 'short-circuits reduce' do
553
+ # // black box test.
554
+ expect(
555
+ R.reduce(
556
+ lambda { |acc, v|
557
+ result = acc + v
558
+ result = R.reduced(result) if result >= 10
559
+ result
560
+ },
561
+ 0,
562
+ [1, 2, 3, 4, 5]
563
+ )
564
+ ).to eq(10)
565
+ end
566
+ end
567
+
540
568
  context '#reduce_right' do
541
569
  def avg
542
570
  ->(a, b) { (a + b) / 2 }
@@ -77,6 +77,17 @@ describe Ramda::Relation do
77
77
  end
78
78
  end
79
79
 
80
+ context '#identical' do
81
+ it 'from docs' do
82
+ o = {}
83
+ expect(R.identical(o, o)).to be_truthy
84
+ expect(R.identical(1, 1)).to be_truthy
85
+ expect(R.identical(1, '1')).to be_falsey
86
+ expect(R.identical([], [])).to be_falsey
87
+ expect(R.identical(nil, nil)).to be_truthy
88
+ end
89
+ end
90
+
80
91
  context '#intersection' do
81
92
  it 'from docs' do
82
93
  expect(r.intersection([1, 2, 3, 4], [7, 6, 5, 4, 3])).to eq([3, 4])
data/spec/ramda_spec.rb CHANGED
@@ -17,6 +17,7 @@ describe Ramda do
17
17
  r(:T)
18
18
  r(:__)
19
19
  r(:add)
20
+ r(:add_index)
20
21
  r(:adjust)
21
22
  r(:all)
22
23
  r(:all_pass)
@@ -80,6 +81,7 @@ describe Ramda do
80
81
  r(:has)
81
82
  r(:has_in)
82
83
  r(:head)
84
+ r(:identical)
83
85
  r(:identity)
84
86
  r(:if_else)
85
87
  r(:inc)
@@ -154,6 +156,7 @@ describe Ramda do
154
156
  r(:props)
155
157
  r(:range)
156
158
  r(:reduce)
159
+ r(:reduced)
157
160
  r(:reduce_right)
158
161
  r(:reject)
159
162
  r(:remove)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ramda-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.14.0
4
+ version: 0.15.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vadim Lazebny
@@ -47,6 +47,7 @@ files:
47
47
  - lib/ramda/internal/transducers/filter_transdurer.rb
48
48
  - lib/ramda/internal/transducers/map_transducer.rb
49
49
  - lib/ramda/internal/transducers/take_transducer.rb
50
+ - lib/ramda/internal/transducers/transducer.rb
50
51
  - lib/ramda/list.rb
51
52
  - lib/ramda/logic.rb
52
53
  - lib/ramda/math.rb