Would any of the functions in unstable/list help? For example,

#lang racket
(require unstable/list)

(define (gather lst)
  (for/hash ([g (group-by car lst)])
    (values (caar g) (append-map cdr g))))

(gather '((a c) (a d) (b e) (b f)))
; => '#hash((a . (c d)) (b . (e f)))

On Thu, Jun 11, 2015 at 12:25 PM, 'John Clements' via users-redirect
<us...@plt-scheme.org> wrote:
> I write this kind of code all the darn time:
>
> ;; take (listof (list a b)) into (hashof a (listof b))
> (define (gather l)
>   (for/fold ([ht (make-hash)])
>             ([pr l])
>     (hash-set ht (first pr) (cons (second pr)
>                                   (hash-ref ht (first pr) empty)))))
>
> ;; gather the responses into a table:
> (define rating-table (gather responses2))
>
> ;; compute the mean rating for each student
> (for/list ([(student ratings) (in-hash rating-table)])
>   (list student (mean ratings)))
>
> … that is: given an association list, gather them together, then compute 
> (say) the mean of the values. In this case, I’m trying to compute the mean 
> rating for a bunch of students. Very simple.
>
> So… why does it take so much code? I feel like these should be built-in 
> abstractions. There are (at least) two possibilities:
>
> 1) There’s an obvious, built-in way to do this, and I’m just missing it.
> 2) There’s an abstraction that we’re missing, and I feel like Clojure might 
> have it. Or am I just making this up? Getting clojure running is such a pain….
>
> Thanks for any advice!
>
> John
>
> --
> You received this message because you are subscribed to the Google Groups 
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to racket-users+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to