I have the following function that uses take-while
(defn process [[cash amount wrappers]]
(let [bought (int (Math/floor (/ cash amount)))
free (->>
(iterate (partial unwrapper wrappers) bought)
(take-while (partial (fn [w a]
(prn (str "a = " a))
(>= a w)
) wrappers)))]
The problem I have is that I want to include the last item when the predicate is false but take-while
does not return that last item.
Is there a way round that with take-while
or should I use something else?
You could do something like this, based on the source of take-while
:
(defn take-while+
[pred coll]
(lazy-seq
(when-let [[f & r] (seq coll)]
(if (pred f)
(cons f (take-while+ pred r))
[f]))))
The common approach is to use split-with
, which returns the results of both take-while
and drop-while
in a vector. You can then append the first element of the drop-while
result.
It requires two passes, though. You might want to write a custom take-while...
(defn take-while-and-one
[pred coll]
(lazy-seq
(when-let [s (seq coll)]
(if (pred (first s))
(cons (first s) (take-while-and-one pred (rest s)))
(list (first s))))))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With