Re: Processing an array

2016-04-26 Thread Olivier Scalbert
Thanks to all of you for your interesting answers !
Olivier

-- 
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/d/optout.


Processing an array

2016-04-24 Thread Olivier Scalbert
Hi all,

I am a Clojure beginner 
And I have an advice to ask !

I have an array of events:

(def events [
  {:id "1" :duration 100}
  {:id "2" :duration 200}
  {:id "3" :duration 300}
])

I wanted to transform this array into:
[
  {:id 1, :duration 100, :time 0} ; event 1 starts at 0
  {:id 2, :duration 200, :time 100} ; event 2 starts when event 1 is 
finished, so at 100
  {:id 3, :duration 300, :time 300} ; event 3 starts when event 2 is 
finished, so at 100 + 200
]

Here is the function code:

(defn to-timed-events
([time events] (to-timed-events time [] events))
([time timed-events events]
(if (empty? events)
timed-events
(let [current (first events)
  next-time (+ (:duration current) time)]
(recur next-time (conj timed-events (assoc current :time time)) 
(rest events))


It seems to work, but it looks a bit long for what it is doing. Is it 
possible to improve it ?

Thanks for your help,

Olivier

-- 
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/d/optout.


Re: Puzzle solving in Clojure

2016-04-14 Thread Olivier Scalbert
Whooww that is impressive !
Respect !!!

Olivier


-- 
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/d/optout.


Re: Puzzle solving in Clojure

2016-04-13 Thread Olivier Scalbert
Thanks to all of you !
There are so much material in your answers that I need to wait this 
week-end to do some tests !

Clojure is cool !

-- 
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/d/optout.


Re: Puzzle solving in Clojure

2016-04-11 Thread Olivier Scalbert
Thanks for your help.

I have replaced "my" permutations by "your" permutations code.
It works faster (16s against 20 or 24s). And it seems to consume much less 
memory.

Now, I will try to understand how it works, which is another story (for me 
of course!).



-- 
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/d/optout.


Puzzle solving in Clojure

2016-04-08 Thread Olivier Scalbert
Hello everybody !

I just start learning Clojure and I am a complete newbie !

I have tried to solve this classical small puzzle with Clojure.
Assign one digit from {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} to each letter {o n e 
t h r l v w y} so that
one + one + one + three + three + eleven = twenty

Here is the code:

(defn permutations [s]
  (lazy-seq
   (if (seq (rest s))
 (apply concat (for [x s]
 (map #(cons x %) (permutations (remove #{x} s)
 [s])))

(defn tomap [proposal]
(zipmap [:o :n :e :t :h :r :l :v :w :y] proposal))

(defn one-value [m]
(+ (* 100 (:o m))
   (*  10 (:n m))
  (:e m)))

(defn three-value [m]
(+ (* 1 (:t m))
   (*  1000 (:h m))
   (*   100 (:r m))
   (*10 (:e m))
(:e m)))

(defn eleven-value [m]
(+ (* 10 (:e m))
   (*  1 (:l m))
   (*   1000 (:e m))
   (*100 (:v m))
   (* 10 (:e m))
 (:n m)))

(defn twenty-value [m]
(+ (* 10 (:t m))
   (*  1 (:w m))
   (*   1000 (:e m))
   (*100 (:n m))
   (* 10 (:t m))
 (:y m)))

(defn check? [proposal]
(let [m (tomap proposal)
  one (one-value m)
  three (three-value m)
  eleven (eleven-value m)
  twenty (twenty-value m)]

(= (+ one one one three three eleven)
   twenty)))

(defn print-solution [solution]
(println (tomap solution)))

(doall (map print-solution (filter check? (permutations (range 10)

This program prints the correct solution, but I have some questions !
It seems to consume lot of memory (around 6GB). Do I miss something with 
the lazy evaluation ?
How can I change the code into a more idiomatic one ?

Thanks for your help and time !

Olivier


-- 
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/d/optout.