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 {
   MapString, Container children;
   String letter;
   ListString 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 {
   MapContainer childContainer = new HashMap();
   container.children.put(letter, childContainer);
   container = container.children.get(letter);
 }
 
if (i == letters.length() - 1) {
  container.values.add(value);
  break;
}
 
 } 

(You don't provide example inputs and outputs for the method, so I may
have misunderstood the code. If that's the case please disregard the
next paragraph.)

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!

It is rarely a good idea to directly translate code from one language
into a much different one. It may be done, but the end result will not
be pretty and it certainly won't be idiomatic. I'm sure you'll get much
more helpful responses if you ask a question of the form I need to
solve this problem – how can I do it in Clojure?

If what you want is to manipulate trees in a purely functional way, you
might want to check out the Zipper data structure. [1] You can find it
in Clojure's standard library in the namespace `clojure.zip`. [2]

[1]
http://www.st.cs.uni-saarland.de/edu/seminare/2005/advanced-fp/docs/huet-zipper.pdf
[2] http://richhickey.github.io/clojure/clojure.zip-api.html

Hope this helps,
-- 
Timo

-- 
-- 
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
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
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 clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


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!

Aaand of course I misread the code... I got `letters` and `value` mixed
up. Sorry about the noise!

-- 
Timo

-- 
-- 
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
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
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 clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Help to morph this imperative snippet into a functional one

2013-08-18 Thread Hussein B.
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 {
  MapString, Container children;
  String letter;
  ListString 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 {
  MapContainer 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 clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
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 clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


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. hubaghd...@gmail.com 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 {
   MapString, Container children;
   String letter;
   ListString 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 {
   MapContainer 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 clojure@googlegroups.com
 Note that posts from new members are moderated - please be patient with
 your first post.
 To unsubscribe from this group, send email to
 clojure+unsubscr...@googlegroups.com
 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 clojure+unsubscr...@googlegroups.com.
 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 clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
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 clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


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.


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. hubag...@gmail.com javascript: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 {
   MapString, Container children;
   String letter;
   ListString 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 {
   MapContainer 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 clo...@googlegroups.comjavascript:
 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 javascript:
 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 clojure+u...@googlegroups.com javascript:.
 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 clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
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 clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


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 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. hubaghd...@gmail.com 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. hubag...@gmail.com 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 {
   MapString, Container children;
   String letter;
   ListString 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 {
   MapContainer 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 clo...@googlegroups.com

 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=enhttp://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_outhttps://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 clojure@googlegroups.com
 Note that posts from new members are moderated - please be patient with
 your first post.
 To unsubscribe from this group, send email to
 clojure+unsubscr...@googlegroups.com
 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 clojure+unsubscr...@googlegroups.com.
 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 clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
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 clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


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) (insert bar 8))


On 18 August 2013 18:03, Chris Ford christophertf...@gmail.com 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. hubaghd...@gmail.com 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. hubag...@gmail.com 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 {
   MapString, Container children;
   String letter;
   ListString 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 {
   MapContainer 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 clo...@googlegroups.com

 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=enhttp://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_outhttps://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 clojure@googlegroups.com
 Note that posts from new members are moderated - please be patient with
 your first post.
 To unsubscribe from this group, send email to
 clojure+unsubscr...@googlegroups.com
 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 clojure+unsubscr...@googlegroups.com.
 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 clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
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 clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.