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 17:04:34 +0100

On Fri, Jan 30, 2015 at 4:59 PM, Nicolas Richard
<address@hidden> wrote:
> Oleh Krehel <address@hidden> writes:
>> Is this already somewhere?
>
> IDK, but I often wrote this kind of snippet for that purpose :
>
> (defun yf/seq-group-by (fn lst)
>   (let ((hash (make-hash-table :test 'equal)))
>     (dolist (elm lst)
>       (push elm (gethash (funcall fn elm) hash)))
>     hash))
>
> (yf/seq-group-by
>  #'car
>  '(("a" 1)
>    ("b" 2)
>    ("b" 5)
>    ("c" 1)))
> => #s(hash-table size 65 test equal rehash-size 1.5 rehash-threshold 0.8 data
>             ("a"
>              (("a" 1))
>              "b"
>              (("b" 5)
>               ("b" 2))
>              "c"
>              (("c" 1))))
>
> --
> Nicolas Richard

Mine's faster:

    (defmacro util-timeit (expr)
      (let ((t-beg (float-time))
            (res (dotimes (i 10000)
                   (eval expr)))
            (t-end (float-time)))
        (/
         (- t-end t-beg)
         10000)))

    (util-timeit (yf/seq-group-by #'car tmp))
    0.0003461523771286011
    0.00033148398399353025
    0.00032957537174224856


    (util-timeit (seq-group-by #'car tmp))
    0.00019227066040039062
    0.00018899762630462645
    0.0001775247573852539



reply via email to

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