Sha256: ec2d7de842de9621b774a25770ea644434a428b7b5f7604f523dbd16076bd6fa

Contents?: true

Size: 863 Bytes

Versions: 1

Compression:

Stored size: 863 Bytes

Contents

=begin rdoc

= Functor

By definition a Functor is simply a first class method, but these are common
in the form of Method and Proc. So here a Functor is a bit more specialied
as a 1st class _metafunction_. Essentally, a Functor can vary its behavior
accorrding to the operation applied to it.

== Synopsis

  require 'carat/functor'
  
  f = Functor.new { |op, x| x.send(op, x) }
  f + 1  #=> 2
  f + 2  #=> 4
  f + 3  #=> 6
  f * 1  #=> 1
  f * 2  #=> 2
  f * 3  #=> 9
  
== Notes

It would probably be a little better if we had a kernelless base object class.
Built-in public Object methods will not work in a Functor b/c of this.
Or perhaps this can improved via delegation.

== History

* 2005-04-11 Passed basic tests.
  
=end

class Functor
  def initialize(&func)
    @func = func
  end
  def method_missing(op, *args)
    @func.call(op, *args)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
carats-0.3.0 lib/carat/functor.rb