Class: Array
Class Method Summary collapse
Instance Method Summary collapse
-
#**(arr, is_zip = false) ⇒ Object
zip or Applicative <*> depending on if there any function values in the array/stream [id, double] ** [1,2,3] [1,2,3,2,4,6].
- #==(obj) ⇒ Object
- #===(obj) ⇒ Object
-
#^(arr, is_cartesian = false) ⇒ Object
Applicative <**> [1,2,3] ^ [id, double] == [1,2,3].flatmap([id, double]) [1,2,2,4,3,6].
- #call(*args) ⇒ Object
-
#cartesian_product(arr) ⇒ Object
cartesian product.
- #deep_clone ⇒ Object
- #drop(n) ⇒ Object
- #empty ⇒ Object
- #fmap(fn) ⇒ Object
- #foldl(func, unit) ⇒ Object
- #foldr(func, unit) ⇒ Object
- #standard_equals ⇒ Object
- #standard_triple_equals ⇒ Object
- #take(n) ⇒ Object
- #to_stream ⇒ Object
Class Method Details
.next_item ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/raskell/array.rb', line 50 def self.next_item next_fn = ->(state) { i = state.first arr = state.last if i == arr.length [:done] else [:yield, arr[i], Stream.new(next_fn, [i+1, arr])] end } end |
.to_stream(xs) ⇒ Object
62 63 64 |
# File 'lib/raskell/array.rb', line 62 def self.to_stream(xs) Stream.new(self.next_item, [0, xs.deep_clone]) end |
Instance Method Details
#**(arr, is_zip = false) ⇒ Object
zip or Applicative <*> depending on if there any function values in the array/stream
- id, double
-
** [1,2,3]
- 1,2,3,2,4,6
1350 1351 1352 |
# File 'lib/raskell/f.rb', line 1350 def **(arr, is_zip=false) is_zip || !self.any?{|x| x.kind_of? Proc } ? self.zip(arr.to_a) : F.flatmap.(->(f) { F.map.(f) << arr.to_stream }).(self.to_stream).to_a end |
#==(obj) ⇒ Object
71 72 73 |
# File 'lib/raskell/array.rb', line 71 def ==(obj) obj.kind_of?(Stream) ? self.to_stream == obj : standard_equals(obj) end |
#===(obj) ⇒ Object
76 77 78 |
# File 'lib/raskell/array.rb', line 76 def ===(obj) obj.kind_of?(Stream) ? self.to_stream === obj : standard_triple_equals(obj) end |
#^(arr, is_cartesian = false) ⇒ Object
Applicative <**>
- 1,2,3
-
^ [id, double] == [1,2,3].flatmap([id, double])
- 1,2,2,4,3,6
1337 1338 1339 |
# File 'lib/raskell/f.rb', line 1337 def ^(arr, is_cartesian=false) is_cartesian || !arr.any?{|x| x.kind_of? Proc } ? self.cartesian_product(arr) : F.flatmap.(->(x) { arr.to_stream.(x) }).(self).to_a end |
#call(*args) ⇒ Object
17 18 19 20 |
# File 'lib/raskell/array.rb', line 17 def call(*args) #functions = self.map { |x| x.kind_of?(Proc) ? self : ->() { x } } self.any? {|x| x.kind_of? Proc } ? fmap(->(f) { f.(*args)}) : self end |
#cartesian_product(arr) ⇒ Object
cartesian product
1343 1344 1345 |
# File 'lib/raskell/f.rb', line 1343 def cartesian_product(arr) self.map {|x| arr.to_a.map { |y| [x,y] } }.foldl(->(acc,el) { acc.push(el) }, []) end |
#deep_clone ⇒ Object
5 6 7 |
# File 'lib/raskell/array.rb', line 5 def deep_clone map(&:deep_clone) end |
#drop(n) ⇒ Object
32 33 34 35 36 37 38 39 40 |
# File 'lib/raskell/array.rb', line 32 def drop(n) if n == 0 self elsif n >= self.length [] else self.slice(n, self.length) end end |
#empty ⇒ Object
9 10 11 |
# File 'lib/raskell/array.rb', line 9 def empty [] end |
#fmap(fn) ⇒ Object
13 14 15 |
# File 'lib/raskell/array.rb', line 13 def fmap(fn) map {|x| fn.(x) } end |
#foldl(func, unit) ⇒ Object
46 47 48 |
# File 'lib/raskell/array.rb', line 46 def foldl(func, unit) (self.length-1).foldl(->(acc, idx) { func.(acc, self[idx]) }, unit) end |
#foldr(func, unit) ⇒ Object
42 43 44 |
# File 'lib/raskell/array.rb', line 42 def foldr(func, unit) (self.length-1).foldr(->(idx, acc) { func.(self[idx], acc) }, unit) end |
#standard_equals ⇒ Object
70 |
# File 'lib/raskell/array.rb', line 70 alias_method :standard_equals, :== |
#standard_triple_equals ⇒ Object
75 |
# File 'lib/raskell/array.rb', line 75 alias_method :standard_triple_equals, :=== |
#take(n) ⇒ Object
22 23 24 25 26 27 28 29 30 |
# File 'lib/raskell/array.rb', line 22 def take(n) if n == 0 [] elsif n >= self.length self else self.slice(0, n) end end |
#to_stream ⇒ Object
66 67 68 |
# File 'lib/raskell/array.rb', line 66 def to_stream self.class.to_stream(self) end |