Sha256: 6a9f6ab0d4ad36400a8159dab09df5b3eddd2fab38b56cb56ae3d7e2db6768e2

Contents?: true

Size: 492 Bytes

Versions: 3

Compression:

Stored size: 492 Bytes

Contents

module Kernel
  # Composes a list of functions.
  # Functions can be specified as symbols or lambdas.
  #
  # @example Composing symbols
  #   ["foo bar", "baz qux"].map(&fn(:split, :last)) #=> ["bar", "qux"]
  # @example Composing symbplds and lambdas
  #   (1..3).map(&fn(:next, -> x { x * x }, -> x { x.to_f / 2 } ))
  #   #=> [2.0, 4.5, 8.0]
  def fn(*funs)
    ->(x) do
      funs.inject(x) do |v, f|
        Proc === f ? f.call(v) : v.send(f)
      end
    end
  end

  private :fn
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
shenanigans-1.0.16 lib/shenanigans/kernel/fn.rb
shenanigans-1.0.15 lib/shenanigans/kernel/fn.rb
shenanigans-1.0.14 lib/shenanigans/kernel/fn.rb