Files
rose-ash/lib/minikanren/tests/take-while-drop-while.sx
giles 2a36e692f4
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Has been cancelled
mk: take-while-o + drop-while-o — predicate-driven prefix/suffix
(take-while-o pred l result): take elements from l while pred holds,
stopping at the first element that fails. (drop-while-o pred l result):
drop matching elements, return the rest including the first non-match.

Together: (take-while p l) ⊕ (drop-while p l) = l, verified by an
end-to-end roundtrip test.

8 new tests, 546/546 cumulative.
2026-05-08 12:30:53 +00:00

81 lines
1.3 KiB
Plaintext

;; lib/minikanren/tests/take-while-drop-while.sx — prefix/suffix by predicate.
(mk-test
"take-while-o-empty"
(run* q (take-while-o (fn (x) (== x 1)) (list) q))
(list (list)))
(mk-test
"take-while-o-while-lt-5"
(run*
q
(take-while-o
(fn (x) (lto-i x 5))
(list 1 3 7 2 9)
q))
(list (list 1 3)))
(mk-test
"take-while-o-all-match"
(run*
q
(take-while-o
(fn (x) (lto-i x 100))
(list 1 2 3)
q))
(list (list 1 2 3)))
(mk-test
"take-while-o-none-match"
(run*
q
(take-while-o
(fn (x) (lto-i 100 x))
(list 1 2 3)
q))
(list (list)))
(mk-test
"drop-while-o-empty"
(run* q (drop-while-o (fn (x) (== x 1)) (list) q))
(list (list)))
(mk-test
"drop-while-o-while-lt-5"
(run*
q
(drop-while-o
(fn (x) (lto-i x 5))
(list 1 3 7 2 9)
q))
(list (list 7 2 9)))
(mk-test
"drop-while-o-all-match"
(run*
q
(drop-while-o
(fn (x) (lto-i x 100))
(list 1 2 3)
q))
(list (list)))
(mk-test
"take-drop-roundtrip"
(run*
q
(fresh
(p s)
(take-while-o
(fn (x) (lto-i x 5))
(list 1 3 7 2 9)
p)
(drop-while-o
(fn (x) (lto-i x 5))
(list 1 3 7 2 9)
s)
(appendo p s q)))
(list (list 1 3 7 2 9)))
(mk-tests-run!)