Zulip Chat Archive
Stream: new members
Topic: Suffix of list
Marcus Rossel (Mar 10 2021 at 16:46):
Is there a way to get a suffix from a list, or more generally a slice of a list?
Something like:
[A, B, C, D, E, F].suffix 3 = [D, E, F]
[A, B].suffix 3 = []
Yakov Pechersky (Mar 10 2021 at 16:48):
A combination of take
and reverse
would work. Or if you know the length already, drop
.
Yakov Pechersky (Mar 10 2021 at 16:49):
I don't understand how [A, B].suffix 3 = []
is true though
Yakov Pechersky (Mar 10 2021 at 16:49):
For any reasonable suffix
Yakov Pechersky (Mar 10 2021 at 16:49):
I'm thinking python-style
[a, b][-3:]
Bryan Gin-ge Chen (Mar 10 2021 at 16:51):
Maybe taking the second component of docs#list.split_at is what you want?
Marcus Rossel (Mar 10 2021 at 16:52):
Bryan Gin-ge Chen said:
Maybe taking the second component of docs#list.split_at is what you want?
That looks promising:
[1, 2, 3, 4].split_at 5 = ([1, 2, 3, 4], [])
Bryan Gin-ge Chen (Mar 10 2021 at 16:56):
It returns []
:
import data.list.defs
#eval (['A','B','C','D','E','F'].split_at 3).snd
-- ['D', 'E', 'F']
#eval (['A','B'].split_at 3).snd
-- []
Yakov Pechersky (Mar 10 2021 at 16:57):
docs#list.split_at_eq_take_drop
Yakov Pechersky (Mar 10 2021 at 16:57):
So provably equal to my first definition
Bryan Gin-ge Chen (Mar 10 2021 at 16:58):
Yep, docs#list.drop is what you want
import data.list.defs
#eval (['A','B','C','D','E','F'].drop 3)
-- ['D', 'E', 'F']
#eval (['A','B'].drop 3)
-- []
Marcus Rossel (Mar 10 2021 at 16:59):
Ohhh, perfect :)
Yakov Pechersky (Mar 10 2021 at 17:00):
Oh, you mean suffix 3
as in suffix after removing 3 elements
. Yes, that's drop
Marcus Rossel (Mar 10 2021 at 17:01):
Well I actually meant suffix as _the last n elements_, but drop
turns out to work better for my purposes :)
Last updated: Dec 20 2023 at 11:08 UTC