Module: FunRuby::Hash

Extended by:
Hash
Includes:
Common::Helpers
Included in:
Hash
Defined in:
lib/fun_ruby/hash.rb

Overview

Module containing methods for hashes

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.compact(keys = F._, hash = F._) ⇒ Object

Returns a new hash without all key-valued pairs

Examples:

Base

hash = { name: "John", age: 18, email: nil, country: nil }
F::Hash.compact(hash) # => { name: "John", age: 18 }

Parameters:

  • hash (#to_h) (defaults to: F._)

Since:

  • 0.1.0



407
408
409
# File 'lib/fun_ruby/hash.rb', line 407

def compact(keys = F._, hash = F._)
  curry_implementation(:compact, keys, hash)
end

.dig(keys = F._, hash = F._) ⇒ ::Array[Object]

Returns a value stored by a given chain of keys. If a value of any level key of the chain is not found nil is returned.

Examples:

Base

hash = { a: { b: { c: 3 } } }
F::Hash.dig([:a], hash) # => { b: { c: 3 } }
F::Hash.dig([:a, :b], hash) # => { c: 3 }
F::Hash.dig([:a, :b, :c], hash) # => 3
F::Hash.dig([:foo], hash) # => nil
F::Hash.dig([:a, :foo], hash) # => nil
F::Hash.dig([:a, :b, :foo], hash) # => nil

Parameters:

  • keys (::Array of (#hash, #eql?)) (defaults to: F._)
  • hash (#to_h) (defaults to: F._)

Returns:

  • (::Array[Object])

Since:

  • 0.1.0



373
374
375
# File 'lib/fun_ruby/hash.rb', line 373

def dig(keys = F._, hash = F._)
  curry_implementation(:dig, keys, hash)
end

.dig!(keys = F._, hash = F._) ⇒ ::Array[Object]

Returns a value stored by a given chain of keys. If a value of any level key of the chain is not found KeyError is raised

Examples:

Base

hash = { a: { b: { c: 3 } } }
F::Hash.dig!([:a], hash) # => { b: { c: 3 } }
F::Hash.dig!([:a, :b], hash) # => { c: 3 }
F::Hash.dig!([:a, :b, :c], hash) # => 3
F::Hash.dig!([:foo], hash) # => raise KeyError, "key not found: :foo"
F::Hash.dig!([:a, :foo], hash) # => raise KeyError, "key not found: :foo"
F::Hash.dig!([:a, :b, :foo], hash) # => raise KeyError, "key not found: :foo"

Parameters:

  • keys (::Array of (#hash, #eql?)) (defaults to: F._)
  • hash (#to_h) (defaults to: F._)

Returns:

  • (::Array[Object])

Since:

  • 0.1.0



394
395
396
# File 'lib/fun_ruby/hash.rb', line 394

def dig!(keys = F._, hash = F._)
  curry_implementation(:dig!, keys, hash)
end

.fetch(key = F._, hash = F._) ⇒ Object

Returns a value stored by a given key. If there is no given key it raises error

Examples:

Base

hash = { age: 20, name: 'John' }
F::Hash.fetch(:age, hash) # => 20
F::Hash.fetch(:name, hash) # => 'John'
F::Hash.fetch(:email, hash) # => raise KeyError, "key not found: :email"

Curried

hash = { age: 20, name: 'John' }
curried = F::Hash.fetch
curried.(:age, hash) # => 20
curried.(:name).(hash) # => 'John'
curried.(:email).(hash) # => raise KeyError, "key not found: :email"

Curried with placeholders

hash = { age: 20, name: 'John' }
curried = F::Hash.fetch(F._, hash)
curried.(:age) # => 20
curried.(:name) # => 'John'

Parameters:

  • key (#hash, #eql?) (defaults to: F._)
  • hash (#to_h) (defaults to: F._)

Returns:

  • (Object)

Raises:

  • KeyError

Since:

  • 0.1.0



77
78
79
# File 'lib/fun_ruby/hash.rb', line 77

def fetch(key = F._, hash = F._)
  curry_implementation(:fetch, key, hash)
end

.fetch_values(keys = F._, hash = F._) ⇒ ::Array[Object]

Returns an array of values stored by given keys If any key is missed in the given hash KeyError is raised with the first missed key

Examples:

Base

hash = { name: "John", age: 20, country: "USA" }

F::Hash.fetch_values([:name, :age, :country], hash) # => ["John", 20, "USA"]
F::Hash.fetch_values([:address, :email], hash) # => raise KeyError, "key not found: :address"

Parameters:

  • keys (::Array of (#hash, #eql?)) (defaults to: F._)
  • hash (#to_h) (defaults to: F._)

Returns:

  • (::Array[Object])

Since:

  • 0.1.0



265
266
267
# File 'lib/fun_ruby/hash.rb', line 265

def fetch_values(keys = F._, hash = F._)
  curry_implementation(:fetch_values, keys, hash)
end

.fetch_with(key = F._, fallback = F._, hash = F._) ⇒ Object

Returns a value stored by a given key. If there is no given key it calls a given fallback.

Examples:

Base

hash = { age: 20, name: 'John' }
F::Hash.fetch_with(:age, ->(_) { :fallback }, hash) # => 20
F::Hash.fetch_with(:name, ->(_) { :fallback }, hash) # => 'John'
F::Hash.fetch_with(:email, ->(_) { :fallback }, hash) # => :fallback

Curried

hash = { age: 20, name: 'John' }
curried = F::Hash.fetch_with
curried.(:age, ->(_) { :fallback }, hash) # => 20
curried.(:name).(->(_) { :fallback }).(hash) # => 'John'
curried.(:email).(->(_) { :fallback }).(hash) # => :fallback

Curried with placeholders

hash = { age: 20, name: 'John' }
curried = F::Hash.fetch_with(F._, F._, F._)
curried.(:age).(->(_) { :fallback }).(hash) # => 20

curried = F::Hash.fetch_with(F._, ->(_) { :fallback }, F._)
curried.(:age).(hash) # => 20
curried.(:email).(hash) # => :fallback

Parameters:

  • key (#hash, #eql?) (defaults to: F._)
  • fallback (#to_call) (defaults to: F._)
  • hash (#to_h) (defaults to: F._)

Returns:

  • (Object)

Since:

  • 0.1.0



113
114
115
# File 'lib/fun_ruby/hash.rb', line 113

def fetch_with(key = F._, fallback = F._, hash = F._)
  curry_implementation(:fetch_with, key, fallback, hash)
end

.get(key = F._, hash = F._) ⇒ Object

Returns a value stored by a given key. If there is no given key it return a default value

Examples:

Base

hash = { age: 20, name: 'John' }
F::Hash.get(:age, hash) # => 20
F::Hash.get(:name, hash) # => 'John'
F::Hash.get(:email, hash) # => nil

Curried

hash = { age: 20, name: 'John' }
curried = F::Hash.get
curried.(:age, hash) # => 20
curried.(:name).(hash) # => 'John'

Curried

hash = { age: 20, name: 'John' }
curried = F::Hash.get
curried.(:age, hash) # => 20
curried.(:name).(hash) # => 'John'

Curried with placeholders

hash = { age: 20, name: 'John' }
curried = F::Hash.get(F._, hash)
curried.(:age) # => 20
curried.(:name) # => 'John'

Parameters:

  • key (#hash, #eql?) (defaults to: F._)
  • hash (#to_h) (defaults to: F._)

Returns:

  • (Object)

Since:

  • 0.1.0



43
44
45
# File 'lib/fun_ruby/hash.rb', line 43

def get(key = F._, hash = F._)
  curry_implementation(:get, key, hash)
end

.keys(hash = F._) ⇒ ::Array[Object]

Returns an array of all stored keys

Examples:

Base

F::Hash.keys({}) # => []
F::Hash.keys({ age: 33 }) # => [:age]
F::Hash.keys({ age: 33, name: "John"}) # => [:age, :name]

Parameters:

  • hash (#to_h) (defaults to: F._)

Returns:

  • (::Array[Object])

Since:

  • 0.1.0



316
317
318
# File 'lib/fun_ruby/hash.rb', line 316

def keys(hash = F._)
  curry_implementation(:keys, hash)
end

.merge(first = F._, second = F._) ⇒ Hash

Merges two hashes. The key-value pairs from the second to the first. If the key is already taken the current value will be replaced.

Examples:

Base

first = { name: "John" }
second = { age: 20 }
F::Hash.merge(first, second) # => { name: "John", age: 20 }

first = { name: "John" }
second = { name: "Bill", age: 20 }
F::Hash.merge(first, second) # => { name: "Bill", age: 20 }

Curried

first = { name: "John" }
second = { age: 20 }
curried = F::Hash.merge
curried.(first).(second) # => { name: "John", age: 20 }

first = { name: "John" }
second = { name: "Bill", age: 20 }
curried = F::Hash.merge
curried.(first).(second) # => { name: "Bill", age: 20 }

Curried with placeholders

first = { name: "John" }
second = { age: 20 }
curried = F::Hash.merge(F._, F._)
curried.(first).(second) # => { name: "John", age: 20 }

first = { name: "John" }
second = { name: "Bill", age: 20 }
curried = F::Hash.merge(F._, second)
curried.(first) # => { name: "Bill", age: 20 }

Parameters:

  • first (#to_h) (defaults to: F._)
  • second (#to_h) (defaults to: F._)

Returns:

Since:

  • 0.1.0



193
194
195
# File 'lib/fun_ruby/hash.rb', line 193

def merge(first = F._, second = F._)
  curry_implementation(:merge, first, second)
end

.put(key = F._, value = F._, hash = F._) ⇒ Hash

Puts a value by a given key. If the key is already taken the current value will be replaced.

Examples:

Base

hash = { name: "John" }
F::Hash.put(:age, 20, hash) # => { name: "John", age: 20 }
F::Hash.put(:name, "Peter", hash) # => { name: "Peter" }

Curried

hash = { name: "John" }
curried = F::Hash.put
curried.(:age).(20).(hash) # => { name: "John", age: 20 }
curried.(:name).("Peter").(hash) # => { name: "Peter" }

Curried with placeholders

hash = { name: "John" }
curried = F::Hash.put(F._, 20, hash)
curried.(:age) # => { name: "John", age: 20 }

curried = F::Hash.put(F._, 20, F._)
curried.(:age).(hash) # => { name: "John", age: 20 }

curried = F::Hash.put(:age, F._, hash)
curried.(20) # => { name: "John", age: 20 }

Parameters:

  • key (#hash, #eql?) (defaults to: F._)
  • value (Object) (defaults to: F._)
  • hash (#to_h) (defaults to: F._)

Returns:

Since:

  • 0.1.0



149
150
151
# File 'lib/fun_ruby/hash.rb', line 149

def put(key = F._, value = F._, hash = F._)
  curry_implementation(:put, key, value, hash)
end

.reject(function = F._, hash = F._) ⇒ ::Array[Object]

Returns a new hash excluding pairs for which a given function returns true

Examples:

Base

hash = { 'a' => 1, 'b' => 2, :c => 3, :d => 4 }
F::Hash.reject(->(key, _value) { key.is_a?(String) }, hash) # => { :c => 3, :d => 4 }
F::Hash.reject(->(_key, value) { value.odd? }, hash) # => { 'b' => 2, :d =>4 }

Parameters:

  • function (#call/2) (defaults to: F._)
  • hash (#to_h) (defaults to: F._)

Returns:

  • (::Array[Object])

Since:

  • 0.1.0



352
353
354
# File 'lib/fun_ruby/hash.rb', line 352

def reject(function = F._, hash = F._)
  curry_implementation(:reject, function, hash)
end

.select(function = F._, hash = F._) ⇒ ::Array[Object]

Returns a new hash containing only pairs for which a given function returns true

Examples:

Base

hash = { 'a' => 1, 'b' => 2, :c => 3, :d => 4 }
F::Hash.select(->(key, _value) { key.is_a?(String) }, hash) # => { 'a' => 1, 'b' => 2 }
F::Hash.select(->(_key, value) { value.odd? }, hash) # => { 'a' => 1, :c => 3 }

Parameters:

  • function (#call/2) (defaults to: F._)
  • hash (#to_h) (defaults to: F._)

Returns:

  • (::Array[Object])

Since:

  • 0.1.0



334
335
336
# File 'lib/fun_ruby/hash.rb', line 334

def select(function = F._, hash = F._)
  curry_implementation(:select, function, hash)
end

.slice(keys = F._, hash = F._) ⇒ Hash

Builds a new hash with only given keys from a given hash. If keys are missed in the given hash the corresponded key will be absent in the result hash.

Examples:

Base

hash = { name: "John", age: 20, email: "[email protected]", country: "USA" }

F::Hash.slice([:name, :age, :country], hash) # => { name: "John", age: 20, country: "USA" }
F::Hash.slice([:name, :age, :address], hash) # => { name: "John", age: 20 }

Curried

hash = { name: "John", age: 20, email: "[email protected]", country: "USA" }
curried = F::Hash.slice

curried.([:name, :age, :country]).(hash) # => { name: "John", age: 20, country: "USA" }
curried.([:name, :age, :address]).(hash) # => { name: "John", age: 20 }

Curried with placeholders

hash = { name: "John", age: 20, email: "[email protected]", country: "USA" }

curried = F::Hash.slice(F._, hash)
curried.([:name, :age, :country]) # => { name: "John", age: 20, country: "USA" }
curried.([:name, :age, :address]) # => { name: "John", age: 20 }

Parameters:

  • keys (::Array of (#hash, #eql?)) (defaults to: F._)
  • hash (#to_h) (defaults to: F._)

Returns:

Since:

  • 0.1.0



227
228
229
# File 'lib/fun_ruby/hash.rb', line 227

def slice(keys = F._, hash = F._)
  curry_implementation(:slice, keys, hash)
end

.slice!(keys = F._, hash = F._) ⇒ Hash

Builds a new hash with only given keys from a given hash. If keys are missed in the given hash KeyError is raised

Examples:

Base

hash = { name: "John", age: 20, country: "USA" }

F::Hash.slice!([:name, :age, :country], hash) # => { name: "John", age: 20, country: "USA" }
F::Hash.slice!([:address, :email], hash) # => raise KeyError, "keys not found: [:address, :email]"

Parameters:

  • keys (::Array of (#hash, #eql?)) (defaults to: F._)
  • hash (#to_h) (defaults to: F._)

Returns:

Since:

  • 0.1.0



246
247
248
# File 'lib/fun_ruby/hash.rb', line 246

def slice!(keys = F._, hash = F._)
  curry_implementation(:slice!, keys, hash)
end

.transform_keys(function = F._, hash = F._) ⇒ Object

Returns a new hash with updated keys calculated as a result returned from a given function

Examples:

Base

hash = { a: 1, b: 2, c: 3 }
F::Hash.transform_keys(->(key) { key.to_s }, hash) #=> { 'a' => 1, 'b' => 2, 'c' => 3 }

Parameters:

  • function (#call/1) (defaults to: F._)
  • hash (#to_h) (defaults to: F._)

Since:

  • 0.1.0



422
423
424
# File 'lib/fun_ruby/hash.rb', line 422

def transform_keys(function = F._, hash = F._)
  curry_implementation(:transform_keys, function, hash)
end

.transform_values(function = F._, hash = F._) ⇒ Object

Returns a new hash with updated values calculated as a result returned from a given function

Examples:

Base

hash = { a: 1, b: 2, c: 3 }
F::Hash.transform_values(->(value) { value.to_s }, hash) #=> { a: '1', b: '2', c: '3' }

Parameters:

  • function (#call/1) (defaults to: F._)
  • hash (#to_h) (defaults to: F._)

Since:

  • 0.1.0



437
438
439
# File 'lib/fun_ruby/hash.rb', line 437

def transform_values(function = F._, hash = F._)
  curry_implementation(:transform_values, function, hash)
end

.values(hash = F._) ⇒ ::Array[Object]

Returns an array of all stored values

Examples:

Base

F::Hash.values({}) # => []
F::Hash.values({ age: 33 }) # => [33]
F::Hash.values({ age: 33, name: "John"}) # => [33, "John"]

Parameters:

  • hash (#to_h) (defaults to: F._)

Returns:

  • (::Array[Object])

Since:

  • 0.1.0



300
301
302
# File 'lib/fun_ruby/hash.rb', line 300

def values(hash = F._)
  curry_implementation(:values, hash)
end

.values_at(keys = F._, hash = F._) ⇒ ::Array[Object]

Returns an array of values stored by given keys If a key is missed the default value is returned

Examples:

Base

hash = { name: "John", age: 20, country: "USA" }

F::Hash.values_at([:name, :age, :country], hash) # => ["John", 20, "USA"]
F::Hash.values_at([:address, :email, :country, :age], hash) # => [nil, nil, "USA", 20]

Parameters:

  • keys (::Array of (#hash, #eql?)) (defaults to: F._)
  • hash (#to_h) (defaults to: F._)

Returns:

  • (::Array[Object])

Since:

  • 0.1.0



284
285
286
# File 'lib/fun_ruby/hash.rb', line 284

def values_at(keys = F._, hash = F._)
  curry_implementation(:values_at, keys, hash)
end

Instance Method Details

#compact(keys = F._, hash = F._) ⇒ Object

Returns a new hash without all key-valued pairs

Examples:

Base

hash = { name: "John", age: 18, email: nil, country: nil }
F::Hash.compact(hash) # => { name: "John", age: 18 }

Parameters:

  • hash (#to_h) (defaults to: F._)

Since:

  • 0.1.0



407
408
409
# File 'lib/fun_ruby/hash.rb', line 407

def compact(keys = F._, hash = F._)
  curry_implementation(:compact, keys, hash)
end

#dig(keys = F._, hash = F._) ⇒ ::Array[Object]

Returns a value stored by a given chain of keys. If a value of any level key of the chain is not found nil is returned.

Examples:

Base

hash = { a: { b: { c: 3 } } }
F::Hash.dig([:a], hash) # => { b: { c: 3 } }
F::Hash.dig([:a, :b], hash) # => { c: 3 }
F::Hash.dig([:a, :b, :c], hash) # => 3
F::Hash.dig([:foo], hash) # => nil
F::Hash.dig([:a, :foo], hash) # => nil
F::Hash.dig([:a, :b, :foo], hash) # => nil

Parameters:

  • keys (::Array of (#hash, #eql?)) (defaults to: F._)
  • hash (#to_h) (defaults to: F._)

Returns:

  • (::Array[Object])

Since:

  • 0.1.0



373
374
375
# File 'lib/fun_ruby/hash.rb', line 373

def dig(keys = F._, hash = F._)
  curry_implementation(:dig, keys, hash)
end

#dig!(keys = F._, hash = F._) ⇒ ::Array[Object]

Returns a value stored by a given chain of keys. If a value of any level key of the chain is not found KeyError is raised

Examples:

Base

hash = { a: { b: { c: 3 } } }
F::Hash.dig!([:a], hash) # => { b: { c: 3 } }
F::Hash.dig!([:a, :b], hash) # => { c: 3 }
F::Hash.dig!([:a, :b, :c], hash) # => 3
F::Hash.dig!([:foo], hash) # => raise KeyError, "key not found: :foo"
F::Hash.dig!([:a, :foo], hash) # => raise KeyError, "key not found: :foo"
F::Hash.dig!([:a, :b, :foo], hash) # => raise KeyError, "key not found: :foo"

Parameters:

  • keys (::Array of (#hash, #eql?)) (defaults to: F._)
  • hash (#to_h) (defaults to: F._)

Returns:

  • (::Array[Object])

Since:

  • 0.1.0



394
395
396
# File 'lib/fun_ruby/hash.rb', line 394

def dig!(keys = F._, hash = F._)
  curry_implementation(:dig!, keys, hash)
end

#fetch(key = F._, hash = F._) ⇒ Object

Returns a value stored by a given key. If there is no given key it raises error

Examples:

Base

hash = { age: 20, name: 'John' }
F::Hash.fetch(:age, hash) # => 20
F::Hash.fetch(:name, hash) # => 'John'
F::Hash.fetch(:email, hash) # => raise KeyError, "key not found: :email"

Curried

hash = { age: 20, name: 'John' }
curried = F::Hash.fetch
curried.(:age, hash) # => 20
curried.(:name).(hash) # => 'John'
curried.(:email).(hash) # => raise KeyError, "key not found: :email"

Curried with placeholders

hash = { age: 20, name: 'John' }
curried = F::Hash.fetch(F._, hash)
curried.(:age) # => 20
curried.(:name) # => 'John'

Parameters:

  • key (#hash, #eql?) (defaults to: F._)
  • hash (#to_h) (defaults to: F._)

Returns:

  • (Object)

Raises:

  • KeyError

Since:

  • 0.1.0



77
78
79
# File 'lib/fun_ruby/hash.rb', line 77

def fetch(key = F._, hash = F._)
  curry_implementation(:fetch, key, hash)
end

#fetch_values(keys = F._, hash = F._) ⇒ ::Array[Object]

Returns an array of values stored by given keys If any key is missed in the given hash KeyError is raised with the first missed key

Examples:

Base

hash = { name: "John", age: 20, country: "USA" }

F::Hash.fetch_values([:name, :age, :country], hash) # => ["John", 20, "USA"]
F::Hash.fetch_values([:address, :email], hash) # => raise KeyError, "key not found: :address"

Parameters:

  • keys (::Array of (#hash, #eql?)) (defaults to: F._)
  • hash (#to_h) (defaults to: F._)

Returns:

  • (::Array[Object])

Since:

  • 0.1.0



265
266
267
# File 'lib/fun_ruby/hash.rb', line 265

def fetch_values(keys = F._, hash = F._)
  curry_implementation(:fetch_values, keys, hash)
end

#fetch_with(key = F._, fallback = F._, hash = F._) ⇒ Object

Returns a value stored by a given key. If there is no given key it calls a given fallback.

Examples:

Base

hash = { age: 20, name: 'John' }
F::Hash.fetch_with(:age, ->(_) { :fallback }, hash) # => 20
F::Hash.fetch_with(:name, ->(_) { :fallback }, hash) # => 'John'
F::Hash.fetch_with(:email, ->(_) { :fallback }, hash) # => :fallback

Curried

hash = { age: 20, name: 'John' }
curried = F::Hash.fetch_with
curried.(:age, ->(_) { :fallback }, hash) # => 20
curried.(:name).(->(_) { :fallback }).(hash) # => 'John'
curried.(:email).(->(_) { :fallback }).(hash) # => :fallback

Curried with placeholders

hash = { age: 20, name: 'John' }
curried = F::Hash.fetch_with(F._, F._, F._)
curried.(:age).(->(_) { :fallback }).(hash) # => 20

curried = F::Hash.fetch_with(F._, ->(_) { :fallback }, F._)
curried.(:age).(hash) # => 20
curried.(:email).(hash) # => :fallback

Parameters:

  • key (#hash, #eql?) (defaults to: F._)
  • fallback (#to_call) (defaults to: F._)
  • hash (#to_h) (defaults to: F._)

Returns:

  • (Object)

Since:

  • 0.1.0



113
114
115
# File 'lib/fun_ruby/hash.rb', line 113

def fetch_with(key = F._, fallback = F._, hash = F._)
  curry_implementation(:fetch_with, key, fallback, hash)
end

#get(key = F._, hash = F._) ⇒ Object

Returns a value stored by a given key. If there is no given key it return a default value

Examples:

Base

hash = { age: 20, name: 'John' }
F::Hash.get(:age, hash) # => 20
F::Hash.get(:name, hash) # => 'John'
F::Hash.get(:email, hash) # => nil

Curried

hash = { age: 20, name: 'John' }
curried = F::Hash.get
curried.(:age, hash) # => 20
curried.(:name).(hash) # => 'John'

Curried

hash = { age: 20, name: 'John' }
curried = F::Hash.get
curried.(:age, hash) # => 20
curried.(:name).(hash) # => 'John'

Curried with placeholders

hash = { age: 20, name: 'John' }
curried = F::Hash.get(F._, hash)
curried.(:age) # => 20
curried.(:name) # => 'John'

Parameters:

  • key (#hash, #eql?) (defaults to: F._)
  • hash (#to_h) (defaults to: F._)

Returns:

  • (Object)

Since:

  • 0.1.0



43
44
45
# File 'lib/fun_ruby/hash.rb', line 43

def get(key = F._, hash = F._)
  curry_implementation(:get, key, hash)
end

#keys(hash = F._) ⇒ ::Array[Object]

Returns an array of all stored keys

Examples:

Base

F::Hash.keys({}) # => []
F::Hash.keys({ age: 33 }) # => [:age]
F::Hash.keys({ age: 33, name: "John"}) # => [:age, :name]

Parameters:

  • hash (#to_h) (defaults to: F._)

Returns:

  • (::Array[Object])

Since:

  • 0.1.0



316
317
318
# File 'lib/fun_ruby/hash.rb', line 316

def keys(hash = F._)
  curry_implementation(:keys, hash)
end

#merge(first = F._, second = F._) ⇒ Hash

Merges two hashes. The key-value pairs from the second to the first. If the key is already taken the current value will be replaced.

Examples:

Base

first = { name: "John" }
second = { age: 20 }
F::Hash.merge(first, second) # => { name: "John", age: 20 }

first = { name: "John" }
second = { name: "Bill", age: 20 }
F::Hash.merge(first, second) # => { name: "Bill", age: 20 }

Curried

first = { name: "John" }
second = { age: 20 }
curried = F::Hash.merge
curried.(first).(second) # => { name: "John", age: 20 }

first = { name: "John" }
second = { name: "Bill", age: 20 }
curried = F::Hash.merge
curried.(first).(second) # => { name: "Bill", age: 20 }

Curried with placeholders

first = { name: "John" }
second = { age: 20 }
curried = F::Hash.merge(F._, F._)
curried.(first).(second) # => { name: "John", age: 20 }

first = { name: "John" }
second = { name: "Bill", age: 20 }
curried = F::Hash.merge(F._, second)
curried.(first) # => { name: "Bill", age: 20 }

Parameters:

  • first (#to_h) (defaults to: F._)
  • second (#to_h) (defaults to: F._)

Returns:

Since:

  • 0.1.0



193
194
195
# File 'lib/fun_ruby/hash.rb', line 193

def merge(first = F._, second = F._)
  curry_implementation(:merge, first, second)
end

#put(key = F._, value = F._, hash = F._) ⇒ Hash

Puts a value by a given key. If the key is already taken the current value will be replaced.

Examples:

Base

hash = { name: "John" }
F::Hash.put(:age, 20, hash) # => { name: "John", age: 20 }
F::Hash.put(:name, "Peter", hash) # => { name: "Peter" }

Curried

hash = { name: "John" }
curried = F::Hash.put
curried.(:age).(20).(hash) # => { name: "John", age: 20 }
curried.(:name).("Peter").(hash) # => { name: "Peter" }

Curried with placeholders

hash = { name: "John" }
curried = F::Hash.put(F._, 20, hash)
curried.(:age) # => { name: "John", age: 20 }

curried = F::Hash.put(F._, 20, F._)
curried.(:age).(hash) # => { name: "John", age: 20 }

curried = F::Hash.put(:age, F._, hash)
curried.(20) # => { name: "John", age: 20 }

Parameters:

  • key (#hash, #eql?) (defaults to: F._)
  • value (Object) (defaults to: F._)
  • hash (#to_h) (defaults to: F._)

Returns:

Since:

  • 0.1.0



149
150
151
# File 'lib/fun_ruby/hash.rb', line 149

def put(key = F._, value = F._, hash = F._)
  curry_implementation(:put, key, value, hash)
end

#reject(function = F._, hash = F._) ⇒ ::Array[Object]

Returns a new hash excluding pairs for which a given function returns true

Examples:

Base

hash = { 'a' => 1, 'b' => 2, :c => 3, :d => 4 }
F::Hash.reject(->(key, _value) { key.is_a?(String) }, hash) # => { :c => 3, :d => 4 }
F::Hash.reject(->(_key, value) { value.odd? }, hash) # => { 'b' => 2, :d =>4 }

Parameters:

  • function (#call/2) (defaults to: F._)
  • hash (#to_h) (defaults to: F._)

Returns:

  • (::Array[Object])

Since:

  • 0.1.0



352
353
354
# File 'lib/fun_ruby/hash.rb', line 352

def reject(function = F._, hash = F._)
  curry_implementation(:reject, function, hash)
end

#select(function = F._, hash = F._) ⇒ ::Array[Object]

Returns a new hash containing only pairs for which a given function returns true

Examples:

Base

hash = { 'a' => 1, 'b' => 2, :c => 3, :d => 4 }
F::Hash.select(->(key, _value) { key.is_a?(String) }, hash) # => { 'a' => 1, 'b' => 2 }
F::Hash.select(->(_key, value) { value.odd? }, hash) # => { 'a' => 1, :c => 3 }

Parameters:

  • function (#call/2) (defaults to: F._)
  • hash (#to_h) (defaults to: F._)

Returns:

  • (::Array[Object])

Since:

  • 0.1.0



334
335
336
# File 'lib/fun_ruby/hash.rb', line 334

def select(function = F._, hash = F._)
  curry_implementation(:select, function, hash)
end

#slice(keys = F._, hash = F._) ⇒ Hash

Builds a new hash with only given keys from a given hash. If keys are missed in the given hash the corresponded key will be absent in the result hash.

Examples:

Base

hash = { name: "John", age: 20, email: "[email protected]", country: "USA" }

F::Hash.slice([:name, :age, :country], hash) # => { name: "John", age: 20, country: "USA" }
F::Hash.slice([:name, :age, :address], hash) # => { name: "John", age: 20 }

Curried

hash = { name: "John", age: 20, email: "[email protected]", country: "USA" }
curried = F::Hash.slice

curried.([:name, :age, :country]).(hash) # => { name: "John", age: 20, country: "USA" }
curried.([:name, :age, :address]).(hash) # => { name: "John", age: 20 }

Curried with placeholders

hash = { name: "John", age: 20, email: "[email protected]", country: "USA" }

curried = F::Hash.slice(F._, hash)
curried.([:name, :age, :country]) # => { name: "John", age: 20, country: "USA" }
curried.([:name, :age, :address]) # => { name: "John", age: 20 }

Parameters:

  • keys (::Array of (#hash, #eql?)) (defaults to: F._)
  • hash (#to_h) (defaults to: F._)

Returns:

Since:

  • 0.1.0



227
228
229
# File 'lib/fun_ruby/hash.rb', line 227

def slice(keys = F._, hash = F._)
  curry_implementation(:slice, keys, hash)
end

#slice!(keys = F._, hash = F._) ⇒ Hash

Builds a new hash with only given keys from a given hash. If keys are missed in the given hash KeyError is raised

Examples:

Base

hash = { name: "John", age: 20, country: "USA" }

F::Hash.slice!([:name, :age, :country], hash) # => { name: "John", age: 20, country: "USA" }
F::Hash.slice!([:address, :email], hash) # => raise KeyError, "keys not found: [:address, :email]"

Parameters:

  • keys (::Array of (#hash, #eql?)) (defaults to: F._)
  • hash (#to_h) (defaults to: F._)

Returns:

Since:

  • 0.1.0



246
247
248
# File 'lib/fun_ruby/hash.rb', line 246

def slice!(keys = F._, hash = F._)
  curry_implementation(:slice!, keys, hash)
end

#transform_keys(function = F._, hash = F._) ⇒ Object

Returns a new hash with updated keys calculated as a result returned from a given function

Examples:

Base

hash = { a: 1, b: 2, c: 3 }
F::Hash.transform_keys(->(key) { key.to_s }, hash) #=> { 'a' => 1, 'b' => 2, 'c' => 3 }

Parameters:

  • function (#call/1) (defaults to: F._)
  • hash (#to_h) (defaults to: F._)

Since:

  • 0.1.0



422
423
424
# File 'lib/fun_ruby/hash.rb', line 422

def transform_keys(function = F._, hash = F._)
  curry_implementation(:transform_keys, function, hash)
end

#transform_values(function = F._, hash = F._) ⇒ Object

Returns a new hash with updated values calculated as a result returned from a given function

Examples:

Base

hash = { a: 1, b: 2, c: 3 }
F::Hash.transform_values(->(value) { value.to_s }, hash) #=> { a: '1', b: '2', c: '3' }

Parameters:

  • function (#call/1) (defaults to: F._)
  • hash (#to_h) (defaults to: F._)

Since:

  • 0.1.0



437
438
439
# File 'lib/fun_ruby/hash.rb', line 437

def transform_values(function = F._, hash = F._)
  curry_implementation(:transform_values, function, hash)
end

#values(hash = F._) ⇒ ::Array[Object]

Returns an array of all stored values

Examples:

Base

F::Hash.values({}) # => []
F::Hash.values({ age: 33 }) # => [33]
F::Hash.values({ age: 33, name: "John"}) # => [33, "John"]

Parameters:

  • hash (#to_h) (defaults to: F._)

Returns:

  • (::Array[Object])

Since:

  • 0.1.0



300
301
302
# File 'lib/fun_ruby/hash.rb', line 300

def values(hash = F._)
  curry_implementation(:values, hash)
end

#values_at(keys = F._, hash = F._) ⇒ ::Array[Object]

Returns an array of values stored by given keys If a key is missed the default value is returned

Examples:

Base

hash = { name: "John", age: 20, country: "USA" }

F::Hash.values_at([:name, :age, :country], hash) # => ["John", 20, "USA"]
F::Hash.values_at([:address, :email, :country, :age], hash) # => [nil, nil, "USA", 20]

Parameters:

  • keys (::Array of (#hash, #eql?)) (defaults to: F._)
  • hash (#to_h) (defaults to: F._)

Returns:

  • (::Array[Object])

Since:

  • 0.1.0



284
285
286
# File 'lib/fun_ruby/hash.rb', line 284

def values_at(keys = F._, hash = F._)
  curry_implementation(:values_at, keys, hash)
end