Re: Help to morph this imperative snippet into a functional one

2013-08-19 Thread Timo Mihaljov
On 19.08.2013 20:27, Timo Mihaljov wrote: > This example may be to artificial to be translated into Clojure. What > use is it to store strings in a tree keyed by the string's characters? > If you know the path to the string, you already know the string itself, > and you don't need the tree at all!

Re: Help to morph this imperative snippet into a functional one

2013-08-19 Thread Timo Mihaljov
On 18.08.2013 16:51, Hussein B. wrote: > Would you please help me transforming this imperative code into > functional one? > > The code is a typical snippet in imperative style. A lot of mutations > that I don't even know how to start morphing it to Clojure. > > class Container { > Map children

Re: Help to morph this imperative snippet into a functional one

2013-08-18 Thread Chris Ford
Here's a version that allows values to be anywhere in the tree: (defn insert [[container values] [letter & letters] value] (if letter [(update-in container [letter] #(insert % letters value)) values] [container (conj values value)])) e.g. (-> [{} []] (insert "foo" 4) (insert "f" 5) (ins

Re: Help to morph this imperative snippet into a functional one

2013-08-18 Thread Chris Ford
This is close, though it only permits values at leaves and stores the values in the reverse order. Depending on what you need, this might be sufficient: (defn insert [container letters value] (update-in container letters #(conj % value))) e.g. (-> {} (insert "foo" 3) (insert "foo" 4) (insert "b

Re: Help to morph this imperative snippet into a functional one

2013-08-18 Thread Hussein B.
It might be Huffman coding but I don't know Hoffman coding in depth, so I can't be precise. :) But any way, it is a snippet written in an imperative style that I'm trying to transfer into a functional one. The amount of mutation and the if statements are blocking me from doing it in Clojure.

Re: Help to morph this imperative snippet into a functional one

2013-08-18 Thread Chris Ford
Can you explain what the code is supposed to do in English? Java is a little hard to read. :-) Are you doing Huffman coding or similar? On 18 August 2013 16:51, Hussein B. wrote: > Hi! > > Would you please help me transforming this imperative code into functional > one? > > The code is a typic