Maybe something like this:
: duplicates-by ( seq quot: ( elt -- key ) -- seq' )
HS{ } clone '[ @ _ ?adjoin ] filter ; inline
Then you can use it:
IN: scratchpad { 1 2 3 4 5 } [ 2/ ] duplicates-by
{ 1 2 4 }
IN: scratchpad sequence-of-tuples [ hash>> ] duplicates-by
It would keep the first element that matches by key and drop all the
subsequent ones.
On Fri, Nov 18, 2016 at 6:36 AM, Alexander Ilin <[email protected]> wrote:
> Hello, all!
>
> I have an interesting little task for you today.
>
> Let's say you have a sequence of tuples, and you want to remove all
> tuples with duplicate ids, so that in the new sequence there is only one
> tuple with each id.
>
> Here's my solution:
>
> TYPED: dedupe-by-hash ( seq: sequence -- seq: sequence )
> dup [ hash>> ] map >hash-set [
> [ hash>> ] dip
> [ in? ] [ delete ] 2bi
> ] curry filter ;
>
> This is not the first time I'm solving this task, and I begun to wonder
> - is there something similar in the Factor library?
>
> Is this the simplest/most efficient implementation?
>
> Is it possible to generalize it to work for any slot like so:
>
> TYPED: dedupe-by-slot ( seq slot -- seq ) ?
>
> If this code is not in the standard library, how about adding it? Seems
> pretty useful, and not too trivial.
>
> What do you say?
>
> ---=====---
> Александр
>
> ------------------------------------------------------------
> ------------------
> _______________________________________________
> Factor-talk mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/factor-talk
>
------------------------------------------------------------------------------
_______________________________________________
Factor-talk mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/factor-talk