ramda-ruby 0.8.1 → 0.9.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: 551c4cf30d89de74593b3d778aa686c240a80daf
4
- data.tar.gz: cf3ae825a6eec5dd22feecdf318cd49578b79416
3
+ metadata.gz: 87c6a7c3172da72254170f7d90c79f3837e8a74e
4
+ data.tar.gz: 0f57eed0c3497ed9da5725a72f7efa279e475473
5
5
  SHA512:
6
- metadata.gz: e911adf71d38c300aa2852872b11d35c0bc03fb3f7d7700d8f1f7171b4c0ba18e150cf4189baf683a80bab43c3d497f46f403c52fbaa63609978608ab1ae6451
7
- data.tar.gz: b5f58b29149340e02badd0ab0804e797550699ccb7df3c6cd1b00344906b1c3b69f95ba703ac1e1bacae02e07ed378b91d6507277d393a93b59ab007e0bc0007
6
+ metadata.gz: a89e53d9df3f0f65620cc5be1b85addb80b7940d2fa56b7866c5b9a3fb31125e8f928da57ea3f0a12bc46b8be55bd8fa47b5c5ec2a2030feeac904a3ed4ec1c7
7
+ data.tar.gz: 7645cde2e138e671dff744fda5c108284e7d4def28de3399770faf03d1cfc3e29cd023143d5cdef1e8b52f5268c27a6d85bbb5bd6f475e8e2ca84eac2d4a44b7
data/CHANGELOG.md CHANGED
@@ -1,7 +1,28 @@
1
1
  Not Released
2
2
  ---------------
3
3
 
4
+ Release 0.9.0
5
+ ---------------
6
+
7
+ Added debug_mode which shows name, arguments and result of each function call.
8
+
9
+ Small performance improvements.
10
+
11
+ Added:
12
+
13
+ * call
14
+ * drop_while
15
+ * evolve
16
+ * init
17
+ * insert_all
18
+ * invert
19
+ * invert_obj
20
+ * map_obj_indexed
21
+ * nth_arg
22
+
23
+
4
24
  Release 0.8.1
25
+ ---------------
5
26
 
6
27
  Added ability to change exceptions handler.
7
28
  Added benchmarks.
