tweak `slicef` to work with 6.0

pull/3/head
Matthew Butterick 10 years ago
parent b519be7f1d
commit 665ef0b517

@ -141,8 +141,8 @@
(check-equal? (slice-at (range 5) 3) '((0 1 2) (3 4))) (check-equal? (slice-at (range 5) 3) '((0 1 2) (3 4)))
(check-equal? (slice-at (range 5) 3 #t) '((0 1 2))) (check-equal? (slice-at (range 5) 3 #t) '((0 1 2)))
(check-equal? (slicef '(1 2 2 1 1 1 2) odd?) '((1) (2 2) (1 1 1) (2))) (check-equal? (slicef '(0 1 2 0 0 0 3) positive?) '((0) (1 2) (0 0 0) (3)))
(check-equal? (slicef '(1 2 2 1 1 1 2) even?) (slicef '(1 2 2 1 1 1 2) odd?)) (check-equal? (slicef '(0 1 2 0 0 0 3) positive?) (slicef '(0 1 2 0 0 0 3) zero?))
(check-equal? (slicef '(1 (1) (1) 1 1 1 (1)) list?) '((1) ((1) (1)) (1 1 1) ((1)))) (check-equal? (slicef '(1 (1) (1) 1 1 1 (1)) list?) '((1) ((1) (1)) (1 1 1) ((1))))
(check-equal? (slicef '(1 2 3 4 5) list?) '((1 2 3 4 5))) (check-equal? (slicef '(1 2 3 4 5) list?) '((1 2 3 4 5)))

@ -13,11 +13,19 @@
(define/typed+provide (slicef xs pred) (define/typed+provide (slicef xs pred)
(All (A) ((Listof A) (A -> Boolean) -> (Listof (Listof A)))) (All (A) ((Listof A) (A -> Boolean) -> (Listof (Listof A))))
(let loop ([xs xs][acc : (Listof (Listof A)) empty]) (define-values (last-list list-of-lists last-negating)
(if (empty? xs) (for/fold: ([current-list : (Listof A) empty]
(reverse acc) [list-of-lists : (Listof (Listof A)) empty]
(let-values ([(cdr-matches rest) (splitf-at (cdr xs) (if (pred (car xs)) pred (λ([x : A]) (not (pred x)))))]) [negating? : Boolean #f])
(loop rest (cons (cons (car xs) cdr-matches) acc)))))) ([x (in-list xs)])
(define current-pred (if negating? (λ: ([x : A]) (not (pred x))) pred))
(if (current-pred x)
(values (cons x current-list) list-of-lists negating?)
(values (cons x null) (if (not (empty? current-list))
(cons (reverse current-list) list-of-lists)
list-of-lists) (not negating?)))))
(reverse (cons (reverse last-list) list-of-lists)))
(define/typed+provide slicef-at (define/typed+provide slicef-at
;; with polymorphic function, use cased typing to simulate optional position arguments ;; with polymorphic function, use cased typing to simulate optional position arguments
@ -29,8 +37,8 @@
[(xs pred force?) [(xs pred force?)
(define-values (last-list list-of-lists) (define-values (last-list list-of-lists)
(for/fold: (for/fold:
([current-list : (Listof A) empty][list-of-lists : (Listof (Listof A)) empty]) ([current-list : (Listof A) empty][list-of-lists : (Listof (Listof A)) empty])
([x (in-list xs)]) ([x (in-list xs)])
(if (pred x) (if (pred x)
(values (cons x null) (if (not (empty? current-list)) (values (cons x null) (if (not (empty? current-list))
(cons (reverse current-list) list-of-lists) (cons (reverse current-list) list-of-lists)
@ -47,7 +55,7 @@
(All (A) ((Listof A) (A -> Boolean) -> (Listof (Listof A)))) (All (A) ((Listof A) (A -> Boolean) -> (Listof (Listof A))))
(define-values (last-list list-of-lists) (define-values (last-list list-of-lists)
(for/fold: ([current-list : (Listof A) empty][list-of-lists : (Listof (Listof A)) empty]) (for/fold: ([current-list : (Listof A) empty][list-of-lists : (Listof (Listof A)) empty])
([x (in-list xs)]) ([x (in-list xs)])
(if (pred x) (if (pred x)
(values empty (cons (reverse (cons x current-list)) list-of-lists)) (values empty (cons (reverse (cons x current-list)) list-of-lists))
(values (cons x current-list) list-of-lists)))) (values (cons x current-list) list-of-lists))))
@ -66,7 +74,7 @@
[(xs len force?) [(xs len force?)
(define-values (last-list list-of-lists) (define-values (last-list list-of-lists)
(for/fold: ([current-list : (Listof A) empty][list-of-lists : (Listof (Listof A)) empty]) (for/fold: ([current-list : (Listof A) empty][list-of-lists : (Listof (Listof A)) empty])
([x (in-list xs)][i (in-naturals)]) ([x (in-list xs)][i (in-naturals)])
(if (= (modulo (add1 i) len) 0) (if (= (modulo (add1 i) len) 0)
(values empty (cons (reverse (cons x current-list)) list-of-lists)) (values empty (cons (reverse (cons x current-list)) list-of-lists))
(values (cons x current-list) list-of-lists)))) (values (cons x current-list) list-of-lists))))
@ -79,7 +87,7 @@
(All (A) ((Listof A) (A -> Boolean) -> (Listof (Listof A)))) (All (A) ((Listof A) (A -> Boolean) -> (Listof (Listof A))))
(define-values (last-list list-of-lists) (define-values (last-list list-of-lists)
(for/fold: ([current-list : (Listof A) empty][list-of-lists : (Listof (Listof A)) empty]) (for/fold: ([current-list : (Listof A) empty][list-of-lists : (Listof (Listof A)) empty])
([x (in-list xs)]) ([x (in-list xs)])
(if (pred x) (if (pred x)
(values empty (if (not (empty? current-list)) (values empty (if (not (empty? current-list))
(cons (reverse current-list) list-of-lists) (cons (reverse current-list) list-of-lists)

Loading…
Cancel
Save