If I want a part of an array I can use [] or split:
arr = [1,2,3,4,5]
arr[1..3]
=> [2, 3, 4]
But is there a 'general' version of []? Can I apply it to any Enumerator?
enum = arr.each
enum.xxx_method(1..3) # is equal to arr[1..3].each
Of course you can use arr[1..3] directly. But I'm seeking a general way to handle any enumerator.
If you have an enumerator, you can count on Enumerable methods drop and take:
# abstract if necessary as enum_slice(range)
enumerator.drop(2).take(3)
If that enumerator is an array you don't need to traverse it, check the method Array#lazy_slice that I asked to be added to enumerable_lazy in relation with your previous question:
require 'enumerable/lazy'
class Array
def lazy_slice(range)
Enumerator.new do |yielder|
range.each do |index|
yielder << self[index]
end
end.lazy
end
end
some_big_array = (0..10000).to_a # fake array, it won't generally be a range
p some_big_array.lazy_slice(9995..10000).map { |x| 2*x }.to_a
#=> [19990, 19992, 19994, 19996, 19998, 20000]
Related
I need help understanding inheritance.
class MyArray < Array
end
a = MyArray[1, 2, 3] #=> [1, 2, 3]
b = MyArray[4, 5] #=> [4, 5]
c = a + b #=> [1, 2, 3, 4, 5]
a.class #=> MyArray
b.class #=> MyArray
c.class #=> Array
I don't understand why the result of the addition is not an instance of MyArray class.
I don't understand why my array "a" is not "MyArray" class after add.
Why should it (be a MyArray)? Concatenation operation on arrays is defined to return a new Array, so that's what happens here. https://ruby-doc.org/core-2.5.3/Array.html#method-i-2B
If you want, you can override that operation in your class to return an instance of MyArray. Don't forget about all other similar methods.
This is also why it's a bad idea to subclass standard collections. Better to use composition over inheritance here.
Just to add a bit to Sergio's answer in terms of his comment on using composition over inheritance and the exchange in the comments.
Instead of saying MyArray is an array you can say MyArrayLike has and array.
Then you can "forward" methods that make sense to the underlying array but still add your own functionality that makes sense for your class without sub-classing array.
Ruby even has several ways to make this very easy including the Forwardable module.
class MyArrayLike
attr_reader :arr
def initialize( initial_arr )
#arr = initial_arr
end
def +(other)
result = self.class.new(arr + other.arr)
# maybe you want to do more than just concat the underlying array, if so you can do it here
result
end
def first
# for example maybe you want first to just return the first item in the underlying array.
arr.first
end
end
a = MyArrayLike.new([1,2,3])
b = MyArrayLike.new([4,5])
puts "a.class = #{a.class}"
# => a.class = MyArrayLike
puts a
# => #<MyArrayLike:0x00000000dc4b00>
a += b
puts "a.class = #{a.class}"
# => a.class = MyArrayLike
puts a
# => #<MyArrayLike:0x00000000dc4470>
puts a.first
# => 1
puts a.arr
# => 1
# 2
# 3
# 4
# 5
Adding MyArray to MyArray to get Array may be counterintuitive, but a method can be defined to return any class. And in the case of Array#+, which you are calling, it just happens to be defined to return an Array. That's it.
If you want it to return a MyArray, one way to do it is to define MyArray#+ as follows:
class MyArray < Array
def +other
MyArray.new(super)
end
end
(MyArray.new([1, 2, 3]) + MyArray.new([4, 5])).class # => MyArray
By the way, notice that your MyArray#initialize definition is meaningless, and hence redundant.
Is there a way to return the new array?
I try to return the array of squared value [1, 4, 9] but it keeps returning [1, 2, 3] (the original array) Here is my code:
def square_array(array)
array.each do |number|
number *= number
puts number
end
end
square_array([1, 2, 3])
A much simpler version that does what you want:
def square_array(array)
array.map do |number|
number*number
end
end
The problem with your code is that when you assign something to number, you're just assigning a value to a local variable, not some magic reference into an array.
Try this out:
Using Each method
def square_array(array)
temp = []
array.each do |number|
temp << (number * number)
end
temp
end
When writing semantic ruby, it is best to use proper enumerable methods. There is no need for the temporary variable in this case... we can use the #map method to return a new array that is the result of applying a function to each value in turn. This is a core concept of the functional programming paradigm:
def square_array numbers
numbers.map { |x| x ** 2 }
end
I fund a way, I just needed to push the result in a new array
def square_array(array)
new_array = []
array.each do |num|
squared_num = num ** 2
new_array.push(squared_num)
end
return new_array
end
say I have
arr = [1,2,3]
How can I change this method so it adds each argument to the array?
def add(*number)
arr << *number
end
So add(4,5,6) produces:
arr #=> [1,2,3,4,5,6]
When accepting arguments via splat, they will always be an array. So you can simply add the two arrays together.
def add(*numbers)
arr + numbers
end
Use concat:
def add(*nums)
arr.concat nums
end
Or +:
def add(*nums)
arr + nums
end
$arr = [1,2,3]
def add(*number)
$arr.concat number
end
add(4,5,6)
$arr #=> [1,2,3,4,5,6]
Note: concat modifies the object it operates on ($arr). Plus (+) does not.
As the Tin Man mentions, you don't want to use a global to do this. It is better to simply do
arr.concat [4,5,6]
outside of a function call. Better yet:
arr += [4,5,6]
I have a tree that I'm trying to traverse. As I traverse it, I keep a stack of enumerators in which each enumerator is used to enumerate over a tree's children.
I'd like to be able to duplicate this stack of enumerators and hand it to another object so it may traverse the tree starting in the place indicated by the state of the stack.
When I attempt to call #dup on Enumerator, I get an error. Is it possible to duplicate an Enumerator? If not, how could I accomplish the same thing? (I've considered a stack of integers as indices, but am worried about the efficiency.
Here's some code to show what I'm seeing...
Once the first enumerator has started, you cannot duplicate it. That is my situation.
a = [1,2,3].each
=> #<Enumerator: [1, 2, 3]:each>
a.next
=> 1
b = a.dup
TypeError: can't copy execution context
from (irb):3:in `initialize_copy'
from (irb):3:in `initialize_dup'
from (irb):3:in `dup'
from (irb):3
Implement your own enumerator class.
There’s not much magic to an enumerator beyond incrementing an internal counter.
class MyArrayEnumerator
def initialize(array)
#ary,#n=array,0
end
def next
raise StopIteration if #n == #ary.length
a=#ary[#n];#n+=1;a
end
end
class Array
def my_each
MyArrayEnumerator.new(self)
end
end
a = [1,2,3].my_each # => #<MyArrayEnumerator:0x101c96588 #n=0, #array=[1, 2, 3]>
a.next # => 1
b = a.dup # => #<MyArrayEnumerator:0x101c95ae8 #n=1, #array=[1, 2, 3]>
a.next # => 2
b.next # => 2
Use clone instead:
e1 = [1,2,3].each
e1.dup # TypeError: can't copy execution context
e2 = e1.clone
e1.next #=> 1
e2.next #=> 1
keep one "head" amongst Enumerator's instances, and store history for behind copies:
class Enum
def initialize()
#history = [] # history will be shared between instances
#history_cursor = -1
#head = Enumerator.new do |yielder|
#yielder = yielder
enumerate
end
end
def next
if #history_cursor < #history.count - 1
#history[#history_cursor += 1]
else
new_item #head.next
end
end
private
def new_item item
#history << item
#history_cursor = #history.count - 1
item
end
def enumerate
13.times do |i|
#yielder << i # yielder is shared between instances
end
end
end
Usage:
enum1 = Enum.new
p enum1.next # 0
enum2 = enum1.clone
p enum2.next # 1
p enum1.next # 1
I am new to Ruby, is there a way to yield values from Ruby functions? If yes, how? If not, what are my options to write lazy code?
Ruby's yield keyword is something very different from the Python keyword with the same name, so don't be confused by it. Ruby's yield keyword is syntactic sugar for calling a block associated with a method.
The closest equivalent is Ruby's Enumerator class. For example, the equivalent of the Python:
def eternal_sequence():
i = 0
while True:
yield i
i += 1
is this:
def eternal_sequence
Enumerator.new do |enum|
i = 0
while true
enum.yield i # <- Notice that this is the yield method of the enumerator, not the yield keyword
i +=1
end
end
end
You can also create Enumerators for existing enumeration methods with enum_for. For example, ('a'..'z').enum_for(:each_with_index) gives you an enumerator of the lowercase letters along with their place in the alphabet. You get this for free with the standard Enumerable methods like each_with_index in 1.9, so you can just write ('a'..'z').each_with_index to get the enumerator.
I've seen Fibers used in that way, look at an example from this article:
fib = Fiber.new do
x, y = 0, 1
loop do
Fiber.yield y
x,y = y,x+y
end
end
20.times { puts fib.resume }
If you are looking to lazily generate values, #Chuck's answer is the correct one.
If you are looking to lazily iterate over a collection, Ruby 2.0 introduced the new .lazy enumerator.
range = 1..Float::INFINITY
puts range.map { |x| x+1 }.first(10) # infinite loop
puts range.lazy.map { |x| x+1 }.first(10) # [2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
Ruby supports generators out of the box using Enumerable::Generator:
require 'generator'
# Generator from an Enumerable object
g = Generator.new(['A', 'B', 'C', 'Z'])
while g.next?
puts g.next
end
# Generator from a block
g = Generator.new { |g|
for i in 'A'..'C'
g.yield i
end
g.yield 'Z'
}
# The same result as above
while g.next?
puts g.next
end
https://ruby-doc.org/stdlib-1.8.7/libdoc/generator/rdoc/Generator.html
Class Enumerator and its method next behave similar
https://docs.ruby-lang.org/en/3.1/Enumerator.html#method-i-next
range = 1..Float::INFINITY
enumerator = range.each
puts enumerator.class # => Enumerator
puts enumerator.next # => 1
puts enumerator.next # => 2
puts enumerator.next # => 3