Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Inspired by https://stackoverflow.com/questions/72809469/in-raku-how-does-one-write-the-equivalent-of-haskells-span-function This implements both the method as well as the sub version of "span". For more flexibility, the condition can also be a List of conditions (in the sub case), or just have multiple conditions (in the method case). .say for span * < 10, 2, 2, 2, 5, 5, 7, 13, 9, 6, 2, 20, 4; (2 2 2 5 5 7) (13 9 6 2 20 4) .say for span (* < 10, * < 20), 2, 2, 2, 5, 5, 7, 13, 9, 6, 2, 20, 4; (2 2 2 5 5 7) (13 9 6 2) (20 4) .say for (2, 2, 2, 5, 5, 7, 13, 9, 6, 2, 20, 4).span( * < 5, * < 10 ); (2 2 2) (5 5 7) (13 9 6 2 20 4) Span documentation: https://hackage.haskell.org/package/base-4.16.1.0/docs/Prelude.html#v:span
- Loading branch information
Showing
8 changed files
with
84 additions
and
3 deletions.
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,59 @@ | ||
augment class Rakudo::Iterator { | ||
# Return an iterator that generates Lists of a given iterator | ||
# and one or Callables with conditions. This is basically the | ||
# Haskell "span" functionality. | ||
my class Span does Iterator { | ||
has $!tests; | ||
has $!iterator; | ||
has $!next; | ||
|
||
method !SET-SELF(@tests, $iterator) { | ||
$!tests := nqp::getattr(@tests,List,'$!reified'); | ||
$!iterator := $iterator; | ||
$!next := $iterator.pull-one; | ||
self | ||
} | ||
|
||
method new(@tests, $iterator) { | ||
nqp::create(self)!SET-SELF(@tests, $iterator) | ||
} | ||
|
||
method pull-one() { | ||
if nqp::eqaddr($!next,IterationEnd) { | ||
IterationEnd | ||
} | ||
else { | ||
my $buffer := nqp::create(IterationBuffer); | ||
nqp::push($buffer,$!next); | ||
|
||
my $iterator := $!iterator; | ||
my $pulled; | ||
|
||
if nqp::elems($!tests) { | ||
my &test := nqp::shift($!tests); | ||
nqp::until( | ||
nqp::eqaddr(($pulled := $iterator.pull-one),IterationEnd) | ||
|| nqp::isfalse(test($pulled)), | ||
nqp::push($buffer,$pulled) | ||
); | ||
|
||
} | ||
else { | ||
nqp::until( | ||
nqp::eqaddr(($pulled := $iterator.pull-one),IterationEnd), | ||
nqp::push($buffer,$pulled) | ||
); | ||
} | ||
|
||
$!next := $pulled; | ||
$buffer.List | ||
} | ||
} | ||
} | ||
|
||
proto method Span(|) {*} | ||
multi method Span(&test, $iterator) { Span.new: (&test,), $iterator } | ||
multi method Span(@tests, $iterator) { Span.new: @tests, $iterator } | ||
} | ||
|
||
# vim: expandtab shiftwidth=4 |
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 |
---|---|---|
|
@@ -433,6 +433,7 @@ my @expected = ( | |
Q{&slurp}, | ||
Q{&so}, | ||
Q{&sort}, | ||
Q{&span}, | ||
Q{&splice}, | ||
Q{&split}, | ||
Q{&sprintf}, | ||
|
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 |
---|---|---|
|
@@ -430,6 +430,7 @@ my %allowed = ( | |
Q{&slurp}, | ||
Q{&so}, | ||
Q{&sort}, | ||
Q{&span}, | ||
Q{&splice}, | ||
Q{&split}, | ||
Q{&sprintf}, | ||
|
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 |
---|---|---|
@@ -1,10 +1,11 @@ | ||
src/core.e/core_prologue.pm6 | ||
src/core.e/control.pm6 | ||
src/core.e/Any-iterable-methods.pm6 | ||
src/core.e/PseudoStash.pm6 | ||
src/core.e/Grammar.pm6 | ||
src/core.e/EXPORTHOW.pm6 | ||
src/core.e/Int.pm6 | ||
src/core.e/array_multislice.pm6 | ||
src/core.e/hash_multislice.pm6 | ||
src/core.e/Rakudo/Internals/Sprintf.pm6 | ||
src/core.e/Any-iterable-methods.pm6 | ||
src/core.e/Rakudo/Iterator.pm6 |