ramda-ruby 0.1.0.alpha
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/.bashrc.docker +2 -0
- data/.coveralls.yml +1 -0
- data/.gitignore +14 -0
- data/.overcommit.yml +73 -0
- data/.pryrc.docker +1 -0
- data/.rspec +2 -0
- data/.rubocop.yml +42 -0
- data/.travis.yml +35 -0
- data/CHANGELOG.md +3 -0
- data/Dockerfile +24 -0
- data/Gemfile +23 -0
- data/Gemfile.dev +6 -0
- data/Gemfile.dev.lock +38 -0
- data/LICENSE.txt +21 -0
- data/README.md +68 -0
- data/ROADMAP.md +14 -0
- data/Rakefile +6 -0
- data/appveyor.yml +26 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/codeclimate.yml +6 -0
- data/docker-compose.override.yml +12 -0
- data/docker-compose.yml +15 -0
- data/docs/FUNCTIONS.md +93 -0
- data/lib/ramda.rb +123 -0
- data/lib/ramda/function.rb +181 -0
- data/lib/ramda/internal/curried_method.rb +16 -0
- data/lib/ramda/internal/function_with_arity.rb +30 -0
- data/lib/ramda/list.rb +372 -0
- data/lib/ramda/logic.rb +58 -0
- data/lib/ramda/math.rb +72 -0
- data/lib/ramda/object.rb +110 -0
- data/lib/ramda/relation.rb +161 -0
- data/lib/ramda/string.rb +49 -0
- data/lib/ramda/version.rb +3 -0
- data/ramda-ruby.gemspec +24 -0
- metadata +80 -0
data/lib/ramda/math.rb
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
require_relative 'internal/curried_method'
|
2
|
+
|
3
|
+
module Ramda
|
4
|
+
# Math functions
|
5
|
+
module Math
|
6
|
+
extend ::Ramda::Internal::CurriedMethod
|
7
|
+
|
8
|
+
# Add two values.
|
9
|
+
#
|
10
|
+
# Number -> Number -> Number
|
11
|
+
#
|
12
|
+
curried_method(:add) do |a, b|
|
13
|
+
a + b
|
14
|
+
end
|
15
|
+
|
16
|
+
# Decrements its argument.
|
17
|
+
#
|
18
|
+
# Number -> Number
|
19
|
+
#
|
20
|
+
curried_method(:dec) do |a|
|
21
|
+
a - 1
|
22
|
+
end
|
23
|
+
|
24
|
+
# Divides two numbers. Equivalent to a / b.
|
25
|
+
#
|
26
|
+
# Number -> Number -> Number
|
27
|
+
#
|
28
|
+
curried_method(:divide) do |a, b|
|
29
|
+
a.to_f / b
|
30
|
+
end
|
31
|
+
|
32
|
+
# Increments its argument.
|
33
|
+
#
|
34
|
+
# Number -> Number
|
35
|
+
#
|
36
|
+
curried_method(:inc) do |a|
|
37
|
+
a + 1
|
38
|
+
end
|
39
|
+
|
40
|
+
# Multiplies two numbers. Equivalent to a * b but curried.
|
41
|
+
#
|
42
|
+
# Number -> Number -> Number
|
43
|
+
#
|
44
|
+
curried_method(:multiply) do |a, b|
|
45
|
+
a * b
|
46
|
+
end
|
47
|
+
|
48
|
+
# Multiplies together all the elements of a list.
|
49
|
+
#
|
50
|
+
# [Number] -> Number
|
51
|
+
#
|
52
|
+
curried_method(:product) do |xs|
|
53
|
+
xs.reduce(:*)
|
54
|
+
end
|
55
|
+
|
56
|
+
# Subtracts its second argument from its first argument.
|
57
|
+
#
|
58
|
+
# Number -> Number -> Number
|
59
|
+
#
|
60
|
+
curried_method(:subtract) do |a, b|
|
61
|
+
a - b
|
62
|
+
end
|
63
|
+
|
64
|
+
# Adds together all the elements of a list.
|
65
|
+
#
|
66
|
+
# [Number] -> Number
|
67
|
+
#
|
68
|
+
curried_method(:sum) do |xs|
|
69
|
+
xs.reduce(:+)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
data/lib/ramda/object.rb
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
require_relative 'internal/curried_method'
|
2
|
+
|
3
|
+
module Ramda
|
4
|
+
# Math functions
|
5
|
+
module Object
|
6
|
+
extend ::Ramda::Internal::CurriedMethod
|
7
|
+
|
8
|
+
# Creates a deep copy of the value which may contain (nested)
|
9
|
+
# Arrays and Objects, Numbers, Strings, Booleans and Dates.
|
10
|
+
# Functions are assigned by reference rather than copied
|
11
|
+
#
|
12
|
+
# {*} -> {*}
|
13
|
+
#
|
14
|
+
curried_method(:clone) do |object|
|
15
|
+
case object
|
16
|
+
when Array
|
17
|
+
object.dup.map(&method(:clone))
|
18
|
+
else
|
19
|
+
object.dup
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
# Reports whether two objects have the same value, in R.equals terms,
|
24
|
+
# for the specified property. Useful as a curried predicate.
|
25
|
+
#
|
26
|
+
# k -> {k: v} -> {k: v} -> Boolean
|
27
|
+
#
|
28
|
+
curried_method(:eq_props) do |prop, a, b|
|
29
|
+
a[prop] == b[prop]
|
30
|
+
end
|
31
|
+
|
32
|
+
# Returns a list containing the names of all the enumerable own properties
|
33
|
+
# of the supplied object.
|
34
|
+
# Note that the order of the output array is not guaranteed.
|
35
|
+
#
|
36
|
+
# {k: v} -> [k]
|
37
|
+
#
|
38
|
+
curried_method(:keys, &:keys)
|
39
|
+
|
40
|
+
# Create a new object with the own properties of the first object merged
|
41
|
+
# with the own properties of the second object. If a key exists in both
|
42
|
+
# objects, the value from the second object will be used.
|
43
|
+
#
|
44
|
+
# {k: v} -> {k: v} -> {k: v}
|
45
|
+
#
|
46
|
+
curried_method(:merge) do |obj_a, obj_b|
|
47
|
+
obj_a.merge(obj_b)
|
48
|
+
end
|
49
|
+
|
50
|
+
# Returns a partial copy of an object omitting the keys specified.
|
51
|
+
#
|
52
|
+
# [String] -> {String: *} -> {String: *}
|
53
|
+
#
|
54
|
+
curried_method(:omit) do |keys, obj|
|
55
|
+
obj_copy = obj.dup
|
56
|
+
keys.each(&obj_copy.method(:delete))
|
57
|
+
obj_copy
|
58
|
+
end
|
59
|
+
|
60
|
+
# Returns a partial copy of an object containing only the keys specified.
|
61
|
+
# If the key does not exist, the property is ignored.
|
62
|
+
#
|
63
|
+
# [k] -> {k: v} -> {k: v}
|
64
|
+
#
|
65
|
+
curried_method(:pick) do |keys, obj|
|
66
|
+
obj.select { |k, _| keys.include?(k) }
|
67
|
+
end
|
68
|
+
|
69
|
+
# Similar to pick except that this one includes a key: undefined pair for
|
70
|
+
# properties that don't exist.
|
71
|
+
#
|
72
|
+
# [k] -> {k: v} -> {k: v}
|
73
|
+
#
|
74
|
+
curried_method(:pick_all) do |keys, obj|
|
75
|
+
Hash[keys.map { |k| [k, obj.key?(k) ? obj.fetch(k) : nil] }]
|
76
|
+
end
|
77
|
+
|
78
|
+
# Reasonable analog to SQL select statement.
|
79
|
+
#
|
80
|
+
# [k] -> [{k: v}] -> [{k: v}]
|
81
|
+
#
|
82
|
+
curried_method(:project) do |keys, objs|
|
83
|
+
objs.map(&pick_all(keys))
|
84
|
+
end
|
85
|
+
|
86
|
+
# Returns a function that when supplied an object returns the indicated
|
87
|
+
# property of that object, if it exists.
|
88
|
+
#
|
89
|
+
# s -> {s: a} -> a | NilClass
|
90
|
+
#
|
91
|
+
curried_method(:prop) do |key, obj|
|
92
|
+
obj[key]
|
93
|
+
end
|
94
|
+
|
95
|
+
# Acts as multiple prop: array of keys in, array of values out.
|
96
|
+
# Preserves order.
|
97
|
+
#
|
98
|
+
# [k] -> {k: v} -> [v]
|
99
|
+
#
|
100
|
+
curried_method(:props) do |keys, obj|
|
101
|
+
keys.map(&obj.method(:[]))
|
102
|
+
end
|
103
|
+
|
104
|
+
# Returns a list of all the enumerable own properties of the supplied object.
|
105
|
+
#
|
106
|
+
# {k: v} -> [v]
|
107
|
+
#
|
108
|
+
curried_method(:values, &:values)
|
109
|
+
end
|
110
|
+
end
|
@@ -0,0 +1,161 @@
|
|
1
|
+
require_relative 'internal/curried_method'
|
2
|
+
|
3
|
+
module Ramda
|
4
|
+
# Relation functions
|
5
|
+
module Relation
|
6
|
+
extend ::Ramda::Internal::CurriedMethod
|
7
|
+
|
8
|
+
# Counts the elements of a list according to how many match each value
|
9
|
+
# of a key generated by the supplied function. Returns an object mapping
|
10
|
+
# the keys produced by fn to the number of occurrences in the list.
|
11
|
+
|
12
|
+
# Acts as a transducer if a transformer is given in list position.
|
13
|
+
#
|
14
|
+
# (a -> String) -> [a] -> {*}
|
15
|
+
#
|
16
|
+
curried_method(:count_by) do |fn, list|
|
17
|
+
list
|
18
|
+
.group_by(&fn)
|
19
|
+
.each_with_object({}) { |(key, value), acc| acc[key] = value.count }
|
20
|
+
end
|
21
|
+
|
22
|
+
# Finds the set (i.e. no duplicates) of all elements in the first list not
|
23
|
+
# contained in the second list. Objects and Arrays are compared in terms
|
24
|
+
# of value equality, not reference equality.
|
25
|
+
#
|
26
|
+
# [*] -> [*] -> [*]
|
27
|
+
#
|
28
|
+
curried_method(:difference) do |list_a, list_b|
|
29
|
+
list_a - list_b
|
30
|
+
end
|
31
|
+
|
32
|
+
# Finds the set (i.e. no duplicates) of all elements in the first list not
|
33
|
+
# contained in the second list. Duplication is determined according to the
|
34
|
+
# value returned by applying the supplied predicate to two list elements.
|
35
|
+
#
|
36
|
+
# ((a, a) -> Boolean) -> [a] -> [a] -> [a]
|
37
|
+
#
|
38
|
+
# TODO: optimize
|
39
|
+
curried_method(:difference_with) do |fn, list_a, list_b|
|
40
|
+
list_a.reject do |el_a|
|
41
|
+
list_b.find { |el_b| fn.call(el_a, el_b) }
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
# Takes a function and two values in its domain and returns true if the
|
46
|
+
# values map to the same value in the codomain; false otherwise.
|
47
|
+
#
|
48
|
+
# (a -> b) -> a -> a -> Boolean
|
49
|
+
#
|
50
|
+
curried_method(:eq_by) do |fn, a, b|
|
51
|
+
fn.call(a) == fn.call(b)
|
52
|
+
end
|
53
|
+
|
54
|
+
# Returns true if its arguments are equivalent, false otherwise.
|
55
|
+
# Handles cyclical data structures.
|
56
|
+
#
|
57
|
+
# a -> b -> Boolean
|
58
|
+
#
|
59
|
+
curried_method(:equals) do |a, b|
|
60
|
+
a == b
|
61
|
+
end
|
62
|
+
|
63
|
+
# Returns true if the first argument is greater than the second; false otherwise.
|
64
|
+
#
|
65
|
+
# Ord a => a -> a -> Boolean
|
66
|
+
#
|
67
|
+
curried_method(:gt) do |a, b|
|
68
|
+
a > b
|
69
|
+
end
|
70
|
+
|
71
|
+
# Returns true if the first argument is greater than or equal to the second;
|
72
|
+
# false otherwise.
|
73
|
+
#
|
74
|
+
# Ord a => a -> a -> Boolean
|
75
|
+
#
|
76
|
+
curried_method(:gte) do |a, b|
|
77
|
+
a >= b
|
78
|
+
end
|
79
|
+
|
80
|
+
# Combines two lists into a set (i.e. no duplicates) composed of those elements
|
81
|
+
# common to both lists. The order is preserved from the original array.
|
82
|
+
#
|
83
|
+
# [*] -> [*] -> [*]
|
84
|
+
#
|
85
|
+
curried_method(:intersection) do |list_a, list_b|
|
86
|
+
list_a & list_b
|
87
|
+
end
|
88
|
+
|
89
|
+
# Returns true if the first argument is less than the second; false otherwise.
|
90
|
+
#
|
91
|
+
# a -> a -> Boolean
|
92
|
+
#
|
93
|
+
curried_method(:lt) do |a, b|
|
94
|
+
a < b
|
95
|
+
end
|
96
|
+
|
97
|
+
# Returns true if the first argument is less than or equal to the second;
|
98
|
+
# false otherwise.
|
99
|
+
#
|
100
|
+
# a -> a -> Boolean
|
101
|
+
#
|
102
|
+
curried_method(:lte) do |a, b|
|
103
|
+
a <= b
|
104
|
+
end
|
105
|
+
|
106
|
+
# Returns the larger of its two arguments.
|
107
|
+
#
|
108
|
+
# Ord a => a -> a -> a
|
109
|
+
#
|
110
|
+
curried_method(:max) do |a, b|
|
111
|
+
[a, b].max
|
112
|
+
end
|
113
|
+
|
114
|
+
# Returns the smaller of its two arguments.
|
115
|
+
#
|
116
|
+
# Ord a => a -> a -> a
|
117
|
+
#
|
118
|
+
curried_method(:min) do |a, b|
|
119
|
+
[a, b].min
|
120
|
+
end
|
121
|
+
|
122
|
+
# Returns true if the specified object property is equal, in R.equals terms,
|
123
|
+
# to the given value; false otherwise.
|
124
|
+
#
|
125
|
+
# String -> a -> Object -> Boolean
|
126
|
+
#
|
127
|
+
curried_method(:prop_eq) do |prop, value, obj|
|
128
|
+
obj[prop] == value
|
129
|
+
end
|
130
|
+
|
131
|
+
# Sorts the list according to the supplied function.
|
132
|
+
#
|
133
|
+
# Ord b => (a -> b) -> [a] -> [a]
|
134
|
+
#
|
135
|
+
curried_method(:sort_by) do |fn, xs|
|
136
|
+
xs.sort_by(&fn)
|
137
|
+
end
|
138
|
+
|
139
|
+
# Combines two lists into a set (i.e. no duplicates) composed of the
|
140
|
+
# elements of each list.
|
141
|
+
#
|
142
|
+
# [*] -> [*] -> [*]
|
143
|
+
#
|
144
|
+
curried_method(:union) do |xs1, xs2|
|
145
|
+
xs1 | xs2
|
146
|
+
end
|
147
|
+
|
148
|
+
# Combines two lists into a set (i.e. no duplicates) composed of the elements
|
149
|
+
# of each list. Duplication is determined according to the value returned
|
150
|
+
# by applying the supplied predicate to two list elements. Combines two
|
151
|
+
# lists into a set (i.e. no duplicates) composed of the elements of each list.
|
152
|
+
# Duplication is determined according to the value returned by applying
|
153
|
+
# the supplied predicate to two list elements.
|
154
|
+
#
|
155
|
+
# (a -> a -> Boolean) -> [*] -> [*] -> [*]
|
156
|
+
#
|
157
|
+
curried_method(:union_with) do |fn, xs1, xs2|
|
158
|
+
(xs1 + xs2).uniq(&fn)
|
159
|
+
end
|
160
|
+
end
|
161
|
+
end
|
data/lib/ramda/string.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
require_relative 'internal/curried_method'
|
2
|
+
|
3
|
+
module Ramda
|
4
|
+
# String functions
|
5
|
+
module String
|
6
|
+
extend ::Ramda::Internal::CurriedMethod
|
7
|
+
|
8
|
+
# Splits a string into an array of strings based on the given separator.
|
9
|
+
#
|
10
|
+
# (String | RegExp) -> String -> [String]
|
11
|
+
#
|
12
|
+
curried_method(:split) do |sep, x|
|
13
|
+
x.split(sep)
|
14
|
+
end
|
15
|
+
|
16
|
+
# The upper case version of a string.
|
17
|
+
#
|
18
|
+
# String -> String
|
19
|
+
#
|
20
|
+
curried_method(:to_upper, &:upcase)
|
21
|
+
|
22
|
+
# The lower case version of a string.
|
23
|
+
#
|
24
|
+
# String -> String
|
25
|
+
#
|
26
|
+
curried_method(:to_lower, &:downcase)
|
27
|
+
|
28
|
+
# Tests a regular expression against a String. Note that this function will
|
29
|
+
# return an empty array when there are no matches. This differs from
|
30
|
+
# String.prototype.match which returns null when there are no matches.
|
31
|
+
#
|
32
|
+
# RegExp -> String -> [String | NilClass]
|
33
|
+
#
|
34
|
+
curried_method(:match) do |exp, x|
|
35
|
+
case x
|
36
|
+
when ::String
|
37
|
+
x[exp]
|
38
|
+
# x.scan(exp).flatten
|
39
|
+
else
|
40
|
+
type_error(x)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
# TODO: Extract from this module
|
45
|
+
def self.type_error(object)
|
46
|
+
raise ArgumentError, "Unexpected type #{object.class}"
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
data/ramda-ruby.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
$LOAD_PATH << File.expand_path('../lib', __FILE__)
|
4
|
+
|
5
|
+
require 'ramda/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = 'ramda-ruby'
|
9
|
+
spec.version = Ramda::VERSION
|
10
|
+
spec.authors = ['Vadim Lazebny']
|
11
|
+
spec.email = ['vadim.lazebny@gmail.com']
|
12
|
+
|
13
|
+
spec.summary = 'RamdaJs implementation for Ruby.'
|
14
|
+
spec.description = 'A gem for porting RamdaJs tools for Ruby.'
|
15
|
+
spec.homepage = 'https://github.com/lazebny/ramda-ruby'
|
16
|
+
spec.license = 'MIT'
|
17
|
+
|
18
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
19
|
+
f.match(%r{^(test|spec|features)/})
|
20
|
+
end
|
21
|
+
spec.bindir = 'exe'
|
22
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
23
|
+
spec.require_paths = ['lib']
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ramda-ruby
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0.alpha
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Vadim Lazebny
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-06-16 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: A gem for porting RamdaJs tools for Ruby.
|
14
|
+
email:
|
15
|
+
- vadim.lazebny@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- ".bashrc.docker"
|
21
|
+
- ".coveralls.yml"
|
22
|
+
- ".gitignore"
|
23
|
+
- ".overcommit.yml"
|
24
|
+
- ".pryrc.docker"
|
25
|
+
- ".rspec"
|
26
|
+
- ".rubocop.yml"
|
27
|
+
- ".travis.yml"
|
28
|
+
- CHANGELOG.md
|
29
|
+
- Dockerfile
|
30
|
+
- Gemfile
|
31
|
+
- Gemfile.dev
|
32
|
+
- Gemfile.dev.lock
|
33
|
+
- LICENSE.txt
|
34
|
+
- README.md
|
35
|
+
- ROADMAP.md
|
36
|
+
- Rakefile
|
37
|
+
- appveyor.yml
|
38
|
+
- bin/console
|
39
|
+
- bin/setup
|
40
|
+
- codeclimate.yml
|
41
|
+
- docker-compose.override.yml
|
42
|
+
- docker-compose.yml
|
43
|
+
- docs/FUNCTIONS.md
|
44
|
+
- lib/ramda.rb
|
45
|
+
- lib/ramda/function.rb
|
46
|
+
- lib/ramda/internal/curried_method.rb
|
47
|
+
- lib/ramda/internal/function_with_arity.rb
|
48
|
+
- lib/ramda/list.rb
|
49
|
+
- lib/ramda/logic.rb
|
50
|
+
- lib/ramda/math.rb
|
51
|
+
- lib/ramda/object.rb
|
52
|
+
- lib/ramda/relation.rb
|
53
|
+
- lib/ramda/string.rb
|
54
|
+
- lib/ramda/version.rb
|
55
|
+
- ramda-ruby.gemspec
|
56
|
+
homepage: https://github.com/lazebny/ramda-ruby
|
57
|
+
licenses:
|
58
|
+
- MIT
|
59
|
+
metadata: {}
|
60
|
+
post_install_message:
|
61
|
+
rdoc_options: []
|
62
|
+
require_paths:
|
63
|
+
- lib
|
64
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - ">"
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: 1.3.1
|
74
|
+
requirements: []
|
75
|
+
rubyforge_project:
|
76
|
+
rubygems_version: 2.5.1
|
77
|
+
signing_key:
|
78
|
+
specification_version: 4
|
79
|
+
summary: RamdaJs implementation for Ruby.
|
80
|
+
test_files: []
|