Module: Prelude

Defined in:
lib/prelude.rb,
lib/prelude/monad.rb,
lib/prelude/lambda.rb,
lib/prelude/functors.rb,
lib/prelude/array_list.rb,
lib/prelude/proper_list.rb,
lib/prelude/proper_ruby_list.rb,
lib/prelude/minimal_array_list.rb

Overview

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

++

Defined Under Namespace

Modules: Monad, ProperList, ProperRubyList Classes: ArgumentError, ArrayList, EmptyListError, ImproperListError, Lambda, MinimalArrayList, MissingFunctionError, MissingMethodError

Constant Summary collapse

VERSION =
'0.0.5'
LIST_IMPLEMENTATION =
Prelude::ArrayList
ID =
Lambda.new { |x|         x }
IDN =
Lambda.new { |*x|        x }
NEG =
Lambda.new { |x|     -   x }
INC =
Lambda.new { |x|   x +   1 }
DEC =
Lambda.new { |x|   x -   1 }
PLUS =
Lambda.new { |x,y| x +   y }
MINUS =
Lambda.new { |x,y| x -   y }
MUL =
Lambda.new { |x,y| x *   y }
DIV =
Lambda.new { |x,y| x /   y }
MOD =
Lambda.new { |x,y| x %   y }
POWER =
Lambda.new { |x,y| x **  y }
NOT =
Lambda.new { |x,y|   !   x }
BOOL_AND =
Lambda.new { |x,y| x &&  y }
BOOL_OR =
Lambda.new { |x,y| x ||  y }
XOR =
Lambda.new { |x,y| x ^   y }
BIT_AND =
Lambda.new { |x,y| x &   y }
BIG_OR =
Lambda.new { |x,y| x |   y }
MATCH =
Lambda.new { |x,y| x =~  y }
EQ =
Lambda.new { |x,y| x ==  y }
NE =
Lambda.new { |x,y| x !=  y }
LT =
Lambda.new { |x,y| x <   y }
GT =
Lambda.new { |x,y| x >   y }
LE =
Lambda.new { |x,y| x <=  y }
GE =
Lambda.new { |x,y| x >=  y }
COMPARE =
Lambda.new { |x,y| x <=> y }
FST =
Lambda.new { |x,_| x       }
SND =
Lambda.new { |_,x| x       }
AT =
Lambda.new { |x,y| x[y]    }
CALL =
Lambda.new { |x,y| x.call(y) }
FEED =
Lambda.new { |x,y| y.call(x) }
TO_A =
Lambda.new { |x|   x.to_a    }
TO_S =
Lambda.new { |x|   x.to_s    }
TO_I =
Lambda.new { |x|   x.to_i    }
TO_SYM =
Lambda.new { |x|   x.to_sym  }
TO_F =
Lambda.new { |x|   x.to_f    }
CONST =

Always returns the given value.

Lambda.new { |v| Lambda.new { |*list| v } }
NTH =

Returns the n-th argument

Lambda.new { |n| Lambda.new { |*list| list[n] } }

Class Method Summary collapse

Class Method Details

.new_list(list = nil) ⇒ Object

A helper method to create an empty list of a given list type. Has to be a proper list.

Raises:



60
61
62
63
64
65
# File 'lib/prelude.rb', line 60

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

.use_native(meth) ⇒ Object

A helper method to generate a new lambda that uses native implementation if available



68
69
70
71
72
73
74
75
# File 'lib/prelude.rb', line 68

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
    nil
  end
end