Sha256: f35211061573f65644a40de0feee783aab3f23bead1749c9572b674b3b058e44

Contents?: true

Size: 1.69 KB

Versions: 3

Compression:

Stored size: 1.69 KB

Contents

#:title: Functor
#--
# Functor
# v 1.0
#
# Copyright (c) 2004,2005 Thomas Sawyer
# 
# Ruby License
#
# This module is free software. You may use, modify, and/or redistribute this
# software under the same terms as Ruby.
# 
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.
#
#
# $Id: functor.rb,v 1.0 2005/04/28 17:00:24 transami Exp $
#
# ==========================================================================
# Revision History ::
# YYYY.MM.DD  Ver. Dev.      Description
# --------------------------------------------------------------------------
# 2005.04.28  1.0  Trans     * Minor modifications to documentation.
# ==========================================================================
#++

# = Description
# 
# 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 'facet/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.
# 
# == Author(s)
#
# * Thomas Sawyer
#

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
facets-0.7.0 lib/facet/functor.rb
facets-0.7.1 lib/facet/functor.rb
facets-0.7.2 lib/facet/functor.rb