On Jan 3, 9:22 pm, Timothy Pratley <timothyprat...@gmail.com> wrote: > 2010/1/4 Tom Hicks <hickstoh...@gmail.com>: > > > All the other code is there to parallel the functionality in 'subvec'. > > Ah right, I see what you mean. > Calling count in the two argument form will realize the entire > sequence unnecessarily. Take and drop are already lazy so no need to > call lazy-seq... so if you wanted to explicitly return nil for invalid > indices you could still simplify to: > (defn indexed-subseq > ([coll start] > (when (not (neg? start)) > (drop start coll))) > ([coll start end] > (when (and (not (neg? start)) (<= start end)) > (take (- end start) (drop start coll))))) > ; (indexed-subseq [0 1 2 3 4] 1 3) > ; = (1 2) > ; (indexed-subseq [0 1 2 3 4] -5 3) > ; = nil > ; (indexed-subseq [0 1 2 3 4] 4 3) > ; = nil > ; (indexed-subseq [0 1 2 3 4] 2) > ; = (2 3 4) > > Regards, > Tim.
Very cool, thank-you....this is exactly the kind of feedback that's valuable as I learn to "think in Clojure". For a second I wondered why your version didn't need the seq call to insure that the argument was a sequence but then realized that, since we were calling only drop and take, they will handle it. I've updated the gist to reflect the latest version. Thanks again for your help. regards, -tom
-- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en