|
|
@ -17,7 +17,7 @@
|
|
|
|
(for/fold: ([current-list : (Listof A) empty]
|
|
|
|
(for/fold: ([current-list : (Listof A) empty]
|
|
|
|
[list-of-lists : (Listof (Listof A)) empty]
|
|
|
|
[list-of-lists : (Listof (Listof A)) empty]
|
|
|
|
[negating? : Boolean #f])
|
|
|
|
[negating? : Boolean #f])
|
|
|
|
([x (in-list xs)])
|
|
|
|
([x (in-list xs)])
|
|
|
|
(define current-pred (if negating? (λ: ([x : A]) (not (pred x))) pred))
|
|
|
|
(define current-pred (if negating? (λ: ([x : A]) (not (pred x))) pred))
|
|
|
|
(if (current-pred x)
|
|
|
|
(if (current-pred x)
|
|
|
|
(values (cons x current-list) list-of-lists negating?)
|
|
|
|
(values (cons x current-list) list-of-lists negating?)
|
|
|
@ -27,35 +27,31 @@
|
|
|
|
(reverse (cons (reverse last-list) list-of-lists)))
|
|
|
|
(reverse (cons (reverse last-list) list-of-lists)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define/typed+provide slicef-at
|
|
|
|
(define/typed+provide (slicef-at xs pred [force? #f])
|
|
|
|
;; with polymorphic function, use cased typing to simulate optional position arguments
|
|
|
|
;; with polymorphic function, use cased typing to simulate optional position arguments
|
|
|
|
(All (A) (case-> ((Listof A) (A -> Boolean) -> (Listof (Listof A)))
|
|
|
|
(All (A) (case-> ((Listof A) (A -> Boolean) -> (Listof (Listof A)))
|
|
|
|
((Listof A) (A -> Boolean) Boolean -> (Listof (Listof A)))))
|
|
|
|
((Listof A) (A -> Boolean) Boolean -> (Listof (Listof A)))))
|
|
|
|
(case-lambda
|
|
|
|
(define-values (last-list list-of-lists)
|
|
|
|
[(xs pred)
|
|
|
|
(for/fold:
|
|
|
|
(slicef-at xs pred #f)]
|
|
|
|
|
|
|
|
[(xs pred force?)
|
|
|
|
|
|
|
|
(define-values (last-list list-of-lists)
|
|
|
|
|
|
|
|
(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)
|
|
|
|
list-of-lists))
|
|
|
|
list-of-lists))
|
|
|
|
(values (cons x current-list) list-of-lists))))
|
|
|
|
(values (cons x current-list) list-of-lists))))
|
|
|
|
(let ([list-of-lists (reverse (if (empty? last-list)
|
|
|
|
(let ([list-of-lists (reverse (if (empty? last-list)
|
|
|
|
list-of-lists
|
|
|
|
list-of-lists
|
|
|
|
(cons (reverse last-list) list-of-lists)))])
|
|
|
|
(cons (reverse last-list) list-of-lists)))])
|
|
|
|
(if (and force? (not (empty? list-of-lists)) (not (pred (caar list-of-lists))))
|
|
|
|
(if (and force? (not (empty? list-of-lists)) (not (pred (caar list-of-lists))))
|
|
|
|
(cdr list-of-lists)
|
|
|
|
(cdr list-of-lists)
|
|
|
|
list-of-lists))]))
|
|
|
|
list-of-lists)))
|
|
|
|
|
|
|
|
|
|
|
|
(define/typed+provide (slicef-after xs pred)
|
|
|
|
(define/typed+provide (slicef-after xs pred)
|
|
|
|
(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))))
|
|
|
@ -64,30 +60,26 @@
|
|
|
|
(cons (reverse last-list) list-of-lists))))
|
|
|
|
(cons (reverse last-list) list-of-lists))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define/typed+provide slice-at
|
|
|
|
(define/typed+provide (slice-at xs len [force? #f])
|
|
|
|
;; with polymorphic function, use cased typing to simulate optional position arguments
|
|
|
|
;; with polymorphic function, use cased typing to simulate optional position arguments
|
|
|
|
(All (A) (case-> ((Listof A) Positive-Integer -> (Listof (Listof A)))
|
|
|
|
(All (A) (case-> ((Listof A) Positive-Integer -> (Listof (Listof A)))
|
|
|
|
((Listof A) Positive-Integer Boolean -> (Listof (Listof A)))))
|
|
|
|
((Listof A) Positive-Integer Boolean -> (Listof (Listof A)))))
|
|
|
|
(case-lambda
|
|
|
|
(define-values (last-list list-of-lists)
|
|
|
|
[(xs len)
|
|
|
|
(for/fold: ([current-list : (Listof A) empty][list-of-lists : (Listof (Listof A)) empty])
|
|
|
|
(slice-at xs len #f)]
|
|
|
|
([x (in-list xs)][i (in-naturals)])
|
|
|
|
[(xs len force?)
|
|
|
|
(if (= (modulo (add1 i) len) 0)
|
|
|
|
(define-values (last-list list-of-lists)
|
|
|
|
(values empty (cons (reverse (cons x current-list)) list-of-lists))
|
|
|
|
(for/fold: ([current-list : (Listof A) empty][list-of-lists : (Listof (Listof A)) empty])
|
|
|
|
(values (cons x current-list) list-of-lists))))
|
|
|
|
([x (in-list xs)][i (in-naturals)])
|
|
|
|
(reverse (if (or (empty? last-list) (and force? (not (= len (length last-list)))))
|
|
|
|
(if (= (modulo (add1 i) len) 0)
|
|
|
|
list-of-lists
|
|
|
|
(values empty (cons (reverse (cons x current-list)) list-of-lists))
|
|
|
|
(cons (reverse last-list) list-of-lists))))
|
|
|
|
(values (cons x current-list) list-of-lists))))
|
|
|
|
|
|
|
|
(reverse (if (or (empty? last-list) (and force? (not (= len (length last-list)))))
|
|
|
|
|
|
|
|
list-of-lists
|
|
|
|
|
|
|
|
(cons (reverse last-list) list-of-lists)))]))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define/typed+provide (filter-split xs pred)
|
|
|
|
(define/typed+provide (filter-split xs pred)
|
|
|
|
(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)
|
|
|
@ -165,17 +157,11 @@
|
|
|
|
(cons tail (loop head (cdr bps)))))))))
|
|
|
|
(cons tail (loop head (cdr bps)))))))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define/typed+provide shift
|
|
|
|
(define/typed+provide (shift xs how-far [fill-item #f] [cycle #f])
|
|
|
|
(All (A) (case-> ((Listof (Option A)) Integer -> (Listof (Option A)))
|
|
|
|
(All (A) (case-> ((Listof (Option A)) Integer -> (Listof (Option A)))
|
|
|
|
((Listof (Option A)) Integer (Option A) -> (Listof (Option A)))
|
|
|
|
((Listof (Option A)) Integer (Option A) -> (Listof (Option A)))
|
|
|
|
((Listof (Option A)) Integer (Option A) Boolean -> (Listof (Option A)))))
|
|
|
|
((Listof (Option A)) Integer (Option A) Boolean -> (Listof (Option A)))))
|
|
|
|
(case-lambda
|
|
|
|
(define abs-how-far (abs how-far))
|
|
|
|
[(xs how-far)
|
|
|
|
|
|
|
|
(shift xs how-far #f #f)]
|
|
|
|
|
|
|
|
[(xs how-far fill-item)
|
|
|
|
|
|
|
|
(shift xs how-far fill-item #f)]
|
|
|
|
|
|
|
|
[(xs how-far fill-item cycle)
|
|
|
|
|
|
|
|
(define abs-how-far (abs how-far))
|
|
|
|
|
|
|
|
(cond
|
|
|
|
(cond
|
|
|
|
[(> abs-how-far (length xs)) (error 'shift "index is too large for list\nindex: ~a\nlist: ~v" how-far xs)]
|
|
|
|
[(> abs-how-far (length xs)) (error 'shift "index is too large for list\nindex: ~a\nlist: ~v" how-far xs)]
|
|
|
|
[(= how-far 0) xs]
|
|
|
|
[(= how-far 0) xs]
|
|
|
@ -188,17 +174,11 @@
|
|
|
|
(define filler (if cycle
|
|
|
|
(define filler (if cycle
|
|
|
|
(take xs abs-how-far)
|
|
|
|
(take xs abs-how-far)
|
|
|
|
(make-list abs-how-far fill-item)))
|
|
|
|
(make-list abs-how-far fill-item)))
|
|
|
|
(append (drop xs abs-how-far) filler)])]))
|
|
|
|
(append (drop xs abs-how-far) filler)]))
|
|
|
|
|
|
|
|
|
|
|
|
(define/typed+provide shifts
|
|
|
|
(define/typed+provide (shifts xs how-fars [fill-item #f] [cycle #f])
|
|
|
|
(All (A) (case-> ((Listof (Option A)) (Listof Integer) -> (Listof (Listof (Option A))))
|
|
|
|
(All (A) (case-> ((Listof (Option A)) (Listof Integer) -> (Listof (Listof (Option A))))
|
|
|
|
((Listof (Option A)) (Listof Integer) (Option A) -> (Listof (Listof (Option A))))
|
|
|
|
((Listof (Option A)) (Listof Integer) (Option A) -> (Listof (Listof (Option A))))
|
|
|
|
((Listof (Option A)) (Listof Integer) (Option A) Boolean -> (Listof (Listof (Option A))))))
|
|
|
|
((Listof (Option A)) (Listof Integer) (Option A) Boolean -> (Listof (Listof (Option A))))))
|
|
|
|
(case-lambda
|
|
|
|
(map (λ:([how-far : Integer]) (shift xs how-far fill-item cycle)) how-fars))
|
|
|
|
[(xs how-fars)
|
|
|
|
|
|
|
|
(shifts xs how-fars #f #f)]
|
|
|
|
|
|
|
|
[(xs how-fars fill-item)
|
|
|
|
|
|
|
|
(shifts xs how-fars fill-item #f)]
|
|
|
|
|
|
|
|
[(xs how-fars fill-item cycle)
|
|
|
|
|
|
|
|
(map (λ:([how-far : Integer]) (shift xs how-far fill-item cycle)) how-fars)]))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|