emacs-devel
[Top][All Lists]
Advanced

[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: Fri, 30 Jan 2015 16:38:24 +0100

On Fri, Jan 30, 2015 at 11:21 AM, Nicolas Petton <address@hidden> wrote:
>
> Oleh Krehel <address@hidden> writes:
>
>> Hi Nicolas,
>>
>> On Thu, Jan 29, 2015 at 11:06 PM, Nicolas Petton <address@hidden> wrote:
>>
>>> Sure, `seq-range' would be a convenient way to create a sequence of
>>> numbers. A simple implementation could be:
>>>
>>> (defun seq-range (start end)
>>>   (let ((lst nil))
>>>     (while (< start end)
>>>       (push end lst)
>>>       (setq end (1- end)))
>>>     lst))
>>
>> This is just `number-sequence' from subr.el.
>
> Indeed :)
>
> Nico
> --
> Nicolas Petton
> http://nicolas-petton.fr
>

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?

Oleh



reply via email to

[Prev in Thread] Current Thread [Next in Thread]