Re: [racket-users] right abstraction for this?

2015-06-18 Thread Geoffrey Knauth
Just the thing I need today for a Racket web-powered table of database info, 
thanks all.  --Geoff

On Thursday, June 11, 2015 at 1:55:35 PM UTC-4, Greg Hendershott wrote:
> IIUC in SQL this would simply be:
> 
> SELECT student, AVG(rating)
> FROM scores
> GROUP BY student
> 
> Apparently a DSL for querying tables can be handy. :)
> 
> 
> The Racket equivalent for the special case of a 2-column table (a
> hash-table) could be something like:
> 
> (define (sql-ish-aggregate-group-by f xs)
>   (for/hash ([(k v) (in-hash (for/fold ([ht (hash)]) ; a "gather"
>([x (in-list xs)])
>(match-define (list k v) x)
>(hash-update ht k (λ (vs) (cons v vs)) '(])
> (cons k (f v
> 
> 
> "Group by" seems to be a Rorschach phrase; overloaded meanings.
> Clojure has a group-by that differs from SQL or unstable/list.  I
> don't know Clojure well enough to code golf this.

-- 
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.


Re: [racket-users] right abstraction for this?

2015-06-11 Thread Vincent St-Amour
As Greg points out, it was directly inspired by SQL's `group-by`. I've
found it really handy when thinking about data in a relational way.

Anyone opposed to moving it to `racket/list`? That would make it more
discoverable?

Vincent



At Thu, 11 Jun 2015 11:17:26 -0700,
'John Clements' via users-redirect wrote:
> 
> 
> > On Jun 11, 2015, at 10:19 AM, Stephen Chang  wrote:
> > 
> > Would any of the functions in unstable/list help? For example,
> 
> Ah, ‘group-by’ is very nice, yes. Looks like that was Vincent’s? Many thanks, 
> Vincent!
> 
> 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.


Re: [racket-users] right abstraction for this?

2015-06-11 Thread 'John Clements' via users-redirect

> On Jun 11, 2015, at 10:19 AM, Stephen Chang  wrote:
> 
> Would any of the functions in unstable/list help? For example,

Ah, ‘group-by’ is very nice, yes. Looks like that was Vincent’s? Many thanks, 
Vincent!

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.


Re: [racket-users] right abstraction for this?

2015-06-11 Thread Greg Hendershott
IIUC in SQL this would simply be:

SELECT student, AVG(rating)
FROM scores
GROUP BY student

Apparently a DSL for querying tables can be handy. :)


The Racket equivalent for the special case of a 2-column table (a
hash-table) could be something like:

(define (sql-ish-aggregate-group-by f xs)
  (for/hash ([(k v) (in-hash (for/fold ([ht (hash)]) ; a "gather"
   ([x (in-list xs)])
   (match-define (list k v) x)
   (hash-update ht k (λ (vs) (cons v vs)) '(])
(cons k (f v


"Group by" seems to be a Rorschach phrase; overloaded meanings.
Clojure has a group-by that differs from SQL or unstable/list.  I
don't know Clojure well enough to code golf this.

-- 
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.


Re: [racket-users] right abstraction for this?

2015-06-11 Thread Stephen Chang
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
 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.


[racket-users] right abstraction for this?

2015-06-11 Thread 'John Clements' via users-redirect
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.