Proposal: add `Enum.take(collection, :infinity)`

4 views
Skip to first unread message

Aleix Matuig

unread,
Feb 21, 2026, 2:55:28 AM (yesterday) Feb 21
to elixir-l...@googlegroups.com
In most cases `:infinity` is treated as an pos-integer-like value (timeouts etc.)

While dealing with long collections (lists) one might be interested in the user-configured length of the collection head to return. This might happen when the collection has been previously ordered and the tail is of lesser value compared to the head.

In such cases I find myself writing

```
|> then(fn
  {collection, :infinity} -> collection
  {collection, n} when is_integer(n) -> Enum.take(collection, n)
end)
```

I think making `Enum.take/2` to accept `:infinity` as the second argument would bring no harm. It could simply return back the `Enumerable.t()`.

WDYT? I am all in for providing a PR if accepted.

--
AM
Reply all
Reply to author
Forward
0 new messages