Hello John,

I would want to add something to what Justin said.

(defn goo [ i k & l] (println l))

=> (goo 2 3 [233])
;;; gives ([233])
(conj '([233]) ...)

(defn goo [i k & [l]](println l))

=>(goo 2 3 [233])
;;; gives [233]
(conj [233] ...)

Hope the difference is clear now.

Emeka


On Mon, Aug 2, 2010 at 10:43 PM, John Sanda <[email protected]> wrote:

> Thanks for the response and suggestions Justin. A co-worker also just
> suggested multiple arg lists which is perfect. He also suggested (or foo
> bar) to further simplify my code. It definitely cleans the code up and
> improves readability.
>
> - John
>
>
> On Mon, Aug 2, 2010 at 5:37 PM, Justin Kramer <[email protected]> wrote:
>
>> I think you want:
>>
>> (defn- do-traversal [tree idx & [tree-traversal]]
>>  ...)
>>
>> Note the extra brackets for destructuring.
>>
>> Another alternative is using multiple arg lists:
>>
>> (defn- do-traversal
>>  ([tree idx]
>>    (do-traversal tree idx []))
>>  ([tree idx traversal]
>>    ...))
>>
>> Lastly, FYI, the form (if foo foo bar) can be simplified to (or foo
>> bar).
>>
>> Hope that helps,
>>
>> Justin
>>
>> On Aug 2, 5:04 pm, John Sanda <[email protected]> wrote:
>> > I've just implemented an inorder traversal function for a vector-based
>> tree.
>> > The functions look like,
>> >
>> > (defn- do-traversal [tree idx traversal]
>> >   (cond
>> >    (not (node-exists? tree idx)) traversal
>> >    (leaf? tree idx) (conj traversal (tree idx))
>> >    :else (apply conj
>> >         (do-traversal tree (left-child idx) traversal)
>> >         (tree idx)
>> >         (do-traversal tree (right-child idx) traversal))))
>> >
>> > (inorder-traversal [tree]
>> >   (do-traversal tree root-idx []))
>> >
>> > This works as expected but now I am looking to refactor the code some. I
>> > wanted to see if I could do away passing an empty vector to the
>> do-traversal
>> > function. So I updated do-traversal to look like,
>> >
>> > (defn- do-traversal [tree idx & tree-traversal]
>> >   (let [traversal (if tree-traversal tree-traversal [])]
>> >    (cond
>> >     (not (node-exists? tree idx)) traversal
>> >     (leaf? tree idx) (conj traversal (tree idx))
>> >     :else (apply conj
>> >          (do-traversal tree (left-child idx) traversal)
>> >          (tree idx)
>> >          (do-traversal tree (right-child idx) traversal)))))
>> >
>> > When the expected traversal for a tree is [10 20 30] I instead get ([]
>> 30 20
>> > 10 [])) in my unit test. Can someone explain to me why using let as I
>> have
>> > done does not work, and what another solution might be?
>> >
>> > Thanks
>> >
>> > - John
>>
>> --
>> 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]<clojure%[email protected]>
>> For more options, visit this group at
>> http://groups.google.com/group/clojure?hl=en
>
>
>
>
> --
>
> - John
>
> --
> 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]<clojure%[email protected]>
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
>

-- 
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

Reply via email to