Creates an array with all falsey values removed. nil are falsey but [false, 0, and ""] values are truthy values in ruby.
array = [1, 0, nil, 'islam']
R_.compact(array) # => [1, 0, 'islam']
Creates a new array concatenating array with any additional arrays and/or values.
arr1 = [1, 2, 3]
arr2 = [4]
R_.concat(arr1, arr2) # => [1, 2, 3, 4]
R_.concat(1, { a: 1 }) # => [1, { a: 1 }]
Creates an array of array values not included in the other given arrays. The order and references of result values are determined by the first array.
arr1 = [1, 2, 3]
arr2 = [2]
arr3 = [3]
R_.difference(arr1, arr2) # => [1, 3]
R_.difference(arr1, arr2, arr3) # => [1]
Creates a slice of array with n elements dropped from the end.
array = [1, 2, 3]
R_.drop_right(array) # => [1, 2]
R_.drop_right(array, 2) # => [1]
Gets the first element of array.
array = [1, 2, 3]
R_.head(array) # => 1
empty_array = []
R_.head(empty_array) # => nil
Gets all but the last element of array.
array = [1, 2, 3]
R_.initial(array) # => [1, 2]
one_item_array = [1]
R_.initial(one_item_array) # => []
Creates an array of unique values that are included in all given arrays.
R_.intersection([2, 1], [2, 3]) # => [2]
R_.intersection([1, 2, 3], [1, 2], [2]) # => [2]
Converts all elements in array into a string separated by separator.
R_.join([1, 2, 3]) # => '1,2,3'
R_.join([1, 2, 3], '*') # => '1*2*3'
R_.join([1, 2, 3], 5) # => '15253'
R_.join(0) # => ''
Gets the last element of array.
array = [1, 2, 3]
R_.last(array) # => 3
empty_array = []
R_.last(empty_array) # => nil
Removes all elements from array that predicate returns truthy for and returns an array of the removed elements. The predicate is invoked with two arguments: (value, index).
# Simple arrays
array = [1, 2, 3, 4]
is_even = ->(v) { v.even? }
removed_items = R_.remove(array, is_even)
removed_items # => [2, 4]
array # => [1, 3]
# Array of hashes
persons = [
{ name: 'islam', sex: 'male' },
{ name: 'sabel', sex: 'female' },
{ name: 'sonia', sex: 'female' }
]
removed_persons = R_.remove(persons, { sex: 'female' })
removed_persons # => [{ name: 'sabel', sex: 'female' }, { name: 'sonia', sex: 'female' }]
persons # => [{ name: 'islam', sex: 'male' }]
Creates a slice of array from start up to, but not including, end.
R_.slice([1, 2, 3], 0, 2) # => [1, 2]
R_.slice([1, 2, 3], 1) # => [2, 3]
R_.slice([1, 2, 3], 1, 2) # => [2]
R_.slice([1, 2, 3, 4, 5, 6], 0, 3) # => [1, 2, 3]
Gets all but the first element of array.
array = [1, 2, 3]
R_.tail(array) # => [2, 3]
empty_array = []
R_.tail(empty_array) # => []
Creates a slice of array with n elements taken from the beginning.
array = [1, 2, 3]
R_.take(array) # => [1]
array = [1, 2, 3]
R_.take(array, 2) # => [1, 2]
Creates an array of unique values, in order, from all given arrays.
R_.union([2, 1], [2, 3]) # => [2, 1, 3]
# Array of hashes
arr1 = [{ a: 1 }]
arr2 = [{ a: 1 }]
arr3 = [{ a: 1, b: 2 }]
R_.union(arr1, arr2, arr3) # => [{ a: 1 }, { a: 1, b: 2 }]
Creates a duplicate-free version of an array.
array = [1,2, 2, 3, 3, 3]
R_.uniq(array) # => [1, 2, 3]
Creates an array excluding all given values.
arr1 = [1, 2, 3]
R_.without(arr1, 2) # => [1, 3]
R_.without(arr1, 2, 3) # => [1]
Iterates over elements of collection and invokes iteratee for each element. The iteratee is invoked with two arguments: (value, index|key).
# Hash
hash = {a: 1, b: 2}
sum = 0
eacher = ->(value) { sum += value }
R_.each(hash, eacher)
sum # => 3
# Array
array = [1, 2, 3]
new_array = []
eacher = ->(value) { new_array << value }
R_.each(array, eacher)
new_array # => [1, 2, 3]
This method is like R_.each except that it iterates over elements of collection from right to left.
# Hash
hash = {a: 1, b: 2}
sum = 0
eacher = ->(value) { sum += value }
R_.each_right(hash, eacher)
sum # => 3
# Array
array = [1, 2, 3]
new_array = []
eacher = ->(value) { new_array << value }
R_.each_right(array, eacher)
new_array # => [3, 2, 1]
Checks if predicate returns truthy for all elements of array.
is_even = ->(value) { value.even? }
R_.every?([1, 2, 3, 4], is_even) # => false
persons = [
{ name: 'islam', sex: 'male' },
{ name: 'sabel', sex: 'female' },
{ name: 'sonia', sex: 'female' }
]
R_.every?(persons, { sex: 'male' }) # => false
Iterates over elements of collection, returning an array of all elements predicate returns truthy for. The predicate is invoked with two arguments: [value, index|key].
# Filtering array
is_even = ->(value) { value.even? }
R_.filter([1, 2, 3, 4], is_even) # => [2, 4]
# Filtering array of hashes
persons = [
{ name: 'islam', sex: 'male' },
{ name: 'sabel', sex: 'female' },
{ name: 'sonia', sex: 'female' }
]
R_.filter(persons, { sex: 'female' })
# => [{ name: 'sabel', sex: 'female' }, { name: 'sonia', sex: 'female' }]
# Filtering hash
is_even = ->(v) { v.even? }
R_.filter({ a: 1, b: 2, c: 3, d: 4 }, is_even) # => [2, 4]
Iterates over elements of collection, returning the first element predicate returns truthy for. The predicate is invoked with two arguments: (value, index|key).
is_even = ->(value) { value.even? }
R_.find([1, 2, 3, 4], is_even) # => 2
persons = [
{ name: 'islam', sex: 'male' },
{ name: 'sabel', sex: 'female' },
{ name: 'sonia', sex: 'female' }
]
R_.find(persons, { sex: 'female' }) # => { name: 'sabel', sex: 'female' }
This method is like R_.find except that it iterates over elements of collection from right to left.
is_even = ->(value) { value.even? }
R_.find_last([1, 2, 3, 4], is_even) # => 4
persons = [
{ name: 'islam', sex: 'male' },
{ name: 'sabel', sex: 'female' },
{ name: 'sonia', sex: 'female' }
]
R_.find_last(persons, { sex: 'female' }) # => { name: 'sonia', sex: 'female' }
Creates an object composed of keys generated from the results of running each element of collection thru iteratee. The order of grouped values is determined by the order they occur in collection. The corresponding value of each key is an array of elements responsible for generating the key. The iteratee is invoked with one argument: (value).
iteratee = ->(value) { value.floor }
R_.group_by([6.1, 4.2, 6.3], iteratee); # => { '4': [4.2], '6': [6.1, 6.3] }
Creates an array of values by running each element in collection thru iteratee. The iteratee is invoked with two arguments: [value, index|key].
# Map for values
double = ->(value) { value * 2 }
R_.map([1, 2, 3], double) # => [2, 4, 6]
# Map for values and indexes
double_even_index = ->(value, index) {
index.even? ? value * 2 : value
}
R_.map([1, 2, 3, 4, 5, 6, 7, 8, 9], double_even_index) # => [2, 2, 6, 4, 10, 6, 14, 8, 18]
# Mapping Hashes
inc_by_one = ->(value) { value + 1 }
hash = { a: 1 }
R_.map(hash, inc_by_one) # => [2]
# Mapping Strings
add_e_to_even_indexes = ->(value, index) {
index.even? ? value + 'e' : value
}
R_.map('islam', add_e_to_even_indexes) # => ["ie", "s", "le", "a", "me"]
Reduces collection to a value which is the accumulated result of running each element in collection thru iteratee, where each successive invocation is supplied the return value of the previous. If accumulator is not given, the first element of collection is used as the initial value. The iteratee is invoked with four arguments: (accumulator, value, index|key).
# Array reducer
sumer = ->(acc, current) {
acc + current
}
R_.reduce([1, 2, 3, 4, 5], sumer) # => 15
# Hash reducer
hash = { a: 1, b: 2 }
hash_sumer = ->(acc, current) { acc + current }
R_.reduce(hash, hash_sumer, 0) # => 3
# String reducer
joiner = ->(acc, current) {
acc + ',' + current
}
R_.reduce('islam', joiner) # => 'i,s,l,a,m'
This method is like R_.reduce except that it iterates over elements of collection from right to left.
sumer = ->(acc, current) {
acc + current
}
R_.reduce_right([1, 2, 3, 4, 5], sumer) # => 15
hash = { a: 1, b: 2 }
hash_sumer = ->(acc, current) { acc + current }
R_.reduce_right(hash, hash_sumer, 0) # => 3
The opposite of R_.filter, this method returns the elements of collection that predicate does not return truthy for.
# Filtering array
is_even = ->(value) { value.even? }
R_.reject([1, 2, 3, 4], is_even) # => [1, 3]
# Filtering array of hashes
persons = [
{ name: 'islam', sex: 'male' },
{ name: 'sabel', sex: 'female' },
{ name: 'sonia', sex: 'female' }
]
R_.reject(persons, { sex: 'female' })
# => [{ name: 'islam', sex: 'male' }]
# Filtering hash
is_even = ->(v, _k) { v.even? }
R_.reject({ a: 1, b: 2, c: 3, d: 4 }, is_even) # => [1, 3]
Reverses array and/or string so that the first element becomes the last, the second element becomes the second to last, and so on.
R_.reverse([1, 2, 3]) # => [3, 2, 1]
R_.reverse('islam') # => 'malsi'
Gets the size of collection by returning its length for array-like values or the number of own enumerable string keyed properties for objects.
R_.size([1, 2, 3]) # => 3
R_.size({a: 1, b: 2}) # => 2
R_.size("pebbles") # => 7
Checks if predicate returns truthy for any element of collection. Iteration is stopped once predicate returns truthy. The predicate is invoked with two arguments: (value, index|key).
is_even = ->(value) { value.even? }
R_.some?([1, 2, 3, 4], is_even) # => true
persons = [
{ name: 'islam', sex: 'male' },
{ name: 'sabel', sex: 'female' },
{ name: 'sonia', sex: 'female' }
]
R_.some?(persons, { sex: 'male' }) # => true
Creates a lambda that accepts arguments of lambda and either invokes lambda returning its result, if at least arity number of arguments have been provided, or returns a lambda that accepts the remaining func arguments, and so on.
inc = ->(a, b) { a + b }
incByOne = R_.curry(inc).call(1)
incByOne.call(3) # => 4
Creates a lambda that invokes the passed lambda with arguments reversed.
subtract = ->(a, b) { a - b }
subtract.call(2, 1) # => 1
flipped_subtract = R_.flip(subtract)
flipped_subtract.call(2, 1) # => -1
Creates a lambda that negates the result of the passed lambda.
is_even = ->(value) { value.even? }
is_odd = R_.negate(is_even)
is_even.call(2) # => true
is_even.call(1) # => false
is_odd.call(1) # => true
is_odd.call(2) # => false
R_.filter([1, 2, 3, 4], is_odd) # => [1, 3]
Creates an array of values corresponding to paths of hash.
# Simple hash
hash = { a: 1, b: 2 }
R_.at(hash, ['a', 'b']) # => [1, 2]
hash = { 'a' => 1, 'b' => 2 }
R_.at(hash, ['a', 'b']) # => [1, 2]
hash = { a: { b: { c: 3 } } }
R_.at(hash, ['a.b', 'a.b.c']) # => [{ c: 3 }, 3]
Gets the value at path of object.
# Simple hash
hash = { a: 1, b: 2 }
R_.get(hash, 'a') # => 1
R_.get(hash, 'b') # => 2
# Complex hash with arrays
hash = { a: { b: { c: [{ a: 1 }] } } }
R_.get(hash, 'a.b.c.0.a') # => 1
# You can use also array [] notations
R_.get(hash, 'a.b.c[0].a') # => 1
# You can use Array paths
array_path = ['a', 'b', 'c', '0', 'a']
R_.get(hash, array_path) # => 1
# Not found case
hash = { a: 1, b: 2 }
R_.get(hash, 'a.b.c.xvx.a') # => nil
Gets the keys of hash.
hash = { a: 1, b: 2 }
R_.keys(hash) # => ['a', 'b']
Creates an hash composed of the picked hash properties.
# Simple hash with one specific path
hash = { a: 1, b: 2 }
R_.pick(hash, 'a') # => { a: 1 }
# Simple hash with multiple paths
hash = { a: 1, b: 2 }
R_.pick(hash, ['a', 'b']) # => { a: 1, b: 2 }
# Simple hash with ignoring not exist paths
hash = { a: 1, b: 2 }
R_.pick(hash, ['a', 'b', 'c.x']) # => { a: 1, b: 2 }
# Complex Hash and Array
hash = { a: 1, b: 2, c: { x: { y: [2, 1] } } }
picked = R_.pick(hash, ['c.x.y[0]', 'a'])
picked # => { a: 1, c: { x: { y: [2] } } }
picked2 = R_.pick(hash, ['c.x.y[0]', 'c.x.y[1]', 'a'])
picked2 # => { a: 1, c: { x: { y: [2, 1] } } }
Sets the value at path of object. If a portion of path doesn't exist, it's created.
# Simple hash
hash = { a: 1, b: 2 }
R_.set(hash, 'a', 2)
hash # => { a: 2, b: 2 }
# Hash path creation if not exist
hash = { a: 1, b: 2 }
R_.set(hash, 'c.x.y', 2)
hash # => { a: 1, b: 2, c: { x: { y: 2 } } }
# Hash containing array
hash = { a: 1, b: 2 }
R_.set(hash, 'c.x.y[0]', 2)
hash # => { a: 1, b: 2, c: { x: { y: [2] } } }
Removes the property at path of object.
hash = { a: 1, b: 2, c: { x: { y: 2 } } }
R_.unset(hash, 'c.x.y')
hash # => { a: 1, b: 2, c: { x: {} } }
This method is like R_.set except that accepts updater lambda to produce the value to set.
# Already exist path
hash = { a: [{ b: { c: 3 } }] };
manipulate_self = ->(n) { n * n }
R_.update(hash, 'a[0].b.c', manipulate_self)
hash # => { a: [{ b: { c: 9 } }] }
# For not exist paths
hash = { a: [{ b: { c: 3 } }] }
add_one_if_exist_else_zero = ->(n) {
n.nil? ? 0 : n + 1
}
R_.update(hash, 'x[0].y.z', add_one_if_exist_else_zero);
hash # => { a: [{ b: { c: 3 } }], x: [{ y: { z: 0 }}] }
Checks if value is classified as an Array object.
R_.array?([1, 2, 3]) # => true
R_.array?({ a: 1 }) # => false
Checks if value is an empty collection.
R_.empty?([1, 2, 3]) # => false
R_.empty?({ a: 1 }) # => false
R_.empty?([]) # => true
R_.empty?({}) # => true
Performs a reference comparison between two values to determine if they are equivalent.
hash1 = { a: 1 }
hash2 = { a: 1 }
R_.eq?(hash1, hash2) # => false
R_.eq?(hash1, hash1) # => true
Performs a deep comparison between two values to determine if they are equivalent.
hash1 = { a: { b: 2 } }
hash2 = { a: { b: 2 } }
R_.equal?(hash1, hash2) # => true
Checks if value is classified as an Hash object.
R_.hash?([1, 2, 3]) # => false
R_.hash?({ a: 1 }) # => true
Checks if value is nil.
R_.nil?(nil) # => true
R_.nil?(0) # => false
Checks if value is classified as an Numeric object.
R_.number?(56) # => true
R_.number?({ a: 1 }) # => false
Checks if value is string.
R_.string?(nil) # => false
R_.string?('str') # => true
Creates a Rudash wrapper instance that wraps value with explicit method chain sequences enabled. The result of such sequences must be unwrapped with value().
filter_even = ->(number) { number.even? }
inc_by_one = ->(number) { number + 1 }
# You can chain using all Rudash functions.
# Pay attention that we are not sending the data ([1, 2, 3, 4])
# in the arguments since it is been sent by the wrapped value
R_.chain([1, 2, 3, 4])
.filter(filter_even)
.map(inc_by_one)
.head()
.value() # => 3
Converts the first character of string to upper case and the remaining to lower case.
R_.capitalize(nil) # => ''
R_.capitalize(4) # =>'4'
R_.capitalize('fiverr') # =>'Fiverr'
Checks if string ends with the given target string.
R_.ends_with?('islam', 'am') # => true
R_.ends_with?('islam1', 1) # => true
R_.ends_with?('islam', 'xyz') # => false
Creates a lambda that returns the result of invoking the given lambdas with the this binding of the created lambda, where each successive invocation is supplied the return value of the previous.
inc_two_numbers = ->(num1, num2) { num1 + num2 }
square = ->(number) { number * number }
double = ->(number) { number * 2 }
composed = R_.flow([inc_two_numbers, square, double])
composed.call(10, 20) # => 1800
This method is like R_.flow except that it creates a lambda that invokes the given lambdas from right to left.
square = ->(number) { number * number }
double = ->(number) { number * 2 }
composed = R_.flow([square, double])
composed.call(10, 20) # => 400
This method returns the first argument it receives.
R_.identity(0) # => 0
R_.identity(1, 2) # => 1
Creates an array of numbers (positive and/or negative) progressing from start up to, but not including, end. A step of -1 is used if a negative start is specified without an end or step. If end is not specified, it's set to start with start then set to 0.
R_.range(4) # => [0, 1, 2, 3]
R_.range(-4) # => [0, -1, -2, -3]
R_.range(1, 5) # => [1, 2, 3, 4]
R_.range(0, 20, 5) # => [0, 5, 10, 15]
R_.range(0, -4, -1) # => [0, -1, -2, -3]
R_.range(1, 4, 0) # => [1, 1, 1]
R_.range(0) # => []