2010/1/4 Tom Hicks <[email protected]>:
> 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.
--
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