[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Would seq-range and seq-mapcat be useful?
From: |
Oleh Krehel |
Subject: |
Re: Would seq-range and seq-mapcat be useful? |
Date: |
Mon, 02 Feb 2015 08:49:44 +0100 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/25.0.50 (gnu/linux) |
Nicolas Petton <address@hidden> writes:
> Oleh Krehel <address@hidden> writes:
>
>> I'd like to have this:
>>
>> (defun seq-group-by (fn lst)
>> (nreverse
>> (cl-reduce
>> (lambda (acc it)
>> (let* ((key (funcall fn it))
>> (cell (assoc key acc)))
>> (if cell
>> (setcdr cell (push it (cdr cell)))
>> (push (list key it) acc))
>> acc))
>> lst
>> :initial-value nil)))
>>
>> (seq-group-by
>> #'car
>> '(("a" 1)
>> ("b" 2)
>> ("b" 5)
>> ("c" 1)))
>> ;; (("a" ("a" 1))
>> ;; ("b" ("b" 5)
>> ;; ("b" 2))
>> ;; ("c" ("c" 1)))
>> (seq-group-by
>> #'cadr
>> '(("a" 1)
>> ("b" 2)
>> ("b" 5)
>> ("c" 1)))
>> ;; ((1 ("c" 1)
>> ;; ("a" 1))
>> ;; (2 ("b" 2))
>> ;; (5 ("b" 5)))
>>
>> Is this already somewhere?
>
> I don't think it is :)
>
> I think I'm going with the following 3 additions to seq.el: `seq-mapcat',
> `seq-group-by', and `seq-slice' (or `seq-partition'?).
>
> What do you think?
>
I like `seq-group-by`.
I'm not sure about `seq-mapcat`, since there is `cl-mapcan` (although
it's destructive), I guess `seq-mapcat` would be fine.
And I would prefer the name `seq-partition` instead of `seq-slice`,
since `partition' is a Clojure name with same effect, and slice means
something different in Python.
Oleh