module Core

Public Instance Methods

compose() click to toggle source

TODO: figure out how to notate many args. [f] isn't correct here.

compose
f

-> (a -> b)

# File lib/fubby/core.rb, line 44
def compose
  ->(*f) {
    _compose = ->(x, *f) {
      f.length == 1 ? f[0].(x) : f[0].(_compose.(x, *f[1..-1]))
    }

    ->(x) { _compose.(x, *f) }
  }
end
curry() click to toggle source
curry

(a -> b) -> (a -> b)

# File lib/fubby/core.rb, line 8
def curry
  _curry = ->(f, *given_args) {
    ->(*remaining_args) {
      total_args = given_args + remaining_args

      total_args.length == f.arity ? f.(*total_args) : _curry.(f, *total_args)
    }
  }

  ->(f) {
    ->(*x) {
      f.arity == x.length ? f.(*x) : _curry.(f, *x)
    }
  }
end
flip() click to toggle source
flip

(a -> b) -> (a -> b)

# File lib/fubby/core.rb, line 55
def flip
  ->(f) {
    ->(*args) {
      f.(*reverse.(args))
    }
  }
end
identity() click to toggle source
identity

a -> a

# File lib/fubby/core.rb, line 3
def identity
  ->(x) { x }
end
reduce() click to toggle source
reduce

a, (b -> a), [b] -> a

# File lib/fubby/core.rb, line 25
def reduce
  curry.(->(memo, f, array) {
    if memo == nil
      case array.length
      when 0 then nil
      when 1 then array[0]
      else reduce.(f.(array[0], array[1]), f, array[2..-1])
      end
    else
      case array.length
      when 0 then memo
      else reduce.(f.(memo, array[0]), f, array[1..-1])
      end
    end
  })
end