Class: Array
- Inherits:
-
Object
- Object
- Array
- Defined in:
- lib/list-utils.rb
Overview
Adds several syntactic sugar methods and method aliases, inspired by CPAN’s List::Util and List::MoreUtils modules, and the Haskell 98 Standard Prelude.
Instance Method Summary collapse
-
#drop(count) ⇒ Object
Returns a copy of the Array with the first count items missing.
-
#drop_until(&blk) ⇒ Object
(also: #take_while)
Returns a copy of the Array from the first point at which &blk is true to the end.
-
#drop_while(&blk) ⇒ Object
(also: #take_until)
Returns a copy of the Array from the first point at which &blk is false to the end.
-
#false_count(&blk) ⇒ Object
Returns the number of elements for which &blk is false.
-
#fold(method_sym) ⇒ Object
(also: #reduce)
Performs the method identified by method_sym on each element with the following element as the single argument.
-
#none?(&blk) ⇒ Boolean
Returns true if none of the elements satisfy &blk, false otherwise.
-
#not_all?(&blk) ⇒ Boolean
Returns true if any of the elements do not satisfy &blk, false otherwise.
-
#rand_element ⇒ Object
Returns a randomly-chosen element of the Array.
-
#rest ⇒ Object
(also: #tail)
Returns all elements of the Array in order except the first.
-
#shuffle ⇒ Object
Returns a copy of the Array in random order.
-
#sum ⇒ Object
Syntactic sugar for fold(:+).
-
#take(count) ⇒ Object
Returns the first count elements of the Array.
-
#true_count(&blk) ⇒ Object
Returns the number of elements for which &blk is true.
Instance Method Details
#drop(count) ⇒ Object
Returns a copy of the Array with the first count items missing.
9 10 11 |
# File 'lib/list-utils.rb', line 9 def drop(count) self[ count .. -1 ] end |
#drop_until(&blk) ⇒ Object Also known as: take_while
Returns a copy of the Array from the first point at which &blk is true to the end.
22 23 24 25 |
# File 'lib/list-utils.rb', line 22 def drop_until(&blk) return self if yield head tail ? tail.drop_until(&blk) : [] end |
#drop_while(&blk) ⇒ Object Also known as: take_until
Returns a copy of the Array from the first point at which &blk is false to the end.
15 16 17 18 |
# File 'lib/list-utils.rb', line 15 def drop_while(&blk) return self unless yield head tail ? tail.drop_while(&blk) : [] end |
#false_count(&blk) ⇒ Object
Returns the number of elements for which &blk is false.
30 31 32 |
# File 'lib/list-utils.rb', line 30 def false_count(&blk) size - true_count(&blk) end |
#fold(method_sym) ⇒ Object Also known as: reduce
Performs the method identified by method_sym on each element with the following element as the single argument.
-
(0..9).to_a.fold(:+) = 45
-
[0, 1].fold(:+) = 1
-
[0, 1].fold(:*) = 0
-
‘hello’.split(”).fold(:+) = ‘hello’
43 44 45 46 47 48 49 |
# File 'lib/list-utils.rb', line 43 def fold(method_sym) output = first (self - [first]).each do |e| output = output.send(method_sym, e) end output end |
#none?(&blk) ⇒ Boolean
Returns true if none of the elements satisfy &blk, false otherwise.
55 56 57 |
# File 'lib/list-utils.rb', line 55 def none?(&blk) not any?(&blk) end |
#not_all?(&blk) ⇒ Boolean
Returns true if any of the elements do not satisfy &blk, false otherwise.
61 62 63 |
# File 'lib/list-utils.rb', line 61 def not_all?(&blk) not all?(&blk) end |
#rand_element ⇒ Object
Returns a randomly-chosen element of the Array.
66 67 68 |
# File 'lib/list-utils.rb', line 66 def rand_element shuffle[0] end |
#rest ⇒ Object Also known as: tail
Returns all elements of the Array in order except the first.
73 74 75 |
# File 'lib/list-utils.rb', line 73 def rest self[ 1 .. -1 ] end |
#shuffle ⇒ Object
Returns a copy of the Array in random order.
78 79 80 |
# File 'lib/list-utils.rb', line 78 def shuffle sort_by { rand } end |
#sum ⇒ Object
Syntactic sugar for fold(:+).
83 84 85 |
# File 'lib/list-utils.rb', line 83 def sum fold(:+) end |
#take(count) ⇒ Object
Returns the first count elements of the Array.
90 91 92 |
# File 'lib/list-utils.rb', line 90 def take(count) self[0 .. (count-1) ] end |
#true_count(&blk) ⇒ Object
Returns the number of elements for which &blk is true.
98 99 100 |
# File 'lib/list-utils.rb', line 98 def true_count(&blk) filter(&blk).size end |