Zulip Chat Archive

Stream: new members

Topic: Suffix of list


view this post on Zulip 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 = []

view this post on Zulip Yakov Pechersky (Mar 10 2021 at 16:48):

A combination of take and reverse would work. Or if you know the length already, drop.

view this post on Zulip Yakov Pechersky (Mar 10 2021 at 16:49):

I don't understand how [A, B].suffix 3 = [] is true though

view this post on Zulip Yakov Pechersky (Mar 10 2021 at 16:49):

For any reasonable suffix

view this post on Zulip Yakov Pechersky (Mar 10 2021 at 16:49):

I'm thinking python-style

[a, b][-3:]

view this post on Zulip Bryan Gin-ge Chen (Mar 10 2021 at 16:51):

Maybe taking the second component of docs#list.split_at is what you want?

view this post on Zulip 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], [])

view this post on Zulip 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
-- []

view this post on Zulip Yakov Pechersky (Mar 10 2021 at 16:57):

docs#list.split_at_eq_take_drop

view this post on Zulip Yakov Pechersky (Mar 10 2021 at 16:57):

So provably equal to my first definition

view this post on Zulip 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)
-- []

view this post on Zulip Marcus Rossel (Mar 10 2021 at 16:59):

Ohhh, perfect :)

view this post on Zulip Yakov Pechersky (Mar 10 2021 at 17:00):

Oh, you mean suffix 3 as in suffix after removing 3 elements. Yes, that's drop

view this post on Zulip 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: May 16 2021 at 05:21 UTC