Add a utility to group list values
This is useful for some queries with duplicated parts of the results.
This commit is contained in:
parent
48070e4a99
commit
5124fbc471
1 changed files with 16 additions and 1 deletions
|
|
@ -1,12 +1,14 @@
|
||||||
(define-module (guix-data-service model utils)
|
(define-module (guix-data-service model utils)
|
||||||
#:use-module (srfi srfi-1)
|
#:use-module (srfi srfi-1)
|
||||||
#:use-module (ice-9 vlist)
|
#:use-module (ice-9 vlist)
|
||||||
|
#:use-module (ice-9 receive)
|
||||||
#:use-module (squee)
|
#:use-module (squee)
|
||||||
#:export (quote-string
|
#:export (quote-string
|
||||||
value->quoted-string-or-null
|
value->quoted-string-or-null
|
||||||
exec-query->vhash
|
exec-query->vhash
|
||||||
two-lists->vhash
|
two-lists->vhash
|
||||||
deduplicate-strings))
|
deduplicate-strings
|
||||||
|
group-list-by-first-n-fields))
|
||||||
|
|
||||||
(define (quote-string s)
|
(define (quote-string s)
|
||||||
(string-append "'" s "'"))
|
(string-append "'" s "'"))
|
||||||
|
|
@ -41,3 +43,16 @@
|
||||||
(cons (first pair) result))))
|
(cons (first pair) result))))
|
||||||
'()
|
'()
|
||||||
(sort strings string<?)))
|
(sort strings string<?)))
|
||||||
|
|
||||||
|
(define (group-list-by-first-n-fields n lists)
|
||||||
|
(fold (lambda (lst groups)
|
||||||
|
(receive (key vals)
|
||||||
|
(split-at lst n)
|
||||||
|
(append
|
||||||
|
(alist-delete key groups)
|
||||||
|
`((,key . ,(append
|
||||||
|
(or (assoc-ref groups key)
|
||||||
|
'())
|
||||||
|
(list vals)))))))
|
||||||
|
'()
|
||||||
|
lists))
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue