lib/prelude.rb in prelude-0.0.3 vs lib/prelude.rb in prelude-0.0.5
- old
+ new
@@ -19,119 +19,64 @@
# 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 $
+# $Id: prelude.rb 34 2007-10-23 21:38:09Z prelude $
$:.unshift(File.dirname(__FILE__))
+require 'prelude/util'
+require 'prelude/array_list'
+
+# WARNING: Somehow rdoc breaks on multiline constants and does not
+# generate a full list of constants below. Please see the sources for
+# all defined functions and functors, there are plenty and they are all
+# defined as constants.
+#
+# The implementation of frequently used functors is based on the code
+# from Ben Yu's rparsec library, see http://rubyforge.org/frs/?group_id=2326
module Prelude
- VERSION='0.0.3'
+ VERSION = '0.0.5'
- # Returns function that returns its argument
- Id = lambda { |x| x }
+ LIST_IMPLEMENTATION = Prelude::ArrayList
# 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 a needed method is not available
+ class MissingMethodError < RuntimeError; 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
+ # Thrown if something wrong with the functions' arguments
+ class ArgumentError < RuntimeError; 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
+ # Thrown if bad list is encountered
+ class ImproperListError < RuntimeError; end
-end # Prelude
-
-class Symbol
-
- # Converts a symbol to a proc object
- def to_proc
- proc { |obj, *args| obj.send(self, *args) }
+ # A helper method to create an empty list of a given list type. Has to be a proper list.
+ def Prelude.new_list(list=nil)
+ res = LIST_IMPLEMENTATION.new(list)
+ #p "new_list #{res.inspect}"
+ raise ImproperListError unless res.kind_of?(Prelude::ProperList)
+ res
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)) }
+ # A helper method to generate a new lambda that uses native implementation if available
+ def Prelude.use_native(meth)
+ list = Prelude.new_list
+ if list.kind_of?(Prelude::ProperList) and list.respond_to?(meth)
+ Lambda.new { |l, *args| l.method(meth)[*args] }
else
- self.to_proc.call(*args.flatten)
+ nil
end
end
-end # Symbol
-class Proc
+end # Prelude
- # 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
+require 'prelude/lambda'
+require 'prelude/functors'
+require 'prelude/functions'
+require 'prelude/monad'