data/README.md CHANGED
@@ -4,6 +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-164-green.svg)](docs/FUNCTIONS.md)
7
8
  [![Travis badge](https://travis-ci.org/lazebny/ramda-ruby.svg?branch=master)](https://travis-ci.org/lazebny/ramda-ruby)
8
9
  [![AppVeyor status](https://ci.appveyor.com/api/projects/status/ponccdax7aj4ufw2?svg=true)](https://ci.appveyor.com/project/lazebny/ramda-ruby)
9
10
  [![Coverage Status](https://coveralls.io/repos/lazebny/ramda-ruby/badge.png)](https://coveralls.io/r/lazebny/ramda-ruby)
@@ -12,11 +13,11 @@ This is a ruby version of [Ramda Js](http://ramdajs.com) library.
12
13
  [![Dependency Status](https://gemnasium.com/lazebny/ramda-ruby.svg)](https://gemnasium.com/lazebny/ramda-ruby)
13
14
  [![License](https://img.shields.io/badge/license-MIT-green.svg)](http://opensource.org/licenses/MIT)
14
15
 
15
- Problems
16
+ Information
16
17
  ------------
17
18
 
18
- This gem is stable but i think it isn't production ready - benchmarks
19
- are showing a huge performance loss comparing to the plain ruby.
19
+ This library uses some stuff like `curry` and `__` which reduces performance.
20
+ Please review benchmarks section.
20
21
 
21
22
 
22
23
  Installation
@@ -86,7 +87,7 @@ Placeholder:
86
87
 
87
88
  ```
88
89
 
89
- Change Exceptions Handler:
90
+ Change exceptions handler:
90
91
 
91
92
  ```ruby
92
93
 
@@ -102,6 +103,30 @@ Change Exceptions Handler:
102
103
 
103
104
  ```
104
105
 
106
+ Ramda in debug mode shows calls trace.
107
+ Notice: trace is shown only for Ramda functions and not for regular procs.
108
+ If you want to show a whole trace you can wrap your proc in Ramda.curry function
109
+ (pleaes see example below).
110
+
111
+ Enable debug mode:
112
+
113
+ ```ruby
114
+
115
+ Ramda.debug_mode = true
116
+
117
+ # Example:
118
+
119
+ Ramda.filter(Ramda.curry(->(n) { n.even? }), [1, 2, 3, 4])
120
+
121
+ # -> curry(#<Proc:0x...@/srv/app/spec/ramda/list_spec.rb:130 (lambda)>) # #<Proc:0x... (lambda)>
122
+ # -> curry(1) # false
123
+ # -> curry(2) # true
124
+ # -> curry(3) # false
125
+ # -> curry(4) # true
126
+ # -> filter(#<Proc:0x0055ff3fad4640 (lambda)>, 1, 2, 3, 4) # [2, 4]
127
+
128
+ ```
129
+
105
130
 
106
131
  Benchmarks
107
132
  -------------
data/ROADMAP.md CHANGED
@@ -1,15 +1,3 @@
1
- Release 0.9.0
2
- ---------------
3
- * call
4
- * drop_while
5
- * evolve
6
- * init
7
- * insert_all
8
- * invert
9
- * invert_obj
10
- * map_obj_indexed
11
- * nth_arg
12
-
13
1
  Release 0.10.0
14
2
  ---------------
15
3
  * compose_p
data/docs/FUNCTIONS.md CHANGED
@@ -19,6 +19,7 @@
19
19
  * assoc_path
20
20
  * binary
21
21
  * bind
22
+ * call
22
23
  * chain
23
24
  * clone
24
25
  * comparator
@@ -39,10 +40,12 @@
39
40
  * dissoc
40
41
  * divide
41
42
  * drop
43
+ * drop_while
42
44
  * empty - uses x.class.new for defining new empty object if empty method does not exist
43
45
  * eq_by
44
46
  * eq_props
45
47
  * equals
48
+ * evolve
46
49
  * filter
47
50
  * find
48
51
  * find_index - returns nil if index doesn't exist
@@ -62,8 +65,12 @@
62
65
  * if_else
63
66
  * inc
64
67
  * index_of - returns nil if index doesn't exist
68
+ * init
65
69
  * insert
70
+ * insert_all
66
71
  * intersection
72
+ * invert
73
+ * invert_obj
67
74
  * invoker
68
75
  * is
69
76
  * is_empty
@@ -83,6 +90,7 @@
83
90
  * lt
84
91
  * lte
85
92
  * map
93
+ * map_obj_indexed
86
94
  * match
87
95
  * max
88
96
  * max_by
@@ -96,6 +104,7 @@
96
104
  * negate
97
105
  * not
98
106
  * nth
107
+ * nth_arg
99
108
  * of
100
109
  * omit
101
110
  * once
data/docs/RESOURCES.md ADDED
@@ -0,0 +1,2 @@
1
+ * [Funkify and Pattern-Proc: Messing with Ruby for Fun (but probably not profit)](https://spin.atomicobject.com/2014/07/23/ruby-partial-application-first-class/)
2
+ * [Funkify](https://github.com/banister/funkify)
data/lib/ramda.rb CHANGED
@@ -1,5 +1,3 @@
1
- require 'forwardable'
2
-
3
1
  require 'ramda/version'
4
2
  require 'ramda/exception_handler'
5
3
  require 'ramda/function'
@@ -10,186 +8,78 @@ require 'ramda/object'
10
8
  require 'ramda/relation'
11
9
  require 'ramda/string'
12
10
  require 'ramda/type'
11
+ require 'ramda/internal/java/__make_curry_proc__' if RUBY_PLATFORM == 'java'
13
12
 
14
13
  # Ramda library implementation, source: http://ramdajs.com/
15
- # rubocop:disable Metrics/ModuleLength
16
14
  module Ramda
17
- extend SingleForwardable
18
-
19
- def_delegators Ramda::Function,
20
- :F,
21
- :T,
22
- :__,
23
- :always,
24
- :ap,
25
- :apply,
26
- :binary,
27
- :bind,
28
- :comparator,
29
- :compose,
30
- :construct,
31
- :construct_n,
32
- :converge,
33
- :curry,
34
- :curry_n,
35
- :empty,
36
- :flip,
37
- :identity,
38
- :invoker,
39
- :juxt,
40
- :lift,
41
- :lift_n,
42
- :memoize,
43
- :n_ary,
44
- :of,
45
- :once,
46
- :pipe,
47
- :tap,
48
- :unapply,
49
- :unary,
50
- :use_with
15
+ extend Ramda::Function
16
+ extend Ramda::List
17
+ extend Ramda::Logic
18
+ extend Ramda::Math
19
+ extend Ramda::Object
20
+ extend Ramda::Relation
21
+ extend Ramda::String
22
+ extend Ramda::Type
23
+ extend Ramda::Internal::Java::MakeCurryProc if RUBY_PLATFORM == 'java'
51
24
 
52
- def_delegators Ramda::List,
53
- :all,
54
- :any,
55
- :append,
56
- :chain,
57
- :concat,
58
- :contains,
59
- :drop,
60
- :filter,
61
- :from_pairs,
62
- :find,
63
- :find_index,
64
- :find_last,
65
- :find_last_index,
66
- :flatten,
67
- :for_each,
68
- :group_by,
69
- :head,
70
- :index_of,
71
- :insert,
72
- :join,
73
- :last,
74
- :last_index_of,
75
- :length,
76
- :slice,
77
- :sort,
78
- :map,
79
- :nth,
80
- :partition,
81
- :pluck,
82
- :prepend,
83
- :range,
84
- :reduce,
85
- :reduce_right,
86
- :reject,
87
- :remove,
88
- :repeat,
89
- :reverse,
90
- :tail,
91
- :take,
92
- :take_while,
93
- :times,
94
- :uniq,
95
- :uniq_with,
96
- :unnest,
97
- :update,
98
- :xprod,
99
- :zip,
100
- :zip_obj,
101
- :zip_with
25
+ # Constants are faster than module variables
26
+ #
27
+ def self.const_missing(name)
28
+ value = {
29
+ DEBUG_MODE: false
30
+ }[name]
102
31
 
103
- def_delegators Ramda::Logic,
104
- :all_pass,
105
- :and,
106
- :any_pass,
107
- :complement,
108
- :cond,
109
- :if_else,
110
- :is_empty,
111
- :not,
112
- :or
32
+ value.nil? ? super : const_set(name, value)
33
+ end
113
34
 
114
- def_delegators Ramda::Math,
115
- # :math_mod,
116
- :add,
117
- :dec,
118
- :divide,
119
- :inc,
120
- :modulo,
121
- :multiply,
122
- :negate,
123
- :product,
124
- :subtract,
125
- :sum
35
+ # A special placeholder value used to specify "gaps" within curried
36
+ # functions, allowing partial application of any combination of
37
+ # arguments, regardless of their positions.
38
+ #
39
+ # If g is a curried ternary function and _ is R.__, the following are equivalent:
40
+ #
41
+ # g(1, 2, 3)
42
+ # g(_, 2, 3)(1)
43
+ # g(_, _, 3)(1)(2)
44
+ # g(_, _, 3)(1, 2)
45
+ # g(_, 2, _)(1, 3)
46
+ # g(_, 2)(1)(3)
47
+ # g(_, 2)(1, 3)
48
+ # g(_, 2)(_, 3)(1)
49
+ #
50
+ def self.__
51
+ :ramda_placeholder
52
+ end
126
53
 
127
- def_delegators Ramda::Object,
128
- # :keys_in,
129
- # :to_pairs_in,
130
- # :values_in,
131
- :assoc,
132
- :assoc_path,
133
- :clone,
134
- :dissoc,
135
- :eq_props,
136
- :has,
137
- :has_in,
138
- :keys,
139
- :lens,
140
- :lens_index,
141
- :lens_path,
142
- :lens_prop,
143
- :merge,
144
- :omit,
145
- :over,
146
- :path,
147
- :pick,
148
- :pick_all,
149
- :pick_by,
150
- :project,
151
- :prop,
152
- :prop_or,
153
- :props,
154
- :set,
155
- :to_pairs,
156
- :values,
157
- :view,
158
- :where
54
+ # A function that always returns false. Any passed in parameters are ignored.
55
+ #
56
+ # * -> Boolean
57
+ #
58
+ # rubocop:disable Style/MethodName
59
+ def self.F
60
+ ->(*) { false }
61
+ end
62
+ # rubocop:enable Style/MethodName
159
63
 
160
- def_delegators Ramda::Relation,
161
- :count_by,
162
- :difference,
163
- :difference_with,
164
- :eq_by,
165
- :equals,
166
- :gt,
167
- :gte,
168
- :intersection,
169
- :lt,
170
- :lte,
171
- :max,
172
- :max_by,
173
- :min,
174
- :min_by,
175
- :path_eq,
176
- :prop_eq,
177
- :sort_by,
178
- :union,
179
- :union_with
64
+ # A function that always returns true. Any passed in parameters are ignored.
65
+ #
66
+ # * -> Boolean
67
+ #
68
+ # rubocop:disable Style/MethodName
69
+ def self.T
70
+ ->(*) { true }
71
+ end
72
+ # rubocop:enable Style/MethodName
180
73
 
181
- def_delegators Ramda::String,
182
- :match,
183
- :replace,
184
- :split,
185
- :to_lower,
186
- :to_upper,
187
- :trim
74
+ # Takes Boolean
75
+ def self.debug_mode=(enabled)
76
+ const_set('DEBUG_MODE', enabled)
77
+ end
188
78
 
189
- def_delegators Ramda::Type,
190
- :is,
191
- :is_nil,
192
- :type
79
+ # Returns Boolean/NilClass
80
+ def self.debug_mode
81
+ DEBUG_MODE
82
+ end
193
83
 
194
84
  def self.exception_handler=(handler)
195
85
  @exception_handler = handler
@@ -199,3 +89,5 @@ module Ramda
199
89
  @exception_handler ||= ::Ramda::ExceptionHandler.method(:with_narrow)
200
90
  end
201
91
  end
92
+
93
+ # Ramda.debug_mode = true
@@ -7,38 +7,6 @@ module Ramda
7
7
  module Function
8
8
  extend ::Ramda::Internal::CurriedMethod
9
9
 
10
- # A special placeholder value used to specify "gaps" within curried
11
- # functions, allowing partial application of any combination of
12
- # arguments, regardless of their positions.
13
- #
14
- # If g is a curried ternary function and _ is R.__, the following are equivalent:
15
- #
16
- # g(1, 2, 3)
17
- # g(_, 2, 3)(1)
18
- # g(_, _, 3)(1)(2)
19
- # g(_, _, 3)(1, 2)
20
- # g(_, 2, _)(1, 3)
21
- # g(_, 2)(1)(3)
22
- # g(_, 2)(1, 3)
23
- # g(_, 2)(_, 3)(1)
24
- #
25
-
26
- # rubocop:disable Style/MethodName
27
- def self.F
28
- ->(*) { false }
29
- end
30
- # rubocop:enable Style/MethodName
31
-
32
- # rubocop:disable Style/MethodName
33
- def self.T
34
- ->(*) { true }
35
- end
36
- # rubocop:enable Style/MethodName
37
-
38
- def self.__
39
- :ramda_placeholder
40
- end
41
-
42
10
  # Returns a function that always returns the given value. Note that
43
11
  # for non-primitives the value returned is a reference to the original
44
12
  # value.
@@ -82,7 +50,7 @@ module Ramda
82
50
  #
83
51
  #
84
52
  curried_method(:binary) do |fn|
85
- n_ary(2, fn)
53
+ ::Ramda.n_ary(2, fn)
86
54
  end
87
55
 
88
56
  # Creates a function that is bound to a context.
@@ -93,6 +61,10 @@ module Ramda
93
61
  fn.unbind.bind(ctx)
94
62
  end
95
63
 
64
+ curried_method(:call) do |fn, *xs|
65
+ fn.call(*xs)
66
+ end
67
+
96
68
  # Makes a comparator function out of a function that reports whether
97
69
  # the first element is less than the second.
98
70
  #
@@ -115,7 +87,7 @@ module Ramda
115
87
  #
116
88
  # Note: The result of compose is not automatically curried.
117
89
  curried_method(:compose) do |*fns|
118
- pipe(*fns.reverse)
90
+ ::Ramda.pipe(*fns.reverse)
119
91
  end
120
92
 
121
93
  # Wraps a constructor function inside a curried function that can be called
@@ -135,8 +107,7 @@ module Ramda
135
107
  # Number -> (* -> {*}) -> (* -> {*})
136
108
  #
137
109
  curried_method(:construct_n) do |arity, constructor|
138
- Ramda::Internal::FunctionWithArity
139
- .new
110
+ ::Ramda::Internal::FunctionWithArity
140
111
  .call(arity, &constructor.method(:new))
141
112
  .curry
142
113
  end
@@ -242,7 +213,7 @@ module Ramda
242
213
  # Number -> String -> (a -> b -> ... -> n -> Object -> *)
243
214
  #
244
215
  curried_method(:invoker) do |arity, method_name|
245
- Ramda::Internal::FunctionWithArity.new.call(arity + 1) do |*args, object|
216
+ ::Ramda::Internal::FunctionWithArity.call(arity + 1) do |*args, object|
246
217
  object.public_send(method_name, *args)
247
218
  end.curry
248
219
  end
@@ -263,7 +234,7 @@ module Ramda
263
234
  # https://stackoverflow.com/questions/36558598/cant-wrap-my-head-around-lift-in-ramda-js
264
235
  curried_method(:lift) do |fn, a, *xs|
265
236
  # ([a] + xs).reduce([curry(fn)], &ap)
266
- lift_n(fn.arity, fn, a, *xs)
237
+ ::Ramda.lift_n(fn.arity, fn, a, *xs)
267
238
  end
268
239
 
269
240
  # "lifts" a function to be the specified arity, so that it may
@@ -273,7 +244,7 @@ module Ramda
273
244
  # Number -> (*... -> *) -> ([*]... -> [*])
274
245
  #
275
246
  curried_method(:lift_n) do |arity, fn, a, *xs|
276
- ([a] + xs).reduce([curry_n(arity, fn)], &ap)
247
+ ([a] + xs).reduce([::Ramda.curry_n(arity, fn)], &::Ramda.ap)
277
248
  end
278
249
 
279
250
  # Creates a new function that, when invoked, caches the result of calling
@@ -287,7 +258,7 @@ module Ramda
287
258
  curried_method(:memoize) do |fn|
288
259
  memo = {}
289
260
 
290
- Ramda::Internal::FunctionWithArity.new.call(fn.arity) do |*args|
261
+ ::Ramda::Internal::FunctionWithArity.call(fn.arity) do |*args|
291
262
  memo[args] = fn.call(*args) unless memo.key?(args)
292
263
  memo[args]
293
264
  end.curry
@@ -300,11 +271,19 @@ module Ramda
300
271
  # Number -> (* -> a) -> (* -> a)
301
272
  #
302
273
  curried_method(:n_ary) do |arity, fn|
303
- Ramda::Internal::FunctionWithArity.new.call(arity) do |*args|
274
+ ::Ramda::Internal::FunctionWithArity.call(arity) do |*args|
304
275
  fn.call(*(args.first(arity) + Array.new(fn.arity - arity, nil)))
305
276
  end.curry
306
277
  end
307
278
 
279
+ # Returns a function which returns its nth argument.
280
+ #
281
+ # Number -> *... -> *
282
+ #
283
+ curried_method(:nth_arg) do |index, x, *xs|
284
+ ([x] + xs)[index]
285
+ end
286
+
308
287
  # Returns a singleton array containing the value provided.
309
288
  #
310
289
  # a -> [a]
@@ -323,7 +302,7 @@ module Ramda
323
302
  curried_method(:once) do |fn|
324
303
  memo = {}
325
304
 
326
- Ramda::Internal::FunctionWithArity.new.call(fn.arity) do |*args|
305
+ ::Ramda::Internal::FunctionWithArity.call(fn.arity) do |*args|
327
306
  memo[:result] = fn.call(*args) unless memo.key?(:result)
328
307
  memo[:result]
329
308
  end.curry
@@ -372,7 +351,7 @@ module Ramda
372
351
  # (* -> b) -> (a -> b)
373
352
  #
374
353
  curried_method(:unary) do |fn|
375
- n_ary(1, fn)
354
+ ::Ramda.n_ary(1, fn)
376
355
  end
377
356
 
378
357
  # Accepts a function fn and a list of transformer functions and returns
@@ -383,7 +362,7 @@ module Ramda
383
362
  # (x1 -> x2 -> ... -> z) -> [(a -> b -> ... -> x1), ...] -> (a -> b -> ... -> z)
384
363
  #
385
364
  curried_method(:use_with) do |fn, fns|
386
- Ramda::Internal::FunctionWithArity.new.call(fns.count) do |*args|
365
+ ::Ramda::Internal::FunctionWithArity.call(fns.count) do |*args|
387
366
  modified_args = args.each_with_index.map do |arg, index|
388
367
  fns[index].call(arg)
389
368
  end