prelude 0.0.3 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. data/CHANGELOG +25 -1
  2. data/README +11 -12
  3. data/Rakefile +6 -10
  4. data/lib/prelude.rb +36 -91
  5. data/lib/prelude/{tuple.rb → array_list.rb} +30 -27
  6. data/lib/prelude/functions.rb +414 -0
  7. data/lib/prelude/functors.rb +72 -0
  8. data/lib/prelude/lambda.rb +89 -0
  9. data/lib/prelude/minimal_array_list.rb +61 -0
  10. data/lib/prelude/monad.rb +11 -15
  11. data/lib/prelude/proper_list.rb +47 -0
  12. data/lib/prelude/proper_ruby_list.rb +48 -0
  13. data/lib/prelude/util.rb +33 -0
  14. data/test/tc_functions.rb +801 -0
  15. data/test/tc_monad.rb +21 -41
  16. data/test/ts_prelude.rb +2 -8
  17. metadata +13 -36
  18. data/doc/classes/Kernel.html +0 -198
  19. data/doc/classes/Prelude.html +0 -241
  20. data/doc/classes/Prelude/EmptyListError.html +0 -113
  21. data/doc/classes/Prelude/List.html +0 -2692
  22. data/doc/classes/Prelude/MissingFunctionError.html +0 -113
  23. data/doc/classes/Prelude/Monad.html +0 -283
  24. data/doc/classes/Prelude/Tuple.html +0 -217
  25. data/doc/classes/Proc.html +0 -198
  26. data/doc/classes/Symbol.html +0 -219
  27. data/doc/created.rid +0 -1
  28. data/doc/files/CHANGELOG.html +0 -122
  29. data/doc/files/README.html +0 -328
  30. data/doc/files/TODO.html +0 -95
  31. data/doc/files/lib/prelude/list_rb.html +0 -83
  32. data/doc/files/lib/prelude/monad_rb.html +0 -83
  33. data/doc/files/lib/prelude/tuple_rb.html +0 -83
  34. data/doc/files/lib/prelude_rb.html +0 -98
  35. data/doc/fr_class_index.html +0 -35
  36. data/doc/fr_file_index.html +0 -33
  37. data/doc/fr_method_index.html +0 -140
  38. data/doc/index.html +0 -27
  39. data/doc/rdoc-style.css +0 -208
  40. data/lib/prelude/list.rb +0 -588
  41. data/test/tc_higher.rb +0 -89
  42. data/test/tc_list.rb +0 -777
  43. data/test/tc_tuple.rb +0 -82
@@ -0,0 +1,72 @@
1
+ #--
2
+ # This file is part of the Prelude library that provides tools to
3
+ # enable Haskell style functional programming in Ruby.
4
+ #
5
+ # http://prelude.rubyforge.org
6
+ #
7
+ # Copyright (C) 2006 APP Design, Inc.
8
+ #
9
+ # This library is free software; you can redistribute it and/or
10
+ # modify it under the terms of the GNU Lesser General Public
11
+ # License as published by the Free Software Foundation; either
12
+ # version 2.1 of the License, or (at your option) any later version.
13
+ #
14
+ # This library is distributed in the hope that it will be useful,
15
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
16
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17
+ # Lesser General Public License for more details.
18
+ #
19
+ # You should have received a copy of the GNU Lesser General Public
20
+ # License along with this library; if not, write to the Free Software
21
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22
+ #++
23
+ #
24
+ # $Id: functors.rb 34 2007-10-23 21:38:09Z prelude $
25
+
26
+ module Prelude
27
+
28
+ ID = Lambda.new { |x| x }
29
+ IDN = Lambda.new { |*x| x }
30
+ NEG = Lambda.new { |x| - x }
31
+ INC = Lambda.new { |x| x + 1 }
32
+ DEC = Lambda.new { |x| x - 1 }
33
+ PLUS = Lambda.new { |x,y| x + y }
34
+ MINUS = Lambda.new { |x,y| x - y }
35
+ MUL = Lambda.new { |x,y| x * y }
36
+ DIV = Lambda.new { |x,y| x / y }
37
+ MOD = Lambda.new { |x,y| x % y }
38
+ POWER = Lambda.new { |x,y| x ** y }
39
+ NOT = Lambda.new { |x,y| ! x }
40
+ BOOL_AND = Lambda.new { |x,y| x && y }
41
+ BOOL_OR = Lambda.new { |x,y| x || y }
42
+ XOR = Lambda.new { |x,y| x ^ y }
43
+ BIT_AND = Lambda.new { |x,y| x & y }
44
+ BIG_OR = Lambda.new { |x,y| x | y }
45
+ MATCH = Lambda.new { |x,y| x =~ y }
46
+ EQ = Lambda.new { |x,y| x == y }
47
+ NE = Lambda.new { |x,y| x != y }
48
+ LT = Lambda.new { |x,y| x < y }
49
+ GT = Lambda.new { |x,y| x > y }
50
+ LE = Lambda.new { |x,y| x <= y }
51
+ GE = Lambda.new { |x,y| x >= y }
52
+ COMPARE = Lambda.new { |x,y| x <=> y }
53
+ FST = Lambda.new { |x,_| x }
54
+ SND = Lambda.new { |_,x| x }
55
+ AT = Lambda.new { |x,y| x[y] }
56
+
57
+ CALL = Lambda.new { |x,y| x.call(y) }
58
+ FEED = Lambda.new { |x,y| y.call(x) }
59
+ TO_A = Lambda.new { |x| x.to_a }
60
+ TO_S = Lambda.new { |x| x.to_s }
61
+ TO_I = Lambda.new { |x| x.to_i }
62
+ TO_SYM = Lambda.new { |x| x.to_sym }
63
+ TO_F = Lambda.new { |x| x.to_f }
64
+
65
+
66
+ # Always returns the given value.
67
+ CONST = Lambda.new { |v| Lambda.new { |*list| v } }
68
+
69
+ # Returns the n-th argument
70
+ NTH = Lambda.new { |n| Lambda.new { |*list| list[n] } }
71
+
72
+ end # Prelude
@@ -0,0 +1,89 @@
1
+ #--
2
+ # This file is part of the Prelude library that provides tools to
3
+ # enable Haskell style functional programming in Ruby.
4
+ #
5
+ # http://prelude.rubyforge.org
6
+ #
7
+ # Copyright (C) 2006 APP Design, Inc.
8
+ #
9
+ # This library is free software; you can redistribute it and/or
10
+ # modify it under the terms of the GNU Lesser General Public
11
+ # License as published by the Free Software Foundation; either
12
+ # version 2.1 of the License, or (at your option) any later version.
13
+ #
14
+ # This library is distributed in the hope that it will be useful,
15
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
16
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17
+ # Lesser General Public License for more details.
18
+ #
19
+ # You should have received a copy of the GNU Lesser General Public
20
+ # License along with this library; if not, write to the Free Software
21
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22
+ #++
23
+
24
+ module Prelude
25
+
26
+ # $Id: lambda.rb 34 2007-10-23 21:38:09Z prelude $
27
+ #
28
+ # Somewhat extended version of Ruby's native lambda/Proc that defines all needed methods. We
29
+ # probably can get away with modifying the Proc class, but this approach seems safer.
30
+ class Lambda < Proc
31
+
32
+ def initialize(&block)
33
+ Proc.new(&block)
34
+ end
35
+
36
+ alias proc_call call
37
+ def call(*args)
38
+ #p "call #{self.inspect} #{args.inspect} arity #{self.arity}"
39
+ proc_call(*args)
40
+ end
41
+ alias [] call
42
+
43
+ def *(right)
44
+ Lambda.new {|*a|
45
+ #p "* #{self.inspect} #{right.inspect} #{a.inspect}"
46
+
47
+ # req = right.arity
48
+ # if req > 0
49
+ # right_args = a[0..req-1]
50
+ # left_args = a[req..-1]
51
+ # else
52
+ # right_args = a
53
+ # left_args = []
54
+ # end
55
+
56
+ # p "right_args #{right_args.inspect}"
57
+ # right_res = right[*right_args]
58
+
59
+ # p "right_res #{right_res.inspect}"
60
+ # if req > 0
61
+ # left_args = [right_res] + left_args
62
+ # else
63
+ # left_args = right_res
64
+ # end
65
+ # p "left_args #{left_args.inspect}"
66
+ # self[*left_args]
67
+
68
+ # Compact version of the above (delete the above when done!)
69
+ req = right.arity
70
+ if req > 0
71
+ self[*([right[*a[0..req-1]]] + a[req..-1]) ]
72
+ else
73
+ self[*right[*a]]
74
+ end
75
+
76
+ }
77
+ end
78
+
79
+ def curry(one)
80
+ Lambda.new { |*args| self[one, *args] }
81
+ end
82
+
83
+ def uncurry()
84
+ Lambda.new { |*args| self[args[0]] }
85
+ end
86
+
87
+ end # class Lambda
88
+
89
+ end # Prelude
@@ -0,0 +1,61 @@
1
+ #--
2
+ # This file is part of the Prelude library that provides tools to
3
+ # enable Haskell style functional programming in Ruby.
4
+ #
5
+ # http://prelude.rubyforge.org
6
+ #
7
+ # Copyright (C) 2006 APP Design, Inc.
8
+ #
9
+ # This library is free software; you can redistribute it and/or
10
+ # modify it under the terms of the GNU Lesser General Public
11
+ # License as published by the Free Software Foundation; either
12
+ # version 2.1 of the License, or (at your option) any later version.
13
+ #
14
+ # This library is distributed in the hope that it will be useful,
15
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
16
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17
+ # Lesser General Public License for more details.
18
+ #
19
+ # You should have received a copy of the GNU Lesser General Public
20
+ # License along with this library; if not, write to the Free Software
21
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22
+ #++
23
+ # $Id: minimal_array_list.rb 34 2007-10-23 21:38:09Z prelude $
24
+
25
+ require 'prelude/proper_ruby_list'
26
+
27
+ module Prelude
28
+
29
+ # This is an impementation of the minimally useful list based on Ruby arrays.
30
+ class MinimalArrayList
31
+ include Prelude::ProperRubyList
32
+
33
+ def initialize(a=nil)
34
+ @arr = (!a.nil? and a.kind_of?(Array)) ? a : []
35
+ self
36
+ end
37
+
38
+ def cons(v)
39
+ self.class.new([v]+@arr)
40
+ end
41
+
42
+ def head
43
+ raise EmptyListError if null
44
+ @arr[0]
45
+ end
46
+
47
+ def tail
48
+ raise EmptyListError if null
49
+ self.class.new(@arr[1..-1])
50
+ end
51
+
52
+ def null
53
+ @arr.length == 0
54
+ end
55
+
56
+ def to_a
57
+ @arr
58
+ end
59
+
60
+ end # ArrayList
61
+ end # Prelude
@@ -23,22 +23,23 @@
23
23
 
24
24
  module Prelude
25
25
 
26
- # $Id: monad.rb 13 2006-09-11 05:19:16Z prelude $
26
+ # $Id: monad.rb 34 2007-10-23 21:38:09Z prelude $
27
27
  #
28
+ # Defines monadic behavior for anything willing to include it.
28
29
  module Monad
29
- State = {}
30
+ M_STATE = {}
30
31
 
31
32
  def wrap
32
- State[object_id] = self
33
+ M_STATE[object_id] = self
33
34
  self
34
35
  end
35
36
 
36
37
  def unwrap
37
- State[object_id]
38
+ M_STATE[object_id]
38
39
  end
39
40
 
40
41
  def empty
41
- State[object_id] = nil
42
+ M_STATE[object_id] = nil
42
43
  self
43
44
  end
44
45
 
@@ -49,21 +50,16 @@ module Prelude
49
50
  end
50
51
 
51
52
  def bind(f=nil)
52
- case
53
- when f.kind_of?(Symbol) || f.kind_of?(Method) || f.kind_of?(Proc) :
54
- State[object_id] = f.to_proc.call(State[object_id])
53
+ wrap unless M_STATE[object_id]
54
+ if f.kind_of?(Symbol) || f.kind_of?(Method) || f.kind_of?(Proc)
55
+ M_STATE[object_id] = f.to_proc.call(M_STATE[object_id])
55
56
  self
56
-
57
- # when f.kind_of?(Proc) :
58
- # State[object_id] = f.call(State[object_id])
59
- # self
60
-
61
57
  else
62
58
  f
63
- end # case
59
+ end # if
64
60
  end # bind
65
61
 
66
- alias << bind
62
+ alias >> bind
67
63
 
68
64
  end # Monad
69
65
 
@@ -0,0 +1,47 @@
1
+ #--
2
+ # This file is part of the Prelude library that provides tools to
3
+ # enable Haskell style functional programming in Ruby.
4
+ #
5
+ # http://prelude.rubyforge.org
6
+ #
7
+ # Copyright (C) 2006 APP Design, Inc.
8
+ #
9
+ # This library is free software; you can redistribute it and/or
10
+ # modify it under the terms of the GNU Lesser General Public
11
+ # License as published by the Free Software Foundation; either
12
+ # version 2.1 of the License, or (at your option) any later version.
13
+ #
14
+ # This library is distributed in the hope that it will be useful,
15
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
16
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17
+ # Lesser General Public License for more details.
18
+ #
19
+ # You should have received a copy of the GNU Lesser General Public
20
+ # License along with this library; if not, write to the Free Software
21
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22
+ #++
23
+
24
+ module Prelude
25
+
26
+ # $Id: proper_list.rb 34 2007-10-23 21:38:09Z prelude $
27
+ #
28
+ # Requires (but does not implement) 4 functions a proper list just cannot do without.
29
+ module ProperList
30
+ def cons(v)
31
+ raise MissingMethodError.new("Method 'cons' is not implemented by #{self.class.name}")
32
+ end
33
+
34
+ def head
35
+ raise MissingMethodError.new("Method 'head' is not implemented by #{self.class.name}")
36
+ end
37
+
38
+ def tail
39
+ raise MissingMethodError.new("Method 'tail' is not implemented by #{self.class.name}")
40
+ end
41
+
42
+ def null
43
+ raise MissingMethodError.new("Method 'null' is not implemented by #{self.class.name}")
44
+ end
45
+
46
+ end # ProperList
47
+ end # Prelude
@@ -0,0 +1,48 @@
1
+ #--
2
+ # This file is part of the Prelude library that provides tools to
3
+ # enable Haskell style functional programming in Ruby.
4
+ #
5
+ # http://prelude.rubyforge.org
6
+ #
7
+ # Copyright (C) 2006 APP Design, Inc.
8
+ #
9
+ # This library is free software; you can redistribute it and/or
10
+ # modify it under the terms of the GNU Lesser General Public
11
+ # License as published by the Free Software Foundation; either
12
+ # version 2.1 of the License, or (at your option) any later version.
13
+ #
14
+ # This library is distributed in the hope that it will be useful,
15
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
16
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17
+ # Lesser General Public License for more details.
18
+ #
19
+ # You should have received a copy of the GNU Lesser General Public
20
+ # License along with this library; if not, write to the Free Software
21
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22
+ #++
23
+
24
+ require 'prelude/proper_list'
25
+
26
+ module Prelude
27
+
28
+ # $Id: proper_ruby_list.rb 34 2007-10-23 21:38:09Z prelude $
29
+ #
30
+ # Adds (but does not implement) 3 functions to a ProperList to make a well-behaved
31
+ # Ruby list.
32
+ module ProperRubyList
33
+ include Prelude::ProperList
34
+
35
+ def initialize(a=nil)
36
+ raise MissingMethodError.new("Method 'initialize' is not implemented by #{self.class.name}")
37
+ end
38
+
39
+ def to_a
40
+ raise MissingMethodError.new("Method 'to_a' is not implemented by #{self.class.name}")
41
+ end
42
+
43
+ def each(&block)
44
+ self.to_a.each(&block)
45
+ end
46
+
47
+ end # ProperRubyList
48
+ end # Prelude
@@ -0,0 +1,33 @@
1
+ #--
2
+ # This file is part of the Prelude library that provides tools to
3
+ # enable Haskell style functional programming in Ruby.
4
+ #
5
+ # http://prelude.rubyforge.org
6
+ #
7
+ # Copyright (C) 2006 APP Design, Inc.
8
+ #
9
+ # This library is free software; you can redistribute it and/or
10
+ # modify it under the terms of the GNU Lesser General Public
11
+ # License as published by the Free Software Foundation; either
12
+ # version 2.1 of the License, or (at your option) any later version.
13
+ #
14
+ # This library is distributed in the hope that it will be useful,
15
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
16
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17
+ # Lesser General Public License for more details.
18
+ #
19
+ # You should have received a copy of the GNU Lesser General Public
20
+ # License along with this library; if not, write to the Free Software
21
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22
+ #++
23
+
24
+ # $Id: util.rb 28 2006-12-19 00:12:54Z prelude $
25
+ #
26
+ class Symbol
27
+
28
+ # Converts a symbol to a proc object
29
+ def to_proc
30
+ proc { |obj, *args| obj.send(self, *args) }
31
+ end
32
+
33
+ end # Symbol
@@ -0,0 +1,801 @@
1
+ #--
2
+ # $Id: tc_functions.rb 30 2006-12-27 23:00:53Z prelude $
3
+ #
4
+ #
5
+ # This file is part of the Prelude library that provides tools to
6
+ # enable Haskell style functional programming in Ruby.
7
+ #
8
+ # http://prelude.rubyforge.org
9
+ #
10
+ # Copyright (C) 2006 APP Design, Inc.
11
+ #
12
+ # This library is free software; you can redistribute it and/or
13
+ # modify it under the terms of the GNU Lesser General Public
14
+ # License as published by the Free Software Foundation; either
15
+ # version 2.1 of the License, or (at your option) any later version.
16
+ #
17
+ # This library is distributed in the hope that it will be useful,
18
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
19
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20
+ # Lesser General Public License for more details.
21
+ #
22
+ # You should have received a copy of the GNU Lesser General Public
23
+ # License along with this library; if not, write to the Free Software
24
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25
+ #++
26
+
27
+ require 'prelude'
28
+ require 'test/unit'
29
+
30
+ class TestFunctions < Test::Unit::TestCase
31
+
32
+ include Prelude
33
+
34
+ def setup
35
+
36
+ @a0 = Prelude.new_list
37
+ @a1 = Prelude.new_list([1])
38
+ @a3 = Prelude.new_list([1, 2, 3])
39
+ @a5 = Prelude.new_list([1, 2, 3, 4, 5])
40
+ @true_list = Prelude.new_list([true, true, true])
41
+ @false_list = Prelude.new_list([false, false, false])
42
+ @mixed_list = Prelude.new_list([false, true, false])
43
+
44
+ end # setup
45
+
46
+ def teardown
47
+ # Nothing
48
+ end # teardown
49
+
50
+ # (:)/cons :: A -> [A] -> [A]
51
+ def test_cons
52
+ result = @a5.cons(0).to_a
53
+ expect = [0, 1, 2, 3, 4, 5]
54
+ assert_equal(expect, result)
55
+
56
+ result = @a0.cons(0).to_a
57
+ expect = [0]
58
+ assert_equal(expect, result)
59
+
60
+ result = @a0.cons([0, 1]).to_a
61
+ expect = [[0, 1]]
62
+ assert_equal(expect, result)
63
+ end
64
+
65
+ # (++)/append :: [a] -> [a] -> [a]
66
+ def test_append
67
+ result = APPEND[@a1, @a5].to_a
68
+ expect = [1, 1, 2, 3, 4, 5].to_a
69
+ assert_equal(expect, result)
70
+
71
+ result = APPEND[@a5, @a5].to_a
72
+ expect = [1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
73
+ assert_equal(expect, result)
74
+
75
+ result = APPEND[@a0, @a5].to_a
76
+ expect = [1, 2, 3, 4, 5]
77
+ assert_equal(expect, result)
78
+
79
+ result = APPEND[@a5, @a0].to_a
80
+ expect = [1, 2, 3, 4, 5]
81
+ assert_equal(expect, result)
82
+ end
83
+
84
+ # head :: [a] -> a
85
+ def test_head
86
+ result = @a5.head
87
+ expect = 1
88
+ assert_equal(expect, result)
89
+
90
+ assert_raise(EmptyListError) { @a0.head }
91
+ end
92
+
93
+ # last :: [a] -> a
94
+ def test_last
95
+ result = LAST[@a5]
96
+ expect = 5
97
+ assert_equal(expect, result)
98
+
99
+ assert_raise(EmptyListError) { LAST[@a0] }
100
+ end
101
+
102
+ # tail :: [a] -> [a]
103
+ def test_tail
104
+ result = @a5.tail.to_a
105
+ expect = [2, 3, 4, 5]
106
+ assert_equal(expect, result)
107
+
108
+ assert_raise(EmptyListError) { @a0.tail }
109
+ end
110
+
111
+ # init :: [a] -> [a]
112
+ def test_init
113
+ result = INIT[@a5].to_a
114
+ expect = [1, 2, 3, 4]
115
+ assert_equal(expect, result)
116
+
117
+ result = INIT[@a1].to_a
118
+ expect = []
119
+ assert_equal(expect, result)
120
+
121
+ assert_raise(EmptyListError) { INIT[@a0] }
122
+ end
123
+
124
+ # null :: [a] -> Bool
125
+ def test_null
126
+ result = @a5.null
127
+ expect = false
128
+ assert_equal(expect, result)
129
+
130
+ result = @a0.null
131
+ expect = true
132
+ assert_equal(expect, result)
133
+ end
134
+
135
+ # length :: [a] -> Int
136
+ def test_length
137
+ result = LENGTH[@a5]
138
+ expect = 5
139
+ assert_equal(expect, result)
140
+
141
+ result = LENGTH[@a0]
142
+ expect = 0
143
+ assert_equal(expect, result)
144
+
145
+ result = LENGTH[@a1]
146
+ expect = 1
147
+ assert_equal(expect, result)
148
+ end
149
+
150
+ # flip :: (a -> b -> c) -> b -> a -> c
151
+ def test_flip
152
+ result = MINUS[5, 10]
153
+ expect = -5
154
+ assert_equal(expect, result)
155
+
156
+ result = FLIP[MINUS, 5, 10]
157
+ expect = 5
158
+ assert_equal(expect, result)
159
+ end
160
+
161
+ # map :: (a -> b) -> [a] -> [b]
162
+ def test_map
163
+ expect = [2, 3, 4, 5, 6]
164
+ result = MAP[INC, @a5].to_a
165
+ assert_equal(expect, result)
166
+ end
167
+
168
+ # reverse :: [a] -> [a]
169
+ def test_reverse
170
+ result = REVERSE[@a5].to_a
171
+ expect = [5, 4, 3, 2, 1]
172
+ assert_equal(expect, result)
173
+ end
174
+
175
+ # reverse1 :: [a] -> [a]
176
+ def test_reverse1
177
+ result = REVERSE1[@a5].to_a
178
+ expect = [5, 4, 3, 2, 1]
179
+ assert_equal(expect, result)
180
+ end
181
+
182
+ # reverse2 :: [a] -> [a]
183
+ def test_reverse2
184
+ result = REVERSE2[@a5].to_a
185
+ expect = [5, 4, 3, 2, 1]
186
+ assert_equal(expect, result)
187
+ end
188
+
189
+ # reverse3 :: [a] -> [a]
190
+ def test_reverse3
191
+ result = REVERSE3[@a5].to_a
192
+ expect = [5, 4, 3, 2, 1]
193
+ assert_equal(expect, result)
194
+ end
195
+
196
+ # intersperse :: a -> [a] -> [a]
197
+ def test_intersperse
198
+ result = INTERSPERSE[0, @a5].to_a
199
+ expect = [1, 0, 2, 0, 3, 0, 4, 0, 5]
200
+
201
+ assert_equal(expect, result)
202
+ end
203
+
204
+ # # # transpose :: [[a]] -> [[a]]
205
+ # # def test_transpose
206
+ # # result = ~transpose([@a5, @a5, @a5])
207
+ # # expect = [[1, 1, 1], [2, 2, 2], [3, 3, 3], [4, 4, 4], [5, 5, 5] ]
208
+
209
+ # # assert_equal(expect, result)
210
+ # # end
211
+
212
+ # # * Reducing lists (folds)
213
+
214
+ # foldl :: (a -> b -> a) -> a -> [b] -> a
215
+ def test_foldl
216
+ expect = 15
217
+ result = FOLDL[PLUS, 0, @a5]
218
+ assert_equal(expect, result)
219
+ end
220
+
221
+ # foldl' :: (a -> b -> a) -> a -> [b] -> a
222
+ def test_foldl_
223
+ result = FOLDL_[PLUS, 0, @a5]
224
+ expect = NOT_IMPLEMENTED_YET
225
+ assert_equal(expect, result)
226
+ end
227
+
228
+ # foldl1 :: (a -> a -> a) -> [a] -> a
229
+ def test_foldl1
230
+ expect = 15
231
+ result = FOLDL1[PLUS, @a5]
232
+ assert_equal(expect, result)
233
+ end
234
+
235
+ # foldl1' :: (a -> a -> a) -> [a] -> a
236
+ def test_foldl1_
237
+ result = FOLDL1_[PLUS, @a5]
238
+ expect = NOT_IMPLEMENTED_YET
239
+ assert_equal(expect, result)
240
+ end
241
+
242
+ # foldr :: (a -> b -> b) -> b -> [a] -> b
243
+ def test_foldr
244
+ expect = 15
245
+ result = FOLDR[PLUS, 0, @a5]
246
+ assert_equal(expect, result)
247
+ end
248
+
249
+ # foldr1 :: (a -> a -> a) -> [a] -> a
250
+ def test_foldr1
251
+ expect = 15
252
+ result = FOLDR1[PLUS, @a5]
253
+ assert_equal(expect, result)
254
+ end
255
+
256
+ # # ** Special folds
257
+
258
+ # concat :: [[a]] -> [a]
259
+ def test_concat
260
+ result = CONCAT[ Prelude.new_list([[1, 2, [1, 2]], [1, 2]])].to_a
261
+ expect = [1, 2, [1, 2], 1, 2]
262
+ assert_equal(expect, result)
263
+ end
264
+
265
+ # concatMap :: (a -> [b]) -> [a] -> [b]
266
+ def test_concat_map
267
+ result = CONCAT_MAP[Lambda.new { |x| Prelude.new_list([x, x]) }, @a5].to_a
268
+ expect = [1, 1, 2, 2, 3, 3, 4, 4, 5, 5]
269
+
270
+ assert_equal(expect, result)
271
+ end
272
+
273
+ # and :: [Bool] -> Bool
274
+ def test_and
275
+ result = AND[@true_list]
276
+ expect = true
277
+ assert_equal(expect, result)
278
+
279
+ result = AND[@mixed_list]
280
+ expect = false
281
+ assert_equal(expect, result)
282
+
283
+ result = AND[@false_list]
284
+ expect = false
285
+ assert_equal(expect, result)
286
+ end
287
+
288
+ # or :: [Bool] -> Bool
289
+ def test_or
290
+ result = OR[@true_list]
291
+ expect = true
292
+ assert_equal(expect, result)
293
+
294
+ result = OR[@mixed_list]
295
+ expect = true
296
+ assert_equal(expect, result)
297
+
298
+ result = OR[@false_list]
299
+ expect = false
300
+ assert_equal(expect, result)
301
+ end
302
+
303
+ # any :: (a -> Bool) -> [a] -> Bool
304
+ def test_any
305
+ f1 = lambda {|x| x>5}
306
+ f2 = lambda {|x| x<5}
307
+
308
+ result = ANY[f1, @a5]
309
+ expect = false
310
+ assert_equal(expect, result)
311
+
312
+ result = ANY[f2, @a5]
313
+ expect = true
314
+ assert_equal(expect, result)
315
+ end
316
+
317
+ # all :: (a -> Bool) -> [a] -> Bool
318
+ def test_all
319
+ result = ALL[lambda {|x| x>3}, @a5]
320
+ expect = false
321
+ assert_equal(expect, result)
322
+
323
+ result = ALL[lambda {|x| x>0}, @a5]
324
+ expect = true
325
+ assert_equal(expect, result)
326
+ end
327
+
328
+ # sum :: (Num a) => [a] -> a
329
+ def test_sum
330
+ result = SUM
331
+ expect = NOT_IMPLEMENTED_YET
332
+ assert_equal(expect, result)
333
+ end
334
+
335
+ # product :: (Num a) => [a] -> a
336
+ def test_product
337
+ result = PRODUCT
338
+ expect = NOT_IMPLEMENTED_YET
339
+ assert_equal(expect, result)
340
+ end
341
+
342
+ # maximum :: (Ord a) => [a] -> a
343
+ def test_maximum
344
+ result = MAXIMUM
345
+ expect = NOT_IMPLEMENTED_YET
346
+ assert_equal(expect, result)
347
+ end
348
+
349
+ # minimum :: (Ord a) => [a] -> a
350
+ def test_minimum
351
+ result = MINIMUM
352
+ expect = NOT_IMPLEMENTED_YET
353
+ assert_equal(expect, result)
354
+ end
355
+
356
+
357
+ # * Building lists
358
+
359
+ # ** Scans
360
+ # scanl :: (a -> b -> a) -> a -> [b] -> [a]
361
+ def test_scanl
362
+ result = SCANL
363
+ expect = NOT_IMPLEMENTED_YET #[0, 1, 3, 6, 10, 15]
364
+ assert_equal(expect, result)
365
+ end
366
+
367
+ # scanl1 :: (a -> a -> a) -> [a] -> [a]
368
+ def test_scanl1
369
+ result = SCANL1
370
+ expect = NOT_IMPLEMENTED_YET #[1, 3, 6, 10, 15]
371
+ assert_equal(expect, result)
372
+ end
373
+
374
+ # scanr :: (a -> b -> b) -> b -> [a] -> [b]
375
+ def test_scanr
376
+ result = SCANR
377
+ expect = NOT_IMPLEMENTED_YET #[0, 1, 3, 6, 10, 15]
378
+ assert_equal(expect, result)
379
+ end
380
+
381
+ # scanr1 :: (a -> a -> a) -> [a] -> [a]
382
+ def test_scanr1
383
+ result = SCANR1
384
+ expect = NOT_IMPLEMENTED_YET #[1, 3, 6, 10, 15]
385
+ assert_equal(expect, result)
386
+ end
387
+
388
+
389
+ # ** Accumulating maps
390
+ # mapAccumL :: (a -> b -> (a,c)) -> a -> [b] -> (a,[c])
391
+ def test_map_accum_l
392
+ result = MAP_ACCUM_L
393
+ expect = NOT_IMPLEMENTED_YET
394
+ assert_equal(expect, result)
395
+ end
396
+
397
+ # mapAccumR :: (a -> b -> (a,c)) -> a -> [b] -> (a,[c])
398
+ def test_map_accum_r
399
+ result = MAP_ACCUM_R
400
+ expect = NOT_IMPLEMENTED_YET
401
+ assert_equal(expect, result)
402
+ end
403
+
404
+
405
+ # ** Infinite lists
406
+ # iterate :: (a -> a) -> a -> [a]
407
+ def test_iterate
408
+ result = ITERATE
409
+ expect = NOT_IMPLEMENTED_YET
410
+ assert_equal(expect, result)
411
+ end
412
+
413
+ # repeat :: a -> [a]
414
+ def test_repeat
415
+ result = REPEAT
416
+ expect = NOT_IMPLEMENTED_YET
417
+ assert_equal(expect, result)
418
+ end
419
+
420
+ # replicate :: Int -> a -> [a]
421
+ def test_replicate
422
+ result = REPLICATE
423
+ expect = NOT_IMPLEMENTED_YET
424
+ assert_equal(expect, result)
425
+ end
426
+
427
+ # cycle :: [a] -> [a]
428
+ def test_cycle
429
+ result = CYCLE
430
+ expect = NOT_IMPLEMENTED_YET
431
+ assert_equal(expect, result)
432
+ end
433
+
434
+
435
+ # ** Unfolding
436
+ # unfoldr :: (b -> Maybe (a, b)) -> b -> [a]
437
+ def test_unfoldr
438
+ result = UNFOLDR
439
+ expect = NOT_IMPLEMENTED_YET
440
+ assert_equal(expect, result)
441
+ end
442
+
443
+
444
+ # * Sublists
445
+
446
+ # ** Extracting sublists
447
+ # take :: Int -> [a] -> [a]
448
+ def test_take
449
+ result = TAKE
450
+ expect = NOT_IMPLEMENTED_YET
451
+ assert_equal(expect, result)
452
+ end
453
+
454
+ # drop :: Int -> [a] -> [a]
455
+ def test_drop
456
+ result = DROP
457
+ expect = NOT_IMPLEMENTED_YET
458
+ assert_equal(expect, result)
459
+ end
460
+
461
+ # splitAt :: Int -> [a] -> ([a], [a])
462
+ def test_split_at
463
+ result = SPLIT_AT
464
+ expect = NOT_IMPLEMENTED_YET
465
+ assert_equal(expect, result)
466
+ end
467
+
468
+ # takeWhile :: (a -> Bool) -> [a] -> [a]
469
+ def test_take_while
470
+ result = TAKE_WHILE
471
+ expect = NOT_IMPLEMENTED_YET
472
+ assert_equal(expect, result)
473
+ end
474
+
475
+ # dropWhile :: (a -> Bool) -> [a] -> [a]
476
+ def test_drop_while
477
+ result = DROP_WHILE
478
+ expect = NOT_IMPLEMENTED_YET
479
+ assert_equal(expect, result)
480
+ end
481
+
482
+ # span :: (a -> Bool) -> [a] -> ([a], [a])
483
+ def test_span
484
+ result = SPAN
485
+ expect = NOT_IMPLEMENTED_YET
486
+ assert_equal(expect, result)
487
+ end
488
+
489
+ # break :: (a -> Bool) -> [a] -> ([a], [a])
490
+ def test_break_
491
+ result = BREAK_
492
+ expect = NOT_IMPLEMENTED_YET
493
+ assert_equal(expect, result)
494
+ end
495
+
496
+ # group :: Eq a => [a] -> [[a]]
497
+ def test_group
498
+ result = GROUP
499
+ expect = NOT_IMPLEMENTED_YET
500
+ assert_equal(expect, result)
501
+ end
502
+
503
+ # inits :: [a] -> [[a]]
504
+ def test_inits
505
+ result = INITS
506
+ expect = NOT_IMPLEMENTED_YET
507
+ assert_equal(expect, result)
508
+ end
509
+
510
+ # tails :: [a] -> [[a]]
511
+ def test_tails
512
+ result = TAILS
513
+ expect = NOT_IMPLEMENTED_YET
514
+ assert_equal(expect, result)
515
+ end
516
+
517
+ # ** Predicates
518
+ # isPrefixOf :: (Eq a) => [a] -> [a] -> Bool
519
+ def test_is_prefix_of
520
+ result = IS_PREFIX_OF
521
+ expect = NOT_IMPLEMENTED_YET
522
+ assert_equal(expect, result)
523
+ end
524
+
525
+ # isSuffixOf :: (Eq a) => [a] -> [a] -> Bool
526
+ def test_is_suffix_of
527
+ result = IS_SUFFIX_OF
528
+ expect = NOT_IMPLEMENTED_YET
529
+ assert_equal(expect, result)
530
+ end
531
+
532
+
533
+ # * Searching lists
534
+
535
+ # ** Searching by equality
536
+ # elem :: a -> [a] -> Bool
537
+ def test_elem
538
+ result = ELEM
539
+ expect = NOT_IMPLEMENTED_YET
540
+ assert_equal(expect, result)
541
+ end
542
+
543
+ # notElem :: a -> [a] -> Bool
544
+ def test_not_elem
545
+ result = NOT_ELEM
546
+ expect = NOT_IMPLEMENTED_YET
547
+ assert_equal(expect, result)
548
+ end
549
+
550
+ # lookup :: (Eq a) => a -> [(a,b)] -> Maybe b
551
+ def test_lookup
552
+ result = LOOKUP
553
+ expect = NOT_IMPLEMENTED_YET
554
+ assert_equal(expect, result)
555
+ end
556
+
557
+ # ** Searching with a predicate
558
+ # find :: (a -> Bool) -> [a] -> Maybe a
559
+ def test_find
560
+ result = FIND
561
+ expect = NOT_IMPLEMENTED_YET
562
+ assert_equal(expect, result)
563
+ end
564
+
565
+ # filter :: (a -> Bool) -> [a] -> [a]
566
+ def test_filter
567
+ result = FILTER
568
+ expect = NOT_IMPLEMENTED_YET
569
+ assert_equal(expect, result)
570
+ end
571
+
572
+ # partition :: (a -> Bool) -> [a] -> ([a], [a])
573
+ def test_partition
574
+ result = PARTITION
575
+ expect = NOT_IMPLEMENTED_YET
576
+ assert_equal(expect, result)
577
+ end
578
+
579
+
580
+ # * Indexing lists
581
+ # | These functions treat a list @xs@ as a indexed collection,
582
+ # with indices ranging from 0 to @'length' xs - 1@.
583
+
584
+ # (!!) :: [a] -> Int -> a
585
+ # Don't know how to implement it in Ruby
586
+
587
+
588
+ # elemIndex :: (Eq a) => a -> [a] -> Maybe Int
589
+ def test_elem_index
590
+ result = ELEM_INDEX
591
+ expect = NOT_IMPLEMENTED_YET
592
+ assert_equal(expect, result)
593
+ end
594
+
595
+ # elemIndices :: (Eq a) => a -> [a] -> [Int]
596
+ def test_elem_indices
597
+ result = ELEM_INDICES
598
+ expect = NOT_IMPLEMENTED_YET
599
+ assert_equal(expect, result)
600
+ end
601
+
602
+
603
+ # findIndex :: (a -> Bool) -> [a] -> Maybe Int
604
+ def test_find_index
605
+ result = FIND_INDEX
606
+ expect = NOT_IMPLEMENTED_YET
607
+ assert_equal(expect, result)
608
+ end
609
+
610
+ # findIndices :: (a -> Bool) -> [a] -> [Int]
611
+ def test_find_indices
612
+ result = FIND_INDICES
613
+ expect = NOT_IMPLEMENTED_YET
614
+ assert_equal(expect, result)
615
+ end
616
+
617
+
618
+ # * Zipping and unzipping lists
619
+
620
+ # zip :: [a] -> [b] -> [(a,b)]
621
+ def test_zip
622
+ result = ZIP[@a5, @a5].to_a
623
+ expect = [[1, 1], [2, 2], [3, 3], [4, 4], [5, 5] ]
624
+ assert_equal(expect, result)
625
+
626
+ result = ZIP[@a5, @a3].to_a
627
+ expect = [[1, 1], [2, 2], [3, 3] ]
628
+ assert_equal(expect, result)
629
+
630
+ result = ZIP[@a3, @a5].to_a
631
+ expect = [[1, 1], [2, 2], [3, 3] ]
632
+ assert_equal(expect, result)
633
+
634
+ result = ZIP[@a3, @a0].to_a
635
+ expect = []
636
+ assert_equal(expect, result)
637
+
638
+ result = ZIP[@a0, @a3]
639
+ expect = []
640
+ assert_equal(expect, result)
641
+
642
+ result = ZIP[@a1, @a3].to_a
643
+ expect = [[1,1]]
644
+ assert_equal(expect, result)
645
+
646
+ result = ZIP[@a3, @a1].to_a
647
+ expect = [[1,1]]
648
+ assert_equal(expect, result)
649
+
650
+ result = ZIP[@a1, @a0].to_a
651
+ expect = []
652
+ assert_equal(expect, result)
653
+
654
+ result = ZIP[@a0, @a1].to_a
655
+ expect = []
656
+ assert_equal(expect, result)
657
+ end
658
+
659
+ # zip3
660
+ def test_zip3
661
+ result = ZIP3
662
+ expect = NOT_IMPLEMENTED_YET
663
+ assert_equal(expect, result)
664
+ end
665
+
666
+ # zip4, zip5, zip6, zip7
667
+ def test_zip4
668
+ result = ZIP4
669
+ expect = NOT_IMPLEMENTED_YET
670
+ assert_equal(expect, result)
671
+ end
672
+
673
+
674
+ # zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
675
+ def test_zip_with
676
+ result = ZIP_WITH
677
+ expect = NOT_IMPLEMENTED_YET
678
+ assert_equal(expect, result)
679
+ end
680
+
681
+ # zipWith3
682
+ def test_zip_with3
683
+ result = ZIP_WITH3
684
+ expect = NOT_IMPLEMENTED_YET
685
+ assert_equal(expect, result)
686
+ end
687
+
688
+ # zipWith4, zipWith5, zipWith6, zipWith7
689
+ def test_zip_with4
690
+ result = ZIP_WITH4
691
+ expect = NOT_IMPLEMENTED_YET
692
+ assert_equal(expect, result)
693
+ end
694
+
695
+
696
+ # unzip :: [(a,b)] -> ([a],[b])
697
+ def test_unzip
698
+ result = UNZIP
699
+ expect = NOT_IMPLEMENTED_YET
700
+ assert_equal(expect, result)
701
+ end
702
+
703
+ # unzip3
704
+ def test_unzip3
705
+ result = UNZIP3
706
+ expect = NOT_IMPLEMENTED_YET
707
+ assert_equal(expect, result)
708
+ end
709
+
710
+ # unzip4, unzip5, unzip6, unzip7
711
+ def test_unzip4
712
+ result = UNZIP4
713
+ expect = NOT_IMPLEMENTED_YET
714
+ assert_equal(expect, result)
715
+ end
716
+
717
+
718
+ # * Special lists
719
+
720
+ # ** Functions on strings
721
+ # lines :: String -> [String]
722
+ def test_lines
723
+ result = LINES
724
+ expect = NOT_IMPLEMENTED_YET
725
+ assert_equal(expect, result)
726
+ end
727
+
728
+ # words :: String -> [String]
729
+ def test_words
730
+ result = WORDS
731
+ expect = NOT_IMPLEMENTED_YET
732
+ assert_equal(expect, result)
733
+ end
734
+
735
+ # unlines :: [String] -> String
736
+ def test_unlines
737
+ result = UNLINES
738
+ expect = NOT_IMPLEMENTED_YET
739
+ assert_equal(expect, result)
740
+ end
741
+
742
+ # unwords :: [String] -> String
743
+ def test_unwords
744
+ result = UNWORDS
745
+ expect = NOT_IMPLEMENTED_YET
746
+ assert_equal(expect, result)
747
+ end
748
+
749
+
750
+ # ** \"Set\" operations
751
+
752
+ # nub :: (Eq a) => [a] -> [a]
753
+ def test_nub
754
+ result = NUB
755
+ expect = NOT_IMPLEMENTED_YET
756
+ assert_equal(expect, result)
757
+ end
758
+
759
+
760
+ # delete :: (Eq a) => a -> [a] -> [a]
761
+ def test_delete
762
+ result = DELETE
763
+ expect = NOT_IMPLEMENTED_YET
764
+ assert_equal(expect, result)
765
+ end
766
+
767
+ # (\\) :: (Eq a) => [a] -> [a] -> [a]
768
+ # Don't know how to implement it in Ruby
769
+
770
+ # union :: (Eq a) => [a] -> [a] -> [a]
771
+ def test_union
772
+ result = UNION
773
+ expect = NOT_IMPLEMENTED_YET
774
+ assert_equal(expect, result)
775
+ end
776
+
777
+ # intersect :: (Eq a) => [a] -> [a] -> [a]
778
+ def test_intersect
779
+ result = INTERSECT
780
+ expect = NOT_IMPLEMENTED_YET
781
+ assert_equal(expect, result)
782
+ end
783
+
784
+
785
+ # ** Ordered lists
786
+
787
+ # sort :: (Ord a) => [a] -> [a]
788
+ def test_sort
789
+ result = SORT
790
+ expect = NOT_IMPLEMENTED_YET
791
+ assert_equal(expect, result)
792
+ end
793
+
794
+ # insert :: (Ord a) => a -> [a] -> [a]
795
+ def test_insert
796
+ result = INSERT
797
+ expect = NOT_IMPLEMENTED_YET
798
+ assert_equal(expect, result)
799
+ end
800
+
801
+ end # TestList