Change the signature for group-by.

Based on discussion with Eli.
This commit is contained in:
Vincent St-Amour 2013-09-28 15:00:24 -04:00
parent 5f2dfa5144
commit a963e42fef
5 changed files with 21 additions and 20 deletions

View File

@ -187,7 +187,7 @@
(printf "splines=\"true\"\n") ; polyline kinda works too, maybe
;; cluster nodes per module, to show boundaries
(for ([module-nodes (in-list (group-by equal? nodes #:key node->module))]
(for ([module-nodes (in-list (group-by node->module nodes))]
[cluster-idx (in-naturals 1)])
(define known-module? (node->module (first module-nodes)))
;; don't cluster nodes for which we have no module info

View File

@ -65,8 +65,8 @@
(displayln "\nBY CONTRACT\n")
(define samples-by-contract
(sort (group-by equal? live-contract-samples
#:key (lambda (x) (blame-contract (car x))))
(sort (group-by (lambda (x) (blame-contract (car x)))
live-contract-samples)
> #:key length #:cache-keys? #t))
(for ([c (in-list samples-by-contract)])
(define representative (caar c))
@ -78,8 +78,9 @@
(define representative (caar g))
(print-contract/loc representative)
(for ([x (sort
(group-by equal? g
#:key (lambda (x) (blame-value (car x)))) ; callee source, maybe
(group-by (lambda (x)
(blame-value (car x))) ; callee source, maybe
g)
> #:key length)])
(printf " ~a\n ~a ms\n"
(blame-value (caar x))
@ -88,8 +89,8 @@
(define samples-by-contract-by-caller
(for/list ([g (in-list samples-by-contract)])
(sort (group-by equal? (map sample-prune-stack-trace g)
#:key cddr) ; pruned stack trace
(sort (group-by cddr ; pruned stack trace
(map sample-prune-stack-trace g))
> #:key length)))
(displayln "\nBY CALLER\n")

View File

@ -158,10 +158,10 @@
;; stamourv added:
;; (y y -> bool) (listof x) #:key (x -> y) -> (listof (listof x))
;; (x -> y) (listof x) [(y y -> bool)] -> (listof (listof x))
;; groups together elements that are considered equal
;; =? should be reflexive, transitive and commutative
(define (group-by =? l #:key [key values])
(define (group-by key l [=? equal?])
(for/fold ([res '()]) ; list of lists
([elt (in-list l)])
(let loop ([classes res] ; "zipper" of the equivalence classes
@ -180,7 +180,7 @@
(cons (car classes) rev-classes))]))))
(provide/contract
[group-by (->* (procedure? list?) (#:key procedure?)
[group-by (->* (procedure? list?) (procedure?)
list?)])
;; endobson added:

View File

@ -161,16 +161,16 @@ for which @racket[pred] produces a true value.
@addition{Vincent St-Amour}
@defproc[(group-by [=? (-> B B any/c)]
@defproc[(group-by [extract-key (-> A B)]
[lst (listof A)]
[#:key extract-key (-> A B) values])
[=? (-> B B any/c) equal?])
(listof (listof A))]{
Groups the given list into equivalence classes, with equivalence being
determined by @racket[=?].
@examples[#:eval the-eval
(group-by = '(1 2 1 2 54 2 5 43 7 2 643 1 2 0))
(group-by (lambda (x) (modulo x 3)) '(1 2 1 2 54 2 5 43 7 2 643 1 2 0))
]
}

View File

@ -39,14 +39,14 @@
(check-equal? bs '(4 5 6))
(check-equal? cs '(7 8 9)))))
(test-suite "group-by"
(check-equal? (group-by = '(1 4 2 56 2 3))
(check-equal? (group-by values '(1 4 2 56 2 3))
'((3) (56) (2 2) (4) (1)))
(check-equal? (group-by = '(1 2 1 2 54 2 5 43 7 2 643 1 2 0))
(check-equal? (group-by values '(1 2 1 2 54 2 5 43 7 2 643 1 2 0))
'((0) (2 2 2 2 2) (7) (43) (5) (54) (643) (1 1 1)))
(check-equal? (group-by (lambda (x y) (or (and (even? x) (even? y))
(and (odd? x) (odd? y))))
'(1 4 2 56 2 3))
(check-equal? (group-by values
'(1 4 2 56 2 3)
(lambda (x y) (or (and (even? x) (even? y))
(and (odd? x) (odd? y)))))
'((2 56 2 4) (3 1)))
(check-equal? (group-by = '((1 a) (4 b) (2 c) (56 d) (2 e) (3 f))
#:key car)
(check-equal? (group-by car '((1 a) (4 b) (2 c) (56 d) (2 e) (3 f)))
'(((3 f)) ((56 d)) ((2 e) (2 c)) ((4 b)) ((1 a)))))))