John, thank you very much! : )
Really helpful stuff!
 
18.11.2016, 18:13, "John Benediktsson" <mrj...@gmail.com>:
P.S., Hah I should have called it unique-by, it's too early in the morning!
 
P.P.S., I committed this word into sets.extras, with one small change besides the name which is to size the hash-set capacity by the length of the sequence.
 
On Fri, Nov 18, 2016 at 6:54 AM, John Benediktsson <mrj...@gmail.com> wrote:
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 <ajs...@yandex.ru> 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
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk
,

------------------------------------------------------------------------------

,

_______________________________________________
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk

 
 
---=====---
Александр
 
------------------------------------------------------------------------------
_______________________________________________
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk

Reply via email to