On Oct 16, 5:36 pm, "Tom Emerson" <[EMAIL PROTECTED]> wrote:
> Hello all,
>
> I wrote a function that behaves as:
>
> user> (split-line "785:3:39334:1:43103")
> ("785" ("3" "39334") ("1" "43103"))
>
> I'd like to solicit comments on better ways I could have written it:
>
> (defn- split-line
> [line]
> (let [parts (seq (.split line ":"))]
> (loop [mills (drop 1 parts)
> result (list (first parts))]
> (if (nil? mills)
> (reverse result)
> (recur (drop 2 mills) (conj result (take 2 mills)))))))
>
> Thanks in advance for your time,
>
You didn't really describe the function's behavior - a doc string
would help us. It would also possibly help you find the higher-level
functions to do the job.
Here's one way:
(defn split-line [line]
(let [parts (.split line ":")]
(cons (first parts) (partition 2 (rest parts)))))
One guideline - in Clojure we tend to produce our results in order.
Having to call reverse is a warning sign. Another is - if your loop/
recur only walks through one sequence there's likely a higher-level
function (or combination of them) that will let you avoid the loop.
Rich
--~--~---------~--~----~------------~-------~--~----~
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
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
-~----------~----~----~----~------~----~------~--~---