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) (insert "bar" 8))
On 18 August 2013 18:03, Chris Ford <[email protected]> wrote:
> 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 "bar" 7))
>
> If you do need values at non-leaf notes, you just need a slightly more
> sophisticated representation.
>
>
> On 18 August 2013 17:38, Hussein B. <[email protected]> wrote:
>
>> 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.
>>
>>
>> On Sunday, August 18, 2013 4:27:43 PM UTC+2, Chris Ford wrote:
>>
>>> 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. <[email protected]> wrote:
>>>
>>>> Hi!
>>>>
>>>> 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<String, Container> children;
>>>> String letter;
>>>> List<String> value;
>>>> }
>>>>
>>>> void insert(Container container, String letters, String value) {
>>>>
>>>> for (int i = 0; i < letters.length; i++) {
>>>>
>>>> String letter = new String(letters.chatAt[i]);
>>>>
>>>> if (container.children.get(**letter) != null) {
>>>> container = container.children.get(letter)**;
>>>> } else {
>>>> Map<Container> childContainer = new HashMap<>();
>>>> container.children.put(letter, childContainer);
>>>> container = container.children.get(letter)**;
>>>> }
>>>>
>>>> if (i == letters.length() - 1) {
>>>> container.values.add(value);
>>>> break;
>>>> }
>>>>
>>>> }
>>>>
>>>>
>>>> Thanks for help and time.
>>>>
>>>> --
>>>> --
>>>> 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
>>>> clojure+u...@**googlegroups.com
>>>>
>>>> For more options, visit this group at
>>>> http://groups.google.com/**group/clojure?hl=en<http://groups.google.com/group/clojure?hl=en>
>>>> ---
>>>> You received this message because you are subscribed to the Google
>>>> Groups "Clojure" group.
>>>> To unsubscribe from this group and stop receiving emails from it, send
>>>> an email to clojure+u...@**googlegroups.com.
>>>>
>>>> For more options, visit
>>>> https://groups.google.com/**groups/opt_out<https://groups.google.com/groups/opt_out>
>>>> .
>>>>
>>>
>>> --
>> --
>> 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
>> ---
>> You received this message because you are subscribed to the Google Groups
>> "Clojure" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to [email protected].
>> For more options, visit https://groups.google.com/groups/opt_out.
>>
>
>
--
--
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
---
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.