3 ms·
TXR Lisp with unpublished reject function (complementing select): (1> (let ((r (range 1 10))) (reject r (take 3 (drop 1 [where oddp r])))) (1 2 4 6
by kazinator 6y ago
TXR Lisp with unpublished reject function (complementing select):
(1> (let ((r (range 1 10)))
(reject r (take 3 (drop 1 [where oddp r]))))
(1 2 4 6 8 9 10)
This is not correct because "drop 1" means "drop the first index where r was found odd" not "drop the first index if it happens to be zero".
Fix:
2> (let ((r (range 1 10)))
(reject r (take 3 [drop-while zerop [where oddp r]])))
(1 2 4 6 8 9 10)
"Let R be a sequence of integers, indexed from 0. Determine the indices where R is odd, as a sequence of indices in ascending order. Remove the zero index, if it is present. Then take the first three of the remaining indices. Remove the elements with those indices from R."
This seems verbose, but watch it do something Common Lisp's remove-if will choke on:
2> (take 50 (let ((r (range 1 1000000000000)))
(reject r (take 3 [drop-while zerop [where oddp r]]))))
(1 2 4 6 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
48 49 50 51 52 53)
range generates a lazy list. where generates the indices lazily. drop-while and take are also lazy, and I just coded reject so that, after the indices have been exhausted, if the remainder of the list is lazy, then it just tacks that lazy suffix into the output. I.e. it's not fully lazy, but in this case it works because the list of indices was trimmed to a finite length.
I think in release 242, I will might include reject, as a fully lazy version supporting a lazy, infinite list of indices as well as sequence; and will fix select to also be fully lazy.