Module: FunRuby::Enum

Extended by:
Enum
Includes:
Common::Helpers
Included in:
Enum
Defined in:
lib/fun_ruby/enum.rb

Overview

Module containing methods for enumerables

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.all?(function = F._, enumerable = F._) ⇒ Boolean

Returns a boolean which represents if all results of applying a function to each element are truthy

Examples:

Basic returning true

F::Enum.all?(->(x) { x % 2 == 0 }, [2, 4, 6]) #=> true

Basic returning false

F::Enum.all?(->(x) { x % 2 == 0 }, [2, 5, 6]) #=> false

Curried

curried = F::Enum.all?
curried.(->(x) { x % 2 == 0 }).([1, 2, 3]) #=> false

Curried with placeholder

curried = F::Enum.all?(F._, [2, 4, 6])
curried.(->(x) { x % 2 == 0 }) # => true

Parameters:

  • function (#call/1) (defaults to: F._)
  • enumerable (#to_enum) (defaults to: F._)

Returns:

  • (Boolean)

Since:

  • 0.1.0



35
36
37
# File 'lib/fun_ruby/enum.rb', line 35

def all?(function = F._, enumerable = F._)
  curry_implementation(:all?, function, enumerable)
end

.count(function = F._, enumerable = F._) ⇒ Integer

Goes through a given enumerable and count the amount of elements for which a passed function returned true

Examples:

Basic: count odd elements

F::Enum.count(->(x) { x % 2 == 1 }, [1, 2, 3]) #=> 2

Curried: count odd elements

curried = F::Enum.count
curried.(->(x) { x % 2 == 1 }).([1, 2, 3]) #=> 2

Curried with placeholder: count odd elements

curried = F::Enum.count(F._, [1, 2, 3])
curried.(->(x) { x % 2 == 1 }) #=> 2

curried = F::Enum.count(->(x) { x % 2 == 1 }, F._)
curried.([1, 2, 3]) #=> 2

Parameters:

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

    ->(item) { returns Boolean }

  • enumerable (#to_enum) (defaults to: F._)

Returns:

  • (Integer)

Since:

  • 0.1.0



180
181
182
# File 'lib/fun_ruby/enum.rb', line 180

def count(function = F._, enumerable = F._)
  curry_implementation(:count, function, enumerable)
end

.each(function = F._, enumerable = F._) ⇒ ::Array

Applies a function to each element of an enumerable and returns the initial enumerable

Examples:

Basic

F::Enum.each(->(x) { puts x }, [1, 2, 3]) #=> [1, 2, 3]

Curried

curried = F::Enum.each
curried.(->(x) { puts x }).([1, 2, 3]) #=> [1, 2, 3]

Curried with placeholder

curried = F::Enum.each(F._, [1, 2, 3])
curried.(->(x) { puts x }) #=> [1, 2, 3]

Parameters:

  • function (#call/1) (defaults to: F._)
  • enumerable (#to_enum) (defaults to: F._)

Returns:

  • (::Array)

    the passed enumerable

Since:

  • 0.1.0



59
60
61
# File 'lib/fun_ruby/enum.rb', line 59

def each(function = F._, enumerable = F._)
  curry_implementation(:each, function, enumerable)
end

.map(function = F._, enumerable = F._) ⇒ ::Array

Returns a new enumerable with new values calculated by applying a function to each element of a passed enumerable

Examples:

Basic

F::Enum.map(->(x) { x * 2 }, [1, 2, 3]) #=> [2, 4, 6]

Curried

curried = F::Enum.map
curried.(->(x) { x * 2 }).([1, 2, 3]) #=> [2, 4, 6]

Curried with placeholder

curried = F::Enum.map(F._, [1, 2, 3])
curried.(->(x) { x * 2 }) # => [2, 4, 6]

Parameters:

  • function (#call/1) (defaults to: F._)
  • enumerable (#to_enum) (defaults to: F._)

Returns:

  • (::Array)

    a new enumerable with calculated values

Since:

  • 0.1.0



83
84
85
# File 'lib/fun_ruby/enum.rb', line 83

def map(function = F._, enumerable = F._)
  curry_implementation(:map, function, enumerable)
end

.reduce(function = F._, accumulator = F._, enumerable = F._) ⇒ Object

Returns a single item by iterating through the list, successively calling the iterator function and passing it an accumulator value and the current value from the array, and then passing the result to the next call.

Examples:

Basic: sum of all elements

F::Enum.reduce(->(acc, x) { acc + x }, 0, [1, 2, 3]) #=> 6

Basic: subtraction of all elements

F::Enum.reduce(->(acc, x) { acc - x }, 0, [1, 2, 3]) #=> -6

Basic: histogram

F::Enum.reduce(
  ->(acc, x) { acc[x] += 1; acc },
  Hash.new(0),
  [1, 1, 1, 2, 2, 3]
) #=> { 1 => 3, 2 => 2, 3 => 1 }

Curried: sum

curried = F::Enum.reduce
curried.(->(acc, x) { acc + x }).(0).([1, 2, 3]) #=> 6

Curried: sum with placeholder for function

curried = F::Enum.reduce(F._, 0, [1, 2, 3])
curried.(->(acc, x) { acc + x }) #=> 6

Curried: sum with placeholder for accumulator

curried = F::Enum.reduce(->(acc, x) { acc + x }, F._, [1, 2, 3])
curried.(0) #=> 6

Curried: sum with placeholder for function and accumulator

curried = F::Enum.reduce(F._, F._, [1, 2, 3])
curried.(->(acc, x) { acc + x }, 0) # => 6
curried.(->(acc, x) { acc + x }).(0) # => 6

Parameters:

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

    ->(accumulator, element) {}

  • accumulator (defaults to: F._)

    Object

  • enumerable (#to_enum) (defaults to: F._)

Returns:

  • Object a result of reducing the enumerable

Since:

  • 0.1.0



153
154
155
# File 'lib/fun_ruby/enum.rb', line 153

def reduce(function = F._, accumulator = F._, enumerable = F._)
  curry_implementation(:reduce, function, accumulator, enumerable)
end

.select(function = F._, enumerable = F._) ⇒ ::Array

Returns a new enumerable containing only these elements results of calling a function on them are truthy

Examples:

Basic

F::Enum.select(->(x) { x % 2 == 0 }, [1, 2, 3, 4, 5]) #=> [2, 4]

Curried

curried = F::Enum.select
curried.(->(x) { x % 2 == 0 }).( [1, 2, 3, 4, 5]) #=> [2, 4]

Curried with placeholder

curried = F::Enum.select(F._, [1, 2, 3, 4, 5])
curried.(->(x) { x % 2 == 0 }) #=> [2, 4]

Parameters:

  • function (#call/1) (defaults to: F._)
  • enumerable (#to_enum) (defaults to: F._)

Returns:

  • (::Array)

    a new enumerable with selected values

Since:

  • 0.1.0



107
108
109
# File 'lib/fun_ruby/enum.rb', line 107

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

Instance Method Details

#all?(function = F._, enumerable = F._) ⇒ Boolean

Returns a boolean which represents if all results of applying a function to each element are truthy

Examples:

Basic returning true

F::Enum.all?(->(x) { x % 2 == 0 }, [2, 4, 6]) #=> true

Basic returning false

F::Enum.all?(->(x) { x % 2 == 0 }, [2, 5, 6]) #=> false

Curried

curried = F::Enum.all?
curried.(->(x) { x % 2 == 0 }).([1, 2, 3]) #=> false

Curried with placeholder

curried = F::Enum.all?(F._, [2, 4, 6])
curried.(->(x) { x % 2 == 0 }) # => true

Parameters:

  • function (#call/1) (defaults to: F._)
  • enumerable (#to_enum) (defaults to: F._)

Returns:

  • (Boolean)

Since:

  • 0.1.0



35
36
37
# File 'lib/fun_ruby/enum.rb', line 35

def all?(function = F._, enumerable = F._)
  curry_implementation(:all?, function, enumerable)
end

#count(function = F._, enumerable = F._) ⇒ Integer

Goes through a given enumerable and count the amount of elements for which a passed function returned true

Examples:

Basic: count odd elements

F::Enum.count(->(x) { x % 2 == 1 }, [1, 2, 3]) #=> 2

Curried: count odd elements

curried = F::Enum.count
curried.(->(x) { x % 2 == 1 }).([1, 2, 3]) #=> 2

Curried with placeholder: count odd elements

curried = F::Enum.count(F._, [1, 2, 3])
curried.(->(x) { x % 2 == 1 }) #=> 2

curried = F::Enum.count(->(x) { x % 2 == 1 }, F._)
curried.([1, 2, 3]) #=> 2

Parameters:

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

    ->(item) { returns Boolean }

  • enumerable (#to_enum) (defaults to: F._)

Returns:

  • (Integer)

Since:

  • 0.1.0



180
181
182
# File 'lib/fun_ruby/enum.rb', line 180

def count(function = F._, enumerable = F._)
  curry_implementation(:count, function, enumerable)
end

#each(function = F._, enumerable = F._) ⇒ ::Array

Applies a function to each element of an enumerable and returns the initial enumerable

Examples:

Basic

F::Enum.each(->(x) { puts x }, [1, 2, 3]) #=> [1, 2, 3]

Curried

curried = F::Enum.each
curried.(->(x) { puts x }).([1, 2, 3]) #=> [1, 2, 3]

Curried with placeholder

curried = F::Enum.each(F._, [1, 2, 3])
curried.(->(x) { puts x }) #=> [1, 2, 3]

Parameters:

  • function (#call/1) (defaults to: F._)
  • enumerable (#to_enum) (defaults to: F._)

Returns:

  • (::Array)

    the passed enumerable

Since:

  • 0.1.0



59
60
61
# File 'lib/fun_ruby/enum.rb', line 59

def each(function = F._, enumerable = F._)
  curry_implementation(:each, function, enumerable)
end

#map(function = F._, enumerable = F._) ⇒ ::Array

Returns a new enumerable with new values calculated by applying a function to each element of a passed enumerable

Examples:

Basic

F::Enum.map(->(x) { x * 2 }, [1, 2, 3]) #=> [2, 4, 6]

Curried

curried = F::Enum.map
curried.(->(x) { x * 2 }).([1, 2, 3]) #=> [2, 4, 6]

Curried with placeholder

curried = F::Enum.map(F._, [1, 2, 3])
curried.(->(x) { x * 2 }) # => [2, 4, 6]

Parameters:

  • function (#call/1) (defaults to: F._)
  • enumerable (#to_enum) (defaults to: F._)

Returns:

  • (::Array)

    a new enumerable with calculated values

Since:

  • 0.1.0



83
84
85
# File 'lib/fun_ruby/enum.rb', line 83

def map(function = F._, enumerable = F._)
  curry_implementation(:map, function, enumerable)
end

#reduce(function = F._, accumulator = F._, enumerable = F._) ⇒ Object

Returns a single item by iterating through the list, successively calling the iterator function and passing it an accumulator value and the current value from the array, and then passing the result to the next call.

Examples:

Basic: sum of all elements

F::Enum.reduce(->(acc, x) { acc + x }, 0, [1, 2, 3]) #=> 6

Basic: subtraction of all elements

F::Enum.reduce(->(acc, x) { acc - x }, 0, [1, 2, 3]) #=> -6

Basic: histogram

F::Enum.reduce(
  ->(acc, x) { acc[x] += 1; acc },
  Hash.new(0),
  [1, 1, 1, 2, 2, 3]
) #=> { 1 => 3, 2 => 2, 3 => 1 }

Curried: sum

curried = F::Enum.reduce
curried.(->(acc, x) { acc + x }).(0).([1, 2, 3]) #=> 6

Curried: sum with placeholder for function

curried = F::Enum.reduce(F._, 0, [1, 2, 3])
curried.(->(acc, x) { acc + x }) #=> 6

Curried: sum with placeholder for accumulator

curried = F::Enum.reduce(->(acc, x) { acc + x }, F._, [1, 2, 3])
curried.(0) #=> 6

Curried: sum with placeholder for function and accumulator

curried = F::Enum.reduce(F._, F._, [1, 2, 3])
curried.(->(acc, x) { acc + x }, 0) # => 6
curried.(->(acc, x) { acc + x }).(0) # => 6

Parameters:

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

    ->(accumulator, element) {}

  • accumulator (defaults to: F._)

    Object

  • enumerable (#to_enum) (defaults to: F._)

Returns:

  • Object a result of reducing the enumerable

Since:

  • 0.1.0



153
154
155
# File 'lib/fun_ruby/enum.rb', line 153

def reduce(function = F._, accumulator = F._, enumerable = F._)
  curry_implementation(:reduce, function, accumulator, enumerable)
end

#select(function = F._, enumerable = F._) ⇒ ::Array

Returns a new enumerable containing only these elements results of calling a function on them are truthy

Examples:

Basic

F::Enum.select(->(x) { x % 2 == 0 }, [1, 2, 3, 4, 5]) #=> [2, 4]

Curried

curried = F::Enum.select
curried.(->(x) { x % 2 == 0 }).( [1, 2, 3, 4, 5]) #=> [2, 4]

Curried with placeholder

curried = F::Enum.select(F._, [1, 2, 3, 4, 5])
curried.(->(x) { x % 2 == 0 }) #=> [2, 4]

Parameters:

  • function (#call/1) (defaults to: F._)
  • enumerable (#to_enum) (defaults to: F._)

Returns:

  • (::Array)

    a new enumerable with selected values

Since:

  • 0.1.0



107
108
109
# File 'lib/fun_ruby/enum.rb', line 107

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