On 6/19/2019, Felix wrote:
I have this function:
(use-module (ice-9 match))
(define (chunk4 obj)
(let chunker ((in obj) (out '()))
(match in
((a b c d . rest) (chunker rest (cons (list a b c d) out)))
(() (reverse out))
(tail (reverse (cons tail out))))))
and I am trying to figure out how to generalize it to
but the only solution I have come up with so far is:
(define (chunk obj n)
(let chunker ((in obj) (out '()))
(match in
(() (reverse out))
((? (lambda (l) (< (length l) n)) tail) (reverse (cons tail out)))
Taking the length of the list being chunked isn't efficient.
(long (chunker (drop n long) (cons (take n long) out))))))
Is there a way to write a pattern to match a
list of n items for the second function?
Tested in Gauche Scheme.
(define (chunk items n)
(let go ((in items) (out '()))
(if (null? in)
(reverse out)
(receive
(a b)
(guard (_ (#t (values in '()))) (split-at in n))
(go b (cons a out))))))
On 6/19/2019, Felix wrote:
I have this function:
(use-module (ice-9 match))
(define (chunk4 obj)
(let chunker ((in obj) (out '()))
(match in
((a b c d . rest) (chunker rest (cons (list a b c d) out)))
(() (reverse out))
(tail (reverse (cons tail out))))))
and I am trying to figure out how to generalize it to
but the only solution I have come up with so far is:
(define (chunk obj n)
(let chunker ((in obj) (out '()))
(match in
(() (reverse out))
((? (lambda (l) (< (length l) n)) tail) (reverse (cons tail out)))
Taking the length of the list being chunked isn't efficient.
(long (chunker (drop n long) (cons (take n long) out))))))
Is there a way to write a pattern to match a
list of n items for the second function?
Tested in Gauche Scheme.
(define (chunk items n)
(let go ((in items) (out '()))
(if (null? in)
(reverse out)
(receive
(a b)
(guard (_ (#t (values in '()))) (split-at in n))
(go b (cons a out))))))
(define (chunk items n)
(unfold
null?
(cut take* <> n)
(cut drop* <> n)
items))
(define (chunk obj n)
(let chunker ((in obj) (out '()))
(match in
(() (reverse out))
((? (lambda (l) (< (length l) n)) tail) (reverse (cons tail out)))
(long (chunker (drop n long) (cons (take n long) out))))))
On 6/19/2019, Felix wrote:
(define (chunk obj n)
(let chunker ((in obj) (out '()))
(match in
(() (reverse out))
((? (lambda (l) (< (length l) n)) tail) (reverse (cons tail out)))
(long (chunker (drop n long) (cons (take n long) out))))))
Using generators in Gauche Scheme.
(use srfi-158) ;; generators and accumulators
(define (chunk items n)
(let ((gen (list->generator items)))
(generator->list
(gtake-while pair? (cut generator->list gen n)))))
Cuter and shorter:
(define (chunk items n)
(generator->list
(gtake-while pair?
(cute generator->list (list->generator items) n))))
"cute" is "cut with evaluated non-slots".
Taking the length of the list being chunked isn't efficient.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 294 |
Nodes: | 16 (2 / 14) |
Uptime: | 243:58:03 |
Calls: | 6,626 |
Calls today: | 2 |
Files: | 12,175 |
Messages: | 5,320,318 |