Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

flat_map on lazy enumerator returns enumerator of incorrect cardinality #5734

Open
ilikeorangutans opened this issue May 16, 2019 · 3 comments

Comments

Projects
None yet
2 participants
@ilikeorangutans
Copy link

commented May 16, 2019

Environment

Provide at least:

  • jruby 9.2.6.0 (2.5.3) 2019-02-11 15ba00b Java HotSpot(TM) 64-Bit Server VM 25.131-b11 on 1.8.0_131-b11 +jit [darwin-x86_64]
  • jruby 9.2.7.0 (2.5.3) 2019-04-09 8a269e3 Java HotSpot(TM) 64-Bit Server VM 25.131-b11 on 1.8.0_131-b11 +jit [darwin-x86_64]
  • Darwin Jakobs-MacBook-Pro-2.local 18.5.0 Darwin Kernel Version 18.5.0: Mon Mar 11 20:40:32 PDT 2019; root:xnu-4903.251.3~3/RELEASE_X86_64 x86_64

Expected Behavior

Given this enumerator:

enumerator = Enumerator.new do |y| loop do y << [1, 2] end end

The lazy enumerator should return this:

enumerator.lazy.take(3).flat_map{|x| x}.force
=> [1, 2, 1, 2, 1, 2]

Actual Behavior

Instead it returns this:

enumerator.lazy.take(3).flat_map { |x| x}.force
 => [1, 1, 1]

@ilikeorangutans ilikeorangutans changed the title flat_map on lazy enumerator returns enumerator cardinality flat_map on lazy enumerator returns enumerator of incorrect cardinality May 16, 2019

@headius

This comment has been minimized.

Copy link
Member

commented May 17, 2019

I would guess it's a problem with the |x| part peeling off just the first element. The following appears to work:

irb(main):001:0> enumerator = Enumerator.new do |y| loop do y << [1, 2] end end
=> #<Enumerator: #<Enumerator::Generator:0x5c90e579>:each>
irb(main):002:0> enumerator.lazy.take(3).flat_map{|*x| x}.force
=> [1, 2, 1, 2, 1, 2]
@ilikeorangutans

This comment has been minimized.

Copy link
Author

commented May 17, 2019

Oh, very interesting, @headius ! I think that works as a temporary solution for me. This behaviour deviates from mri ruby, so it should still be a bug, right?

@headius

This comment has been minimized.

Copy link
Member

commented May 20, 2019

@ilikeorangutans Yes, this should be fixed in JRuby. If you have time to help out, you could confirm whether https://github.com/ruby/spec/tree/master/core/enumerator contains enough tests for this behavior, since it exposes some peculiarities of Ruby's single-argument block behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.