-

   rss_rss_hh_new

 - e-mail

 

 -

 LiveInternet.ru:
: 17.03.2011
:
:
: 51

:


[] Ruby

, 08 2017 . 20:48 +
(enumeration) - . , , , , / .

, , . , , , .

Ruby


, , (block) yield'. Ruby, , , proc/lambda. yield, , - . .

def my_printer
  puts "Hello World!"
end

def thrice
  3.times do
    yield
  end
end

thrice &method(:my_printer)
# Hello World!
# Hello World!
# Hello World!

thrice { puts "Ruby" }
# Ruby
# Ruby
# Ruby

yield: proc' . method proc, , my_printer .

, yield, , , yield. yield, puts Hello World! yield puts Ruby.

yield . block/proc yield.

def simple_enum
  yield 4
  yield 3
  yield 2
  yield 1
  yield 0
end

simple_enum do |value|
  puts value
end
# 4
# 3
# 2
# 1
# 0


Ruby each, / (yields) . , each Ruby 50 Enumerable. include Enumerable , each, ( Enumerable).

Array (), , each ( Enumerable /).

Array.ancestors
# => [Array, Enumerable, Object, Kernel, BasicObject]
Hash.ancestors
# => [Hash, Enumerable, Object, Kernel, BasicObject]
Hash.method_defined? :each
# => true
require "set"
Set.ancestors
# => [Set, Enumerable, Object, Kernel, BasicObject]


, , .. , .

, , / . , , , (, , ), , . , Ruby , / , .

, / . , 20 , , 20 - , . . , , .

: , . , , . , , , , /. , () .

Ruby lazy Enumerable to_enum.lazy , each.

class Thing
  def each
    yield "winning"
    yield "not winning"
  end
end

a = Thing.new.to_enum.lazy

Thing.include Enumerable
b = Thing.new.lazy

a.next
# => "winning"
b.next
# => "winning"

to_enum , Enumerator Enumerable, .

, , () . , partition , . , chunk select.

x = (0..Flot::INFINITY)

y = x.chunk(&:even?)
# => #:each>>
y.next
# => [true, [0]]
y.next
# => [false, [1]]
y.next
#=> [true, [2]]

z = x.lazy.select(&:even?)
# => #:select>
z.next
# => 0
z.next
# => 2
z.next
# => 4

select , lazy select, - .


Ruby Enumerator::Lazy, take Ruby.

(0..Float::INFINITY).take(2)
# => [0, 1]

, FizzBuzz, FizzBuzz .

def divisible_by?(num)
  ->input{ (input % num).zero? }
end

def fizzbuzz_from(value)
  Enumerator::Lazy.new(value..Float::INFINITY) do |yielder, val|
    yielder << case val
    when divisible_by?(15)
      "FizzBuzz"
    when divisible_by?(3)
      "Fizz"
    when divisible_by?(5)
      "Buzz"
    else
      val
    end
  end end

x = fizzbuzz_from(7)
# => #

9.times { puts x.next }
# 7
# 8
# Fizz
# Buzz
# 11
# Fizz
# 13
# 14
# FizzBuzz

Enumerator::Lazy, , yielder . , next. each next, .

, Enumerator::Lazy.new . Enumerable , self . val , each, yielder , , each.


, , . , , , Ruby. .

require "prime"
x = (0..34).lazy.select(&Prime.method(:prime?))
x.next
# => 2
x.next
# => 3
x.next
# => 5
x.next
# => 7
x.next
# => 11

select , . , .


group_by, . , , .

[0,1,2,3,4,5,6,7,8].group_by.with_index {|_,index| index % 3 }.values
# => [[0, 3, 6], [1, 4, 7], [2, 5, 8]]

-, :

0    3    6
1    4    7
2    5    8

group_by , . , , , . , 0, 1 2 , . , values , .

, :

threes = (0..2).cycle
[0,1,2,3,4,5,6,7,8].slice_when { threes.next == 2 }.to_a
# => [[0, 1, 2], [3, 4, 5], [6, 7, 8]]

threes 0 2, . :

0    1    2
3    4    5
6    7    8

Ruby transpose, .

x = [[0, 1, 2], [3, 4, 5], [6, 7, 8]]
x = x.transpose
# => [[0, 3, 6], [1, 4, 7], [2, 5, 8]]
x = x.transpose
# => [[0, 1, 2], [3, 4, 5], [6, 7, 8]]


. , fold. Ruby reduce inject. , each_with_object. , .

:

[1,2,3].reduce(:+)
# => 6

[1,2,3].inject(:+)
# => 6

class AddStore
  def add(num)
    @value = @value.to_i + num
  end

  def inspect
    @value
  end
end

[1,2,3].each_with_object(AddStore.new) {|val, memo| memo.add(val) }
# => 6

# As of Ruby 2.4
[1,2,3].sum
# => 6

each_with_object , . , AddStore.

, . , inject reduce Ruby , .

each_with_object , .

collection = [:a, 2, :p, :p, 6, 7, :l, :e]

collection.reduce("") { |memo, value|
  memo << value.to_s if value.is_a? Symbol
  memo # Note the return value needs to be the object/collection we're building
}
# => "apple"

collection.each_with_object("") { |value, memo|
  memo << value.to_s if value.is_a? Symbol
}
# => "apple"


Ruby, , , .

class Pair < Struct.new(:first, :second)
  def same?;    inject(:eql?)  end
  def add;      inject(:+)     end
  def subtract; inject(:-)     end
  def multiply; inject(:*)     end
  def divide;   inject(:/)     end

  def swap!
    members.zip(entries.reverse) {|a,b| self[a] = b}
  end

end

x = Pair.new(23, 42)
x.same?
# => false

x.first
# => 23

x.swap!

x.first
# => 42

x.multiply
# => 966

, -, , , .
, . /.

, Ruby , , . , , , .


Ruby , . , Ruby, , .

, , / , , , . , readline , read readlines LIMIT number SQL.

. , . , , Rust , (implemented lazily).

, . , . Ruby enumerable , . , .
Original source: habrahabr.ru (comments, light).

https://habrahabr.ru/post/335072/

:  

: [1] []
 

:
: 

: ( )

:

  URL