Subject: bug#56521: Add 'take' list operation [PATCH]

From: Lars Ingebrigtsen <[email-masked]>
Date: Wed, 13 Jul 2022 00:51:28 +0200

Mattias Engdegård <[email-masked]> writes:

> A basic list operation that is often useful is 'take', that extracts
> the N first elements of a list. The attached patch adds 'take' as a
> built-in function.

I'm not against adding this, but I'm not sure of the utility.  That is,
if you have performance critical code, you don't use `take', because it
generates much garbage -- instead you loop over a list, taking the
elements a few at a time explicitly.  And if you don't have performance
critical code, then why not use `seq-take'?

So a fast `take' seems to me to land in that uncanny zone of "you don't
really need that", but there may, of course, be usage scenarios that I'm
not aware of.

> I included `ntake` in the patch for reference and because it's Lisp
> tradition to have faster destructive 'n-' variants of list functions.

I think `ntake' would be a more popular function than `take', really --
because shortening a list is efficient, and something that's not
uncommon to do, and it's a bother to write

  (setcdr (nthcdr 10 list) nil)

especially since that may bug out if the list is shorter.  But as you
say, the Lisp version of ntake may well be faster than the C version, so
perhaps just stick it in subr.el instead?

But I'm not sure about the `ntake' name.  😀  It's natural in one way,
but since the main point is that the list is shortened, then perhaps
`list-shorten' (or something like that) is more appropriate.

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no