Module: FunRuby::Function

Extended by:
Function
Included in:
Function
Defined in:
lib/fun_ruby/function.rb

Overview

Module for useful manipulations with functions

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.compose(required = F._, *functions) ⇒ #call

Performs right-to-left function composition

Examples:

Basic: one function

plus_2 = ->(x) { x + 2 }

curried = F::Function.compose(plus_2)
curried.(2) # => 4

Basic: two functions

plus_2 = ->(x) { x + 2 }
multiply_3 = ->(x) { x * 3 }

# multiplying -> adding
curried = F::Function.compose(plus_2, multiply_3)

curried.(2) # => 8

# adding ->  multiplying
curried = F::Function.compose(multiply_3, plus_2)

curried.(2) # => 12

Basic: three functions

plus_2 = ->(x) { x + 2 }
multiply_3 = ->(x) { x * 3 }
mod_4 = ->(x) { x % 4 }

# multiplying -> adding -> mod
curried = F::Function.compose(mod_4, plus_2, multiply_3)
curried.(4) # => 2

Parameters:

  • required (#call) (defaults to: F._)
  • *functions (#call)

Returns:

  • (#call)

Since:

  • 0.1.0



48
49
50
# File 'lib/fun_ruby/function.rb', line 48

def compose(required = F._, *functions)
  curry(method(:_compose)).(required, *functions)
end

.curry(function = F._) ⇒ Object

Examples:

build_ary = ->(a, b, c) { [a, b, c] }
curry = F::Function.curry
curried = curry.(build_ary)

curried.(1, 2, 3) # => [1, 2, 3]
curried.(1).(2).(3) # => [1, 2, 3]
curried.(F._, 2).(F._, 3).(1) # => [1, 2, 3]
curried.(F._, 2, F._).(1).(3) # => [1, 2, 3]
curried.(F._, F._, 3).(F._, 2).(1) # => [1, 2, 3]
curried.(F._, 2, F._).(1, 3) # => [1, 2, 3]
curried.(F._, 2, F._).(1, 3) # => [1, 2, 3]


105
106
107
# File 'lib/fun_ruby/function.rb', line 105

def curry(function = F._)
  handling_placeholders(method(:_curry).curry, [function])
end

.pipe(required = F._, *functions) ⇒ #call

Performs left-to-right function composition

Examples:

Basic: one function

plus_2 = ->(x) { x + 2 }

curried = F::Function.pipe(plus_2)
curried.(2) # => 4

Basic: two functions

plus_2 = ->(x) { x + 2 }
multiply_3 = ->(x) { x * 3 }

# multiplying -> adding
curried = F::Function.pipe(multiply_3, plus_2)

curried.(2) # => 8

# adding ->  multiplying
curried = F::Function.pipe(plus_2, multiply_3)

curried.(2) # => 12

Basic: three functions

plus_2 = ->(x) { x + 2 }
multiply_3 = ->(x) { x * 3 }
mod_4 = ->(x) { x % 4 }

# multiplying -> adding -> mod
curried = F::Function.pipe(multiply_3, plus_2,  mod_4)
curried.(4) # => 2

Parameters:

  • required (#call) (defaults to: F._)
  • *functions (#call)

Returns:

  • (#call)

Since:

  • 0.1.0



89
90
91
# File 'lib/fun_ruby/function.rb', line 89

def pipe(required = F._, *functions)
  curry(method(:_pipe)).(required, *functions)
end

Instance Method Details

#compose(required = F._, *functions) ⇒ #call

Performs right-to-left function composition

Examples:

Basic: one function

plus_2 = ->(x) { x + 2 }

curried = F::Function.compose(plus_2)
curried.(2) # => 4

Basic: two functions

plus_2 = ->(x) { x + 2 }
multiply_3 = ->(x) { x * 3 }

# multiplying -> adding
curried = F::Function.compose(plus_2, multiply_3)

curried.(2) # => 8

# adding ->  multiplying
curried = F::Function.compose(multiply_3, plus_2)

curried.(2) # => 12

Basic: three functions

plus_2 = ->(x) { x + 2 }
multiply_3 = ->(x) { x * 3 }
mod_4 = ->(x) { x % 4 }

# multiplying -> adding -> mod
curried = F::Function.compose(mod_4, plus_2, multiply_3)
curried.(4) # => 2

Parameters:

  • required (#call) (defaults to: F._)
  • *functions (#call)

Returns:

  • (#call)

Since:

  • 0.1.0



48
49
50
# File 'lib/fun_ruby/function.rb', line 48

def compose(required = F._, *functions)
  curry(method(:_compose)).(required, *functions)
end

#curry(function = F._) ⇒ Object

Examples:

build_ary = ->(a, b, c) { [a, b, c] }
curry = F::Function.curry
curried = curry.(build_ary)

curried.(1, 2, 3) # => [1, 2, 3]
curried.(1).(2).(3) # => [1, 2, 3]
curried.(F._, 2).(F._, 3).(1) # => [1, 2, 3]
curried.(F._, 2, F._).(1).(3) # => [1, 2, 3]
curried.(F._, F._, 3).(F._, 2).(1) # => [1, 2, 3]
curried.(F._, 2, F._).(1, 3) # => [1, 2, 3]
curried.(F._, 2, F._).(1, 3) # => [1, 2, 3]


105
106
107
# File 'lib/fun_ruby/function.rb', line 105

def curry(function = F._)
  handling_placeholders(method(:_curry).curry, [function])
end

#pipe(required = F._, *functions) ⇒ #call

Performs left-to-right function composition

Examples:

Basic: one function

plus_2 = ->(x) { x + 2 }

curried = F::Function.pipe(plus_2)
curried.(2) # => 4

Basic: two functions

plus_2 = ->(x) { x + 2 }
multiply_3 = ->(x) { x * 3 }

# multiplying -> adding
curried = F::Function.pipe(multiply_3, plus_2)

curried.(2) # => 8

# adding ->  multiplying
curried = F::Function.pipe(plus_2, multiply_3)

curried.(2) # => 12

Basic: three functions

plus_2 = ->(x) { x + 2 }
multiply_3 = ->(x) { x * 3 }
mod_4 = ->(x) { x % 4 }

# multiplying -> adding -> mod
curried = F::Function.pipe(multiply_3, plus_2,  mod_4)
curried.(4) # => 2

Parameters:

  • required (#call) (defaults to: F._)
  • *functions (#call)

Returns:

  • (#call)

Since:

  • 0.1.0



89
90
91
# File 'lib/fun_ruby/function.rb', line 89

def pipe(required = F._, *functions)
  curry(method(:_pipe)).(required, *functions)
end