We could define a fn called take-until (defn take-until [pred coll] (take-while (complement pred) coll))
And get the last entry of that user=>(last (take-until odd? [2 4 6 8 9 10 11])) 8 It's based on take-while, so it's lazy. Sean On Dec 24, 1:34 pm, Chouser <[email protected]> wrote: > On Thu, Dec 24, 2009 at 1:24 PM, samppi <[email protected]> wrote: > > I'm having trouble with figuring out something. First, to get the > > first element of a sequence so that (pred element) is false, you do > > (first (drop-while pred sequence)). This is lazy, and stops > > immediately when the element is found. > > > Now, I want to get the element *right before* the element returned by > > (drop-while pred sequence)). I could use reduce, but that is non-lazy > > and always goes through the entire sequence. The sequences I'm working > > with are very big. So, is there a lazy way to get the element right > > before the first element that doesn't fulfill the predicate? Thanks in > > advance. > > (some > (fn [[a b]] (when (odd? b) a)) > (partition 2 1 [2 4 6 8 9 10 11]))) > ;=> 8 > > --Chouser > -- > -- I funded Clojure 2010, did you? -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to [email protected] Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/clojure?hl=en
