Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

missing "lfilter" in tcl

66 views
Skip to first unread message

Andreas Otto

unread,
Apr 22, 2018, 5:48:52 AM4/22/18
to
Hi,

tcl has a set of wonderful "list" commands… all starting with "l"… but ONE command seems to be missing.

usage: lfilter list repeat-range repeat-index...

goal: to get a sublist out of a tcl list with repeats

example 1:

set mylist {a 1 b 2 c 3}
> range = "2" -> "a 1" or "b 2" or "c 3"

lfilter $mylist 2 1
> 1 2 3

example 1 would be a replacement for

set ret [list]
foreach {dummy elm} $mylist {
lappend ret $elm
}

repeat-range => {dummy elm}
repeat-index => elm


=> all this can be expanded to sublists as well…


mfg AO

Gerald Lester

unread,
Apr 22, 2018, 10:22:21 AM4/22/18
to
Sounds great, TIP it and provide a reference implementation in the TIP!


--
+----------------------------------------------------------------------+
| Gerald W. Lester, President, KNG Consulting LLC |
| Email: Gerald...@kng-consulting.net |
+----------------------------------------------------------------------+

Rich

unread,
Apr 22, 2018, 10:48:29 AM4/22/18
to
Gerald Lester <Gerald...@kng-consulting.net> wrote:
> On 04/22/2018 04:48 AM, Andreas Otto wrote:
>> Hi,
>>
>> tcl has a set of wonderful "list" commands? all starting with "l"? but ONE command seems to be missing.
>>
>> usage: lfilter list repeat-range repeat-index...
>>
>> goal: to get a sublist out of a tcl list with repeats
>>
>> example 1:
>>
>> set mylist {a 1 b 2 c 3}
>>> range = "2" -> "a 1" or "b 2" or "c 3"
>>
>> lfilter $mylist 2 1
>>> 1 2 3
>>
>> example 1 would be a replacement for
>>
>> set ret [list]
>> foreach {dummy elm} $mylist {
>> lappend ret $elm
>> }
>>
>> repeat-range => {dummy elm}
>> repeat-index => elm
>>
>>
>> => all this can be expanded to sublists as well?
>
> Sounds great, TIP it and provide a reference implementation in the TIP!

To the OP, I would also recommend not calling it lfilter. When I see
lfilter I think it should be doing something like what the current lmap
does, pass the elements through a bit of code, only where the bit of
code 'filters' out certian elements based upon a condition. So your
example would simply be a filter on a list where the remainder after
dividing the list index value of the element by 2 is 1. Your example
would be an "lextract" to me, but not a full generic 'filter', which is
what I think a function named 'filter' should be doing, generic
filtering, not a specific particular subset of 'filtering'.

jda...@gmail.com

unread,
Apr 22, 2018, 12:27:33 PM4/22/18
to

> usage: lfilter list repeat-range repeat-index...
>
> goal: to get a sublist out of a tcl list with repeats
>
> example 1:
>
> set mylist {a 1 b 2 c 3}
> > range = "2" -> "a 1" or "b 2" or "c 3"
>
> lfilter $mylist 2 1
> > 1 2 3
>
> example 1 would be a replacement for
>
> set ret [list]
> foreach {dummy elm} $mylist {
> lappend ret $elm
> }

lfiter (or lextract) looks closer to:

lmap {dummy elm} $mylist {set elem}

however that leaves the dummy and elm variables defined (with the last two list entries in this case).

>
> repeat-range => {dummy elm}
> repeat-index => elm
>
>
> => all this can be expanded to sublists as well…


I'm not clear about how it would expand to sublists, or what the use case would be.

Dave B

Rich

unread,
Apr 22, 2018, 4:31:13 PM4/22/18
to
jda...@gmail.com wrote:
>
>> usage: lfilter list repeat-range repeat-index...
>>
>> goal: to get a sublist out of a tcl list with repeats
>>
>> example 1:
>>
>> set mylist {a 1 b 2 c 3}
>> > range = "2" -> "a 1" or "b 2" or "c 3"
>>
>> lfilter $mylist 2 1
>> > 1 2 3
>>
>> example 1 would be a replacement for
>>
>> set ret [list]
>> foreach {dummy elm} $mylist {
>> lappend ret $elm
>> }
>
> lfiter (or lextract) looks closer to:
>
> lmap {dummy elm} $mylist {set elem}
>
> however that leaves the dummy and elm variables defined (with the
> last two list entries in this case).

If you have lmap, you have 8.6, so you also have apply, and you can use
apply to avoid creating any variables in the local scope:

% interp alias {} lextract {} ::apply {{mylist} {lmap {_ elm} $mylist {set elm}}}
% set thelist [list 1 2 3 4]
1 2 3 4
% lextract $thelist
2 4
%

briang

unread,
Apr 22, 2018, 6:26:11 PM4/22/18
to
Actually, it already exist in the form: lsearch -inline -all -not ...
I have used this pattern to great effect in a number of situations.

-Brian

Christian Gollwitzer

unread,
Apr 23, 2018, 4:24:58 AM4/23/18
to
Am 22.04.18 um 11:48 schrieb Andreas Otto:
> tcl has a set of wonderful "list" commands… all starting with "l"… but ONE command seems to be missing.
>
> usage: lfilter list repeat-range repeat-index...
>
> goal: to get a sublist out of a tcl list with repeats
>

I think that all those l... functions could be made obsolete by a
powerful list comprehension function. There are some ideas on the wiki:

http://wiki.tcl.tk/12574

I think we should rather generalize it and TIP a lcomp type
functionality - it belongs to the core of other list heavy languages
like Haskell and Python with great success.

Christian
0 new messages