Skip to content

Commit

Permalink
Implement .toggle
Browse files Browse the repository at this point in the history
- takes a number of Callables and an :off flag indicating initial state
- (1..15).toggle(* < 5, * > 10, * < 15)  i           # 1 2 3 4 11 12 13 14
- (1..15).toggle(:off, * > 2, * < 5, * > 10, * < 15) # 3 4 11 12 13 14
  • Loading branch information
lizmat committed Nov 26, 2017
1 parent 694f534 commit 78caeb6
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/core/Any-iterable-methods.pm
Expand Up @@ -1810,6 +1810,22 @@ Did you mean to add a stub (\{...\}) or did you mean to .classify?"
}
}

proto method toggle(|) {*}
multi method toggle(Any:D: Callable:D \condition, :$off!) {
Seq.new( $off
?? Rakudo::Iterator.Until(self.iterator, condition)
!! Rakudo::Iterator.While(self.iterator, condition)
)
}
multi method toggle(Any:D: Callable:D \condition) {
Seq.new(Rakudo::Iterator.While(self.iterator, condition))
}
multi method toggle(Any:D: *@conditions, :$off) {
Seq.new(
Rakudo::Iterator.Toggle(self.iterator, @conditions.iterator, !$off)
)
}

proto method head(|) {*}
multi method head(Any:D:) is raw {
nqp::if(
Expand Down

0 comments on commit 78caeb6

Please sign in to comment.