Chain section, keep_if, etc.

[1,2,3].partition.inject(0) do |acc, x|
  x>2  # this line is intended to be used by `partition`
  acc+=x # this line is intended to be used by `inject`
end

I know that I can write above stanzas using different methods, but this is not important here. What I want to ask is why does someone want to use partition(or other methods, such as keep_if, delete_if) at the beginning of the chain?
In my example, after I bound inject, I could not use partition. I can write the above stanzas with each:

[1,2,3].each.inject(0) do |acc, x|
  x>2 # this line is intended to be used by `partition`
  acc+=x # this line is intended to be used by `inject`
end

and he will be the same, right?

I know it x>2will be dropped (and not used) on partition. Only acc+=xcomplete the task (summarize all the elements in this case).
I just wrote this to show my “intention”: I want to use it partitionin a chain like this [].partition.inject(0).
I know that the above code will not work as I expected, and I know that I can chain after the block ( }.mapas Neil Slater mentioned).

, , partition ( , keep_if, delete_if ..), each ( partition ).
partition.inject, partition each, partition (x>2).
partition.with_index ( ) ( ):

shuffled_array
  .partition                        
  .with_index { |element, index|    
    element > index                 
}

. , , 2.

+3
2

. , , , Ruby , , . , Enumerator, , Enumerator::Lazy . , . OP. -.

, :

. x>2 , .

[1,2,3].partition.inject(0) do |x, acc|
  x>2         # <---- return value of this line is never used
  acc+=x
end

[1,2,3].each.inject(0) do |x, acc|
  x>2         # <---- return value of this line is never used
  acc+=x
end

.

#each.

[1,2,3].each.inject(0) do |x, acc|
  acc+=x
end

:

[1,2,3].inject(0) do |x, acc|
  acc+=x
end

#partition. :

[1,2,3].partition.inject(0) do |x, acc|
  acc+=x
end

:

[1,2,3].inject(0) do |x, acc|
  acc+=x
end

, , :

[ 1, 2, 3 ].inject :+

#partition . , , , #partition, #keep_if .., , , , , :

array = [ *1..6 ]
shuffled_arrray = array.shuffle     # randomly shuffles the array elements
shuffled_array
  .partition                        # partition enumerator comes into play
  .with_index { |element, index|    # method Enumerator#with_index comes into play
    element > index                 # and partitions elements into those greater
}                                   # than their index, and those smaller

:

e = partition_enumerator_of_array = array.partition
# And then, we can partition the array in many ways:
e.each &:even? # partitions into odd / even numbers
e.each { rand() > 0.5 } # partitions the array randomly
# etc.

, :

array.partition &:even?

:

e.each &:even?

, , . . , #map! #reject!, , . , , . , , , , , , , - . .

Enumerator , Enumerator. , Enumerator , , , . , .

+3

, [3, 3] - , - , . , , "" , .

, , , .method : }.each end.each

, , , , . ( a map):

[1,2,3].partition {|x| x > 2}.map do |part|
  part.inject(0) do |acc, x|
    x + acc
  end
end
# => [3, 3]

( , , Ruby ).

.inject , . , Ruby-.

.partition .map . :

[1,2,3].partition do
  |x| x > 2
end.map do |part|
  part.inject(0) do |acc, x|
    x + acc
  end
end

., { } do end, .

, , .

parts = [1,2,3].partition {|x| x > 2}
parts.map do |part|
  part.inject(0) do |acc, x|
    x + acc
  end
end
+3

All Articles