lib/prelude.rb in prelude-0.0.2 vs lib/prelude.rb in prelude-0.0.3

- old
+ new

@@ -19,45 +19,119 @@ # 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 8 2006-09-06 17:06:22Z prelude $ +# $Id: prelude.rb 17 2006-09-17 18:03:15Z prelude $ $:.unshift(File.dirname(__FILE__)) -require 'prelude/list' -require 'prelude/tuple' -require 'prelude/monad' - module Prelude - VERSION='0.0.2' + + 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) - proc{ |*args| self.call(one, *args)} + lambda { |*args| self.call(one, *args)} end - # This is will serve as an infix composition operator - def <<(*args) + # 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) - proc {|*a| self.call(args[0].call(*a)) } + 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