Class: Array
- Extended by:
- Fr::Monad, Fr::Monoid
- Defined in:
- lib/fr/array.rb
Selection collapse
-
#drop(n) ⇒ Array
Select all elements except the first ‘n` ones.
-
#init(n = 1) ⇒ Array
Selects all elements except the last ‘n` ones.
-
#split_at(n) ⇒ (Array, Array)
Split the array in two at the given position.
-
#tail ⇒ Array
Selects all elements except the first.
-
#take(n) ⇒ Array
Select the first ‘n` elements.
Filtering collapse
-
#drop_until(&block) ⇒ Array
Drops the longest prefix of elements that do not satisfy the predicate.
-
#drop_while(&block) ⇒ Array
Drops the longest prefix of elements that satisfy the predicate.
-
#split_until(&block) ⇒ (Array, Array)
Splits the array into prefix/suffix pair according to the predicate.
-
#split_when(&block) ⇒ (Array, Array)
Splits the array into prefix/suffix pair according to the predicate.
-
#take_until(accumulator = [], &block) ⇒ Array
Takes the longest prefix of elements that do not satisfy the predicate.
-
#take_while(accumulator = [], &block) ⇒ Array
Takes the longest prefix of elements that satisfy the predicate.
Class Method Summary collapse
- .bind(x, &f) ⇒ Object
-
.plus(a, b) ⇒ Object
-
Array a -> Array a -> Array a.
-
-
.run(computation) ⇒ Object
Evaluators.
-
.unfold(seed) ⇒ Object
First generated element is first in Array.
-
.unfoldR(seed) ⇒ Object
First generated element is last in Array.
-
.unit(value) ⇒ Object
Combinators.
-
.zero ⇒ Object
-
Array a.
-
Instance Method Summary collapse
-
#defined_at?(n) ⇒ Boolean
True if ‘#at` is defined for the given `n`.
-
#head ⇒ Object
Return the first item.
Methods included from Fr::Monoid
Methods included from Fr::Monad
applyM, composeM, filterM, foldM, forM, join, liftM, mapM, sequence, unlessM, whenM, zipM
Methods included from Fr::Functor
Class Method Details
.bind(x, &f) ⇒ Object
18 19 20 |
# File 'lib/fr/monad/array.rb', line 18 def bind(x, &f) x.map(&f).inject([], &:concat) end |
.plus(a, b) ⇒ Object
-
Array a -> Array a -> Array a
10 11 12 |
# File 'lib/fr/monoid/array.rb', line 10 def plus(a, b) a + b end |
.run(computation) ⇒ Object
Evaluators
7 8 9 |
# File 'lib/fr/monad/array.rb', line 7 def run(computation) computation end |
.unfold(seed) ⇒ Object
First generated element is first in Array
3 4 5 6 |
# File 'lib/fr/unfold.rb', line 3 def unfold(seed, &block) m = yield(seed) m.fold([]){|(item, seed)| [item] + unfold(seed, &block) } end |
.unfoldR(seed) ⇒ Object
First generated element is last in Array
21 22 23 24 |
# File 'lib/fr/unfold.rb', line 21 def unfoldR(seed, &block) m = yield(seed) m.fold([]){|(item, seed)| unfold(seed, &block) + [item] } end |
.unit(value) ⇒ Object
Combinators
14 15 16 |
# File 'lib/fr/monad/array.rb', line 14 def unit(value) [value] end |
Instance Method Details
#defined_at?(n) ⇒ Boolean
True if ‘#at` is defined for the given `n`
20 21 22 |
# File 'lib/fr/array.rb', line 20 def defined_at?(n) n < length and -n <= length end |
#drop(n) ⇒ Array
Select all elements except the first ‘n` ones.
61 62 63 64 |
# File 'lib/fr/array.rb', line 61 def drop(n) raise ArgumentError, "n cannot be negative" if n < 0 slice(n..-1) or [] end |
#drop_until(&block) ⇒ Array
Drops the longest prefix of elements that do not satisfy the predicate.
111 112 113 114 115 116 117 118 |
# File 'lib/fr/array.rb', line 111 def drop_until(&block) # This is in tail call form unless empty? or yield(head) tail.drop_until(&block) else self end end |
#drop_while(&block) ⇒ Array
Drops the longest prefix of elements that satisfy the predicate.
99 100 101 102 103 104 105 106 |
# File 'lib/fr/array.rb', line 99 def drop_while(&block) # This is in tail call form if not empty? and yield(head) tail.drop_while(&block) else self end end |
#head ⇒ Object
Return the first item. Raises an ‘IndexError` if the Array is `empty?`.
8 9 10 11 12 |
# File 'lib/fr/array.rb', line 8 def head raise IndexError, "head of empty list" if empty? x, = self x end |
#init(n = 1) ⇒ Array
Selects all elements except the last ‘n` ones.
48 49 50 51 |
# File 'lib/fr/array.rb', line 48 def init(n = 1) raise ArgumentError, "n cannot be negative" if n < 0 slice(0..-(n + 1)) or [] end |
#split_at(n) ⇒ (Array, Array)
Split the array in two at the given position.
85 86 87 88 |
# File 'lib/fr/array.rb', line 85 def split_at(n) n = length + n if n < 0 return take(n), drop(n) end |
#split_until(&block) ⇒ (Array, Array)
Splits the array into prefix/suffix pair according to the predicate.
147 148 149 150 151 |
# File 'lib/fr/array.rb', line 147 def split_until(&block) prefix = take_while(&block) suffix = drop(prefix.length) return prefix, suffix end |
#split_when(&block) ⇒ (Array, Array)
Splits the array into prefix/suffix pair according to the predicate.
156 157 158 159 160 |
# File 'lib/fr/array.rb', line 156 def split_when(&block) prefix = take_until(&block) suffix = drop(prefix.length) return prefix, suffix end |
#tail ⇒ Array
Selects all elements except the first.
35 36 37 38 |
# File 'lib/fr/array.rb', line 35 def tail _, *xs = self xs end |
#take(n) ⇒ Array
Select the first ‘n` elements.
73 74 75 76 |
# File 'lib/fr/array.rb', line 73 def take(n) raise ArgumentError, "n cannot be negative" if n < 0 slice(0, n) or [] end |
#take_until(accumulator = [], &block) ⇒ Array
Takes the longest prefix of elements that do not satisfy the predicate.
135 136 137 138 139 140 141 142 |
# File 'lib/fr/array.rb', line 135 def take_until(accumulator = [], &block) # This is in tail call form unless empty? or yield(head) tail.take_until(head.snoc(accumulator), &block) else accumulator end end |
#take_while(accumulator = [], &block) ⇒ Array
Takes the longest prefix of elements that satisfy the predicate.
123 124 125 126 127 128 129 130 |
# File 'lib/fr/array.rb', line 123 def take_while(accumulator = [], &block) # This is in tail call form if not empty? and yield(head) tail.take_while(head.snoc(accumulator), &block) else accumulator end end |