This channel is intended for people just starting with the Raku Programming Language (raku.org). Logs are available at irclogs.raku.org/raku-beginner/live.html
Set by lizmat on 8 June 2022.
elcaro One potential way is to compare the index of the value with the `.end` (final index) of your iterator. However this would presumably only be applicable for fully reified (ie. non-lazy) arrays. (which, a lot of times you're working with anyway) 01:42
```
my @ns = 1,2,1,2,3,1,2;
for @ns.kv -> $i, $n {
if $i == @ns.end {
say "$n is the last value";
}
say $n × 2;
}
```
01:43 razetime joined
People have asked this question before, and while it seems like the answer might be the `LAST` phaser, it's probably not what you want... `LAST` fires right at the end of the last loop, _after_ a `LEAVE` phaser would fire (if there was one) 01:45
Tracking the index with `.kv` is not that bad. It's probably how you'd do it in most languages, and like I said, this feels like something you'd want on something finite like an array, rather than a indefinite lazy sequence. 01:50
If you feel like that `==` comparison is costing you valuable CPU cycles (but it probably isn't significant), another possibly trick is separate out the last value.
```
for @ns[0 ..^ *-1, *-1] -> @not-last, $last-n {
for @not-last -> $n {
say "Got $n";
}
say "$last-n is last";
}
```
Tracking the index with `.kv` is not that bad. It's probably how you'd do it in most languages, and like I said, this feels like something you'd want on something finite like an array, rather than a indefinite lazy sequence.
If you feel like that `==` comparison is costing you valuable CPU cycles (but it probably isn't significant), another possible trick is separate out the last value.
```
for @ns[0 ..^ *-1, *-1] -> @not-last, $last-n {
for @not-last -> $n {
say "Got $n";
}
say "$last-n is last";
}
```
_Another one_ 01:59
```
for @ns.rotor(2 => -1, :partial) -> ($x, $not-last?) {
if $not-last {
say "Got $x";
}
else {
say "$x is last"
}
} 02:00
```
I knew someone had asked this before... 02:05
www.reddit.com/r/perl/comments/3jq...onalities/ 02:06
7 years ago it seems. Wow, how the time flies.
03:57 Heptite left
Kaiepi taking elements from the end of an array requires eagerness in general. it should be ok to just: 05:25
```
my @ns = lazy 1,2,1,2,3,1,2;
for @ns.eager.kv -> $i, $n {
if $i == @ns.end {
say "$n is the last value";
}
say $n × 2;
}
```
elcaro What I meant was that the `.kv` method is less nice for certain sequences... like `for 'file'.IO.lines -> $line { ... }`. l use this syntax a lot and it's very nice, but to use this`.kv` trick, i'd first have to load the lines into an Array. It's no deal breaker... just adds a little extraneous code 05:44
Kaiepi ah 05:45
yabobay i just did a C style loop 06:40
done simple
07:30 frost joined
South the LAST phaser didnt seem beneficial? 08:42
yabobay nope
it was exactly the opposite of what i wanted to do
South rip
09:50 perlsol joined 09:57 perlsol left
gfldex m:``` 11:57
my @fib = 0, 1, * + * … ∞;
.say for flat @fib.head(*-1), @fib.tail;
CATCH { default { say .^name, ': ', .gist } }
```
`.head` and `.tail` deal properly with lazy lists. 11:58
yabobay does tail mean the last element of a list? 12:07
lizmat without an argument, yes 12:08
m: say (1..10).tail(5)
camelia (6 7 8 9 10)
lizmat m: say (1..10).tail
camelia 10
yabobay ooo
lizmat same for head
m: say (1..10).head
camelia 1
lizmat m: say (1..10).head(5)
camelia (1 2 3 4 5)
yabobay why is the raku bot faster on IRC 12:09
that was like, instand
instant*
lizmat perhaps because there are fewer intermediaries than when doing this on Discord? 12:10
gfldex IRC doesn't even know camilia is a bot. Discord does know about bots and it aint fair. Also, the bridge-bot loads a few modules. 12:49
13:18 razetime left, razetime joined 13:39 jgaz joined
Kaiepi discord api is also lol 13:42
like json? come on
at this scale? 13:43
13:47 frost left 15:30 razetime left 15:37 razetime joined 15:55 Heptite joined 18:00 razetime left 19:21 jgaz left 20:01 perlsol joined 20:05 jgaz joined 20:12 perlsol left 22:00 perlsol joined 22:09 perlsol left 22:19 Kaipei left 23:32 Kaipei joined