#-- # This file is part of the Prelude library that provides tools to # enable Haskell style functional programming in Ruby. # # http://prelude.rubyforge.org # # Copyright (C) 2006 APP Design, Inc. # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public # License as published by the Free Software Foundation; either # version 2.1 of the License, or (at your option) any later version. # # This library 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. See the GNU # Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with this library; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA #++ # # $Id: prelude.rb 17 2006-09-17 18:03:15Z prelude $ $:.unshift(File.dirname(__FILE__)) module Prelude VERSION='0.0.3' # Returns function that returns its argument Id = lambda { |x| x } # Thrown if an illegal operation is performed on an empty list. class EmptyListError < RuntimeError; end # This is used to handle empty list errors in this library. Re-define to fit. def empty_list_error raise EmptyListError, 'Illegal operation on an empty list.' end # Thrown if no function was supplied class MissingFunctionError < RuntimeError; end # This is used to handle missing function errors in this library. Re-define to fit. def missing_function_error raise MissingFunctionError, 'No function or block supplied.' end # A utility to determine if a function was passed def get_proc(f=nil, &block) # Has to be either function 'f' or block f = block_given? ? block : missing_function_error if f.nil? f.to_proc end end # Prelude class Symbol # Converts a symbol to a proc object def to_proc proc { |obj, *args| obj.send(self, *args) } end # Syntaxic sugar for something like this: -:+, i.e., defines proc object that executes 'plus'. alias -@ to_proc # FIXIT def curry(one, *args) proc { |*args| self.to_proc.call(one, *args) } end # This is will serve as an infix composition operator for symbols. If between two symbols, # returns composition proc, executes left symbol otherwise. def **(*args) if (1==args.length) && args[0].is_a?(Symbol) proc {|*a| self.to_proc.call(args[0].call(*a)) } else self.to_proc.call(*args.flatten) end end end # Symbol class Proc # Syntaxic sugar for something like this: ~head(list), i.e., gives actual head instead of proc that # can do it if called. alias ~ call # FIXIT def curry(one, *args) lambda { |*args| self.call(one, *args)} end # This is will serve as an infix composition operator for procs. If between two procs, # returns composition proc, executes left proc otherwise. def **(*args) if (1==args.length) && args[0].is_a?(Proc) lambda {|*a| self.call(args[0].call(*a)) } else self.call(*args.flatten) end end end # Proc module Kernel # Method object for currently executing method def this_method name = (Kernel.caller[0] =~ /`([^']*)'/ and $1) eval "self.method(\"#{name}\".to_sym)", binding end # Method object for the caller of the currently executing method def caller_method name = (Kernel.caller[1] =~ /`([^']*)'/ and $1) eval "self.method(\"#{name}\".to_sym)", binding end # Shuts up Ruby's warning. def silence_warnings old_verbose, $VERBOSE = $VERBOSE, nil yield ensure $VERBOSE = old_verbose end end # Kernel silence_warnings do require 'prelude/list' require 'prelude/tuple' require 'prelude/monad' end