Class: Array
Overview
lib/ruby/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.
-
#runs(&block) ⇒ [Array]
Returns a list of sublists, where each sublist contains only equal elements.
-
#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.
Instance Method Summary collapse
- #blank? ⇒ Boolean
-
#count(*args) ⇒ Integer
Count the number of elements that satisfy the predicate.
-
#defined_at?(n) ⇒ Boolean
True if ‘#at` is defined for the given `n`.
-
#head ⇒ Object
Return the first item.
- #present? ⇒ Boolean
-
#sum(&block) ⇒ Object
Accumulate elements using the ‘+` method, optionally transforming them first using a block.
Instance Method Details
#blank? ⇒ Boolean
5 6 7 |
# File 'lib/ruby/jruby_hack.rb', line 5 def blank? empty? end |
#count(*args) ⇒ Integer
Count the number of elements that satisfy the predicate
215 216 217 218 219 220 221 222 223 |
# File 'lib/ruby/jruby_hack.rb', line 215 def count(*args) if block_given? inject(0){|n, e| yield(e) ? n + 1 : n } elsif args.empty? size else inject(0){|n, e| e == args.first ? n + 1 : n } end end |
#defined_at?(n) ⇒ Boolean
True if ‘#at` is defined for the given `n`
30 31 32 |
# File 'lib/ruby/jruby_hack.rb', line 30 def defined_at?(n) n < length and -n <= length end |
#drop(n) ⇒ Array
Select all elements except the first ‘n` ones.
71 72 73 74 |
# File 'lib/ruby/jruby_hack.rb', line 71 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.
121 122 123 124 125 126 127 128 |
# File 'lib/ruby/jruby_hack.rb', line 121 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.
109 110 111 112 113 114 115 116 |
# File 'lib/ruby/jruby_hack.rb', line 109 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?`.
18 19 20 21 22 |
# File 'lib/ruby/jruby_hack.rb', line 18 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.
58 59 60 61 |
# File 'lib/ruby/jruby_hack.rb', line 58 def init(n = 1) raise ArgumentError, "n cannot be negative" if n < 0 slice(0..-(n + 1)) or [] end |
#present? ⇒ Boolean
9 10 11 |
# File 'lib/ruby/jruby_hack.rb', line 9 def present? not empty? end |
#runs(&block) ⇒ [Array]
Returns a list of sublists, where each sublist contains only equal elements. Equality is determined by a two-argument block parameter. The concatenation of the result is equal to the original argument.
180 181 182 183 184 185 186 187 |
# File 'lib/ruby/jruby_hack.rb', line 180 def runs(&block) unless empty? as, bs = tail.split_until{|x| block.call(head, x) } head.cons(as).cons(bs.runs(&block)) else [] end end |
#split_at(n) ⇒ (Array, Array)
Split the array in two at the given position.
95 96 97 98 |
# File 'lib/ruby/jruby_hack.rb', line 95 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.
157 158 159 160 161 |
# File 'lib/ruby/jruby_hack.rb', line 157 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.
166 167 168 169 170 |
# File 'lib/ruby/jruby_hack.rb', line 166 def split_when(&block) prefix = take_until(&block) suffix = drop(prefix.length) return prefix, suffix end |
#sum(&block) ⇒ Object
Accumulate elements using the ‘+` method, optionally transforming them first using a block
199 200 201 202 203 204 205 |
# File 'lib/ruby/jruby_hack.rb', line 199 def sum(&block) if block_given? tail.inject(yield(head)){|sum,e| sum + yield(e) } else tail.inject(head){|sum,e| sum + e } end end |
#tail ⇒ Array
Selects all elements except the first.
45 46 47 48 |
# File 'lib/ruby/jruby_hack.rb', line 45 def tail _, *xs = self xs end |
#take(n) ⇒ Array
Select the first ‘n` elements.
83 84 85 86 |
# File 'lib/ruby/jruby_hack.rb', line 83 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.
145 146 147 148 149 150 151 152 |
# File 'lib/ruby/jruby_hack.rb', line 145 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.
133 134 135 136 137 138 139 140 |
# File 'lib/ruby/jruby_hack.rb', line 133 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 |