[proposal] limit(n, ...)

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

[proposal] limit(n, ...)

Soni "They/Them" L.
it'd be nice to have a limit() function companion to select() where select skips the first n and limit skips everything after n.

e.g. limit(1) --> nothing, limit(1, 1, 2, 3) --> 1, etc
Reply | Threaded
Open this post in threaded view
|

Re: [proposal] limit(n, ...)

Egor Skriptunoff-2
On Thu, Oct 10, 2019 at 2:54 PM Soni L. wrote:
it'd be nice to have a limit() function companion to select() where select skips the first n and limit skips everything after n.

e.g. limit(1) --> nothing, limit(1, 1, 2, 3) --> 1, etc



1) Please note that you can get the last N elements by invoking
select(-N, ...)
You probably can change the order of parameters in your functions to switch from "get first N" approach to "get last N".

2) Why asking for introducing new function instead of overloading old one?
For example:
select("range", 2, 3, "a", "b", "c", "d")  -->  "b", "c"

3) Extracting a subtuple from a tuple would be practically useless without ability to concatenate tuples.
Reply | Threaded
Open this post in threaded view
|

Re: [proposal] limit(n, ...)

Soni "They/Them" L.


On Thu, Oct 10, 2019, 09:50 Egor Skriptunoff <[hidden email]> wrote:
On Thu, Oct 10, 2019 at 2:54 PM Soni L. wrote:
it'd be nice to have a limit() function companion to select() where select skips the first n and limit skips everything after n.

e.g. limit(1) --> nothing, limit(1, 1, 2, 3) --> 1, etc



1) Please note that you can get the last N elements by invoking
select(-N, ...)
You probably can change the order of parameters in your functions to switch from "get first N" approach to "get last N".

Yes I can use a coroutine to reverse the order, use select, then use another coroutine to reverse the order again.


2) Why asking for introducing new function instead of overloading old one?
For example:
select("range", 2, 3, "a", "b", "c", "d")  -->  "b", "c"

Uhh no


3) Extracting a subtuple from a tuple would be practically useless without ability to concatenate tuples.

We don't have tuples.
Reply | Threaded
Open this post in threaded view
|

Re: [proposal] limit(n, ...)

Daniel Silverstone
In reply to this post by Soni "They/Them" L.
On Thu, Oct 10, 2019 at 08:54:08 -0300, Soni L. wrote:
> it'd be nice to have a limit() function companion to select() where select
> skips the first n and limit skips everything after n.
>
> e.g. limit(1) --> nothing, limit(1, 1, 2, 3) --> 1, etc

In most languages this would be called "take" and it is indeed a useful
thing to be able to do.

I'd guess an implementation might look like:

```c
static int
lua_take(lua_State *L)
{
    lua_Integer n_items = MIN(luaL_checkinteger(L, 1), lua_gettop(L, -1) - 1);
    lua_remove(L, 1);
    lua_settop(L, n_items);
    return n_items;
}
```

It has been a while since I wrote Lua/C code though, so who knows how badly
wrong that is :D

For your first example, the stack is [1] on input, so n_items is set to MIN(1,
1 - 1) which will be 0.  We remove the 1, set top to 0 and return 0 items,
matching your limit(1) --> nothing.

For your second example, the stack is [1 1 2 3] on input, so n_items is set to
MIN(1, 4 - 1) which will be 1.  Remove the 1, truncate to 1 items, return it.
Works again.

D.

--
Daniel Silverstone                         http://www.digital-scurf.org/
PGP mail accepted and encouraged.            Key Id: 3CCE BABE 206C 3B69

Reply | Threaded
Open this post in threaded view
|

Re: [proposal] limit(n, ...)

Andrew Gierth
>>>>> "Daniel" == Daniel Silverstone <[hidden email]> writes:

 Daniel> For your first example, the stack is [1] on input, so n_items
 Daniel> is set to MIN(1, 1 - 1) which will be 0. We remove the 1, set
 Daniel> top to 0 and return 0 items, matching your limit(1) -->
 Daniel> nothing.

 Daniel> For your second example, the stack is [1 1 2 3] on input, so
 Daniel> n_items is set to MIN(1, 4 - 1) which will be 1. Remove the 1,
 Daniel> truncate to 1 items, return it. Works again.

You don't actually need to remove the count, just truncate the stack to
n+1 items and return n.

--
Andrew.