-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[GR-20446] Implement Enumerator::Lazy#with_index
PullRequest: truffleruby/2669
- Loading branch information
Showing
6 changed files
with
83 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# -*- encoding: us-ascii -*- | ||
|
||
require_relative '../../../spec_helper' | ||
require_relative 'fixtures/classes' | ||
|
||
ruby_version_is "2.7" do | ||
describe "Enumerator::Lazy#with_index" do | ||
it "enumerates with an index" do | ||
(0..Float::INFINITY).lazy.with_index.map { |i, idx| [i, idx] }.first(3).should == [[0, 0], [1, 1], [2, 2]] | ||
end | ||
|
||
it "enumerates with an index starting at a given offset" do | ||
(0..Float::INFINITY).lazy.with_index(3).map { |i, idx| [i, idx] }.first(3).should == [[0, 3], [1, 4], [2, 5]] | ||
end | ||
|
||
it "enumerates with an index starting at 0 when offset is nil" do | ||
(0..Float::INFINITY).lazy.with_index(nil).map { |i, idx| [i, idx] }.first(3).should == [[0, 0], [1, 1], [2, 2]] | ||
end | ||
|
||
it "raises TypeError when offset does not convert to Integer" do | ||
-> { (0..Float::INFINITY).lazy.with_index(false).map { |i, idx| i }.first(3) }.should raise_error(TypeError) | ||
end | ||
|
||
it "enumerates with a given block" do | ||
result = [] | ||
(0..Float::INFINITY).lazy.with_index { |i, idx| result << [i * 2, idx] }.first(3) | ||
result.should == [[0,0],[2,1],[4,2]] | ||
end | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
src/main/ruby/truffleruby/core/truffle/enumerator_operations.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# frozen_string_literal: true | ||
|
||
# Copyright (c) 2021, 2021 Oracle and/or its affiliates. All rights reserved. This | ||
# code is released under a tri EPL/GPL/LGPL license. You can use it, | ||
# redistribute it and/or modify it under the terms of the: | ||
# | ||
# Eclipse Public License version 2.0, or | ||
# GNU General Public License version 2, or | ||
# GNU Lesser General Public License version 2.1. | ||
|
||
module Truffle | ||
module EnumeratorOperations | ||
LAZY_OVERRIDE_METHODS = %i[map collect flat_map collect_concat select find_all filter filter_map reject grep grep_v | ||
zip take take_while drop drop_while uniq with_index] | ||
|
||
def self.lazy_method(meth) | ||
if LAZY_OVERRIDE_METHODS.include?(meth) | ||
:"_enumerable_#{meth}" | ||
else | ||
meth | ||
end | ||
end | ||
end | ||
end |