clojure on XEmacs inferior-lisp

2009-01-03 Thread Tzach

Hello
I'm a clojure  newbie, trying to set the working environment on
XEmacs.
I download all the relevant from http://clojure.codestuffs.com//
and added the following to my init.el
(setq load-path (append load-path (list (expand-file-name "C:/lisp/
clojure/emacs/"
(require 'clojure-auto)

(setq swank-clojure-jar-path "C:/clojure_20081217/clojure/
clojure.jar")
(require 'swank-clojure-autoload)
(require 'swank-clojure)

When running "slime", I get the following:
(add-classpath "file:///c:\\lisp\\clojure\\emacs\\")

(require 'swank.swank)

(swank.swank/ignore-protocol-version "2008-07-05")

(swank.swank/start-server "C:\\DOCUME~1\\tzach\\LOCALS~1\\Temp\\slime.
8120" :encoding "binary")

Clojure
user=> nil
user=> nil
user=> "2008-07-05"
user=> Connection opened on local port  4452
4452
user=> Exception in thread "Server 0 [7]" java.lang.RuntimeException:
java.io.UnsupportedEncodingException: binary

at clojure.lang.AFn.run(AFn.java:42)

at java.lang.Thread.run(Thread.java:595)

Caused by: java.io.UnsupportedEncodingException: binary

at sun.io.Converters.getConverterClass(Converters.java:218)

at sun.io.Converters.newConverter(Converters.java:251)

at sun.io.CharToByteConverter.getConverter(CharToByteConverter.java:
68)

at sun.nio.cs.StreamEncoder$ConverterSE.(StreamEncoder.java:
189)

at sun.nio.cs.StreamEncoder$ConverterSE.(StreamEncoder.java:
172)

at sun.nio.cs.StreamEncoder.forOutputStreamWriter(StreamEncoder.java:
72)

at java.io.OutputStreamWriter.(OutputStreamWriter.java:82)

at swank.core.connection$make_connection__188.invoke(connection.clj:
35)

at swank.core.server$accept_authenticated_connection__505.invoke
(server.clj:31)

at swank.core.server$socket_serve__515.invoke(server.clj:48)

at swank.core.server$setup_server__525$fn__527.invoke(server.clj:64)

at swank.util.net.sockets$socket_server__465$fn__492$fn__494.invoke
(sockets.clj:15)

at clojure.lang.AFn.applyToHelper(AFn.java:172)

at clojure.lang.AFn.applyTo(AFn.java:165)

at clojure.core$apply__2869.doInvoke(core.clj:374)

at clojure.lang.RestFn.invoke(RestFn.java:428)

at swank.util.net.sockets$socket_server__465$fn__492.doInvoke
(sockets.clj:9)

at clojure.lang.RestFn.invoke(RestFn.java:402)

at clojure.lang.AFn.run(AFn.java:38)

... 1 more


Any idea what the problem is?
Thanks




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



Ugly Sudoku solver

2009-01-09 Thread Tzach

Hi Clojure fans
My first attempt with Clojure is a Sudoku solver, and I'm fighting
with what are probably trivial problems (for a non newbie)

The main function sudoku is recursive:
1. Getting a sudoku board as an input
2. Choosing the next empty (zero) cell to test, loop on all valid
values, and call sudoku with the new board
3. When a solution (board with no zero values) is found: throw.

(defn sudoku [board]
  "solve a sudoku problem"
  (when (complete? board)
(do
 (println "complete")
 (print-board board)
  (throw nil)))
  (let [cell (next-cell board)
   pos (first cell)
   valid-values (second cell)]
   (when cell
 (doseq [v valid-values]
(sudoku (assoc-in board pos v)))
 )))

Although it does work, we can all agree its pretty ugly, so I would
appreciate your help on the following questions:
1. How to can I return a solution via the recursive stack with out
throwing an exception? I understand there is no "return-from"
facility.
2. Can this function be implemented as tail recursive (using loop?
recur?)

Naturally, any other inputs are welcome.

Thanks
Tzach

---
Full source:

(defn print-board [board]
  "Pretty print the sudoku board"
  (doseq [row board] (println row)))

(defn mod3range [x]
  (let [start (- x (rem x 3))]
   (range start (+ 3 start

(defn neighbors-pos [pos]
  "return a collection of neighbors positions to pos"
  (remove #(= pos %)
  (distinct (concat
 (for [y (range 3) z (range 3)] [(get pos 0) y z]) ; row
neighbors
 (for [x (range 9)] [x (get pos 1) (get pos 2)]) ; col 
neighbors
 (for [x (mod3range (get pos 0)) z (range 3)] [x (get pos 1)
z]))) ; square neighbors;
  ))

(defn neighbors-values [board pos]
  "return a list of neighbor positions values"
  (map #(get-in board %) (neighbors-pos pos)))

(defn valid-values [board pos]
  "return a list of values which does not violate the neighbors
values. return nil if the position already have a value"
  (if (zero? (get-in board pos))
  (clojure.set/difference (set (range 1 10)) (neighbors-values board
pos))
  (seq nil)))

(defn map-board [board f]
  "execute function f on each of the position on board.  function
f get the position and the board as parameters"
   (for [x (range 9) y (range 3) z (range 3)]
(let [pos [x y z]]
 [pos (f board pos) ]
 )))

(defn next-cell [board]
  "return the next potential cell to set, and the valid alternatives"
  (first (filter
  #(not (empty? (second %)))
  (sort-by #(count (second %)) (map-board board valid-values)

(defn complete? [board]
  (not (some #(second %)
 (map-board board (fn [board pos] (zero? (get-in board pos)))


(defn sudoku [board]
  "solve a sudoku problem"
  (when (complete? board)
(do
 (println "complete")
 (print-board board)
  (throw nil)))
  (let [cell (next-cell board)
   pos (first cell)
   valid-values (second cell)]
   (when cell
 (doseq [v valid-values]
(sudoku (assoc-in board pos v)))
 )))

;;; use the solver
(def *sudoku-problem*
 [[[0 0 0] [5 0 0] [0 9 1]]
  [[1 0 0] [8 6 0] [0 3 2]]
  [[0 0 6] [9 3 0] [0 0 0]]
  [[0 0 4] [6 0 0] [0 7 3]]
  [[0 5 0] [4 9 3] [0 1 0]]
  [[3 6 0] [0 0 8] [9 0 0]]
  [[0 0 0] [0 8 5] [3 0 0]]
  [[8 3 0] [0 1 6] [0 0 5]]
  [[6 7 0] [0 0 9] [0 0 0]]])

(sudoku *sudoku-problem*)


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



Re: Ugly Sudoku solver

2009-01-10 Thread Tzach

Thanks Konrad
A very elegant solution.
40 years of laziness, and I finally realize what a great feature the
lazy evaluation is ;)

Tzach


On Jan 9, 3:30 pm, Konrad Hinsen  wrote:
> On Jan 9, 2009, at 13:18, Tzach wrote:
>
>
>
> > The main functionsudokuis recursive:
> > 1. Getting asudokuboard as an input
> > 2. Choosing the next empty (zero) cell to test, loop on all valid
> > values, and callsudokuwith the new board
> > 3. When a solution (board with no zero values) is found: throw.
>
> > (defnsudoku[board]
> >   "solve asudokuproblem"
> >   (when (complete? board)
> >     (do
> >      (println "complete")
> >      (print-board board)
> >       (throw nil)))
> >   (let [cell (next-cell board)
> >        pos (first cell)
> >        valid-values (second cell)]
> >        (when cell
> >     (doseq [v valid-values]
> >            (sudoku(assoc-in board pos v)))
> >     )))
>
> > Although it does work, we can all agree its pretty ugly, so I would
> > appreciate your help on the following questions:
> > 1. How to can I return a solution via the recursive stack with out
> > throwing an exception? I understand there is no "return-from"
> > facility.
>
> The return value of a function is the last expression that was  
> evaluated. Yoursudokufunction could have a structure like this:
>
> (defnsudoku[board]
>    "solve asudokuproblem"
>    (if (complete? board)
>      board
>      (let [...]
>        ..))
>
> The problem is then in the let branch, as it can terminate without  
> returning a valid board.
>
> > 2. Can this function be implemented as tail recursive (using loop?
> > recur?)
>
> As it is, no, because you have multiple recursive calls. However, I  
> wonder what those are good for. If I understand your algorithm  
> correctly, it find all valid values for the next cell to be filled,  
> and then tries for each of them to complete the puzzle, using a  
> recursive call.
>
> I would rewrite the solver as a function that returns a lazy sequence  
> of valid solutions:
>
> (defn all-solutions [board]
>    (if (complete? board)
>      (list board)
>      (let [[pos valid-values] (next-cell board)]
>        (apply concat (for [v valid-values]
>                       (all-solutions (assoc-in board pos v)))
>
> Note that you don't have to do anything to make the sequences lazy;  
> apply and for take care of that automatically.
> The solver then just takes the first item of that sequence:
>
> (defnsudoku[board]
>    "solve asudokuproblem"
>    (first (all-solutions board)))
>
> Since the sequence is lazy, the remaining solutions (if any) are  
> never computed, so this version does not do more work than your  
> original one.
>
> Konrad.
--~--~-~--~~~---~--~~
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
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
-~--~~~~--~~--~--~---



Re: Ugly Sudoku solver

2009-01-10 Thread Tzach

Following your good advice, I also update the next-cell function to
work in a lazy way instead of sorting the values of the entire board.
The full source bellow.
Next phase: GUI.

;; sudoku solver by Tach
;; with the help of Konrad
(defn print-board [board]
  "Pretty print the sudoku board"
  (doseq [row board] (println row)))

(defn mod3range [x]
  (let [start (- x (rem x 3))]
   (range start (+ 3 start

(defn neighbors-pos [pos]
  "return a collection of neighbors positions to pos"
  (remove #(= pos %)
  (distinct (concat
 (for [y (range 3) z (range 3)] [(get pos 0) y z]) ; row
neighbors
 (for [x (range 9)] [x (get pos 1) (get pos 2)]) ; col 
neighbors
 (for [x (mod3range (get pos 0)) z (range 3)] [x (get pos 1)
z]))) ; square neighbors;
  ))

(defn neighbors-values [board pos]
  "return a list of neighbor positions values"
  (map #(get-in board %) (neighbors-pos pos)))

(defn valid-values [board pos]
  "return a list of values which does not violate the neighbors
values. return nil if the position already have a value"
  (if (zero? (get-in board pos))
  (clojure.set/difference (set (range 1 10)) (neighbors-values board
pos))
  (seq nil)))

(defn map-board [board f]
  "execute function f on each of the position on board.  function
f get the position and the board as parameters"
   (for [x (range 9) y (range 3) z (range 3)]
 (let [pos [x y z]]
  [pos (f board pos) ]
 )))

(defn next-cell [board]
  "return the next potential cell to set, and the valid alternatives"
  (first (for [n (range 1 10)]
(filter
 #(= n (count (second %)))
 (map-board board valid-values)

(defn complete? [board]
  (not (some #(second %)
 (map-board board (fn [board pos] (zero? (get-in board pos)))


(defn all-solutions [board]
  (if (complete? board)
  (list board)
  (let [[pos valid-values] (next-cell board)]
   (apply concat (for [v valid-values]
  (all-solutions (assoc-in board pos v)))

(defn sudoku [board]
   "solve a sudoku problem"
   (first (all-solutions board)))


;;; use the solver
(def *sudoku-problem*
 [[[0 0 0] [5 0 0] [0 9 1]]
  [[1 0 0] [8 6 0] [0 3 2]]
  [[0 0 6] [9 3 0] [0 0 0]]
  [[0 0 4] [6 0 0] [0 7 3]]
  [[0 5 0] [4 9 3] [0 1 0]]
  [[3 6 0] [0 0 8] [9 0 0]]
  [[0 0 0] [0 8 5] [3 0 0]]
  [[8 3 0] [0 1 6] [0 0 5]]
  [[6 7 0] [0 0 9] [0 0 0]]])

(print-board (sudoku *sudoku-problem*))

On Jan 10, 10:22 pm, Tzach  wrote:
> Thanks Konrad
> A very elegant solution.
> 40 years of laziness, and I finally realize what a great feature the
> lazy evaluation is ;)
>
> Tzach
>
> On Jan 9, 3:30 pm, Konrad Hinsen  wrote:
>
> > On Jan 9, 2009, at 13:18, Tzach wrote:
>
> > > The main functionsudokuis recursive:
> > > 1. Getting asudokuboard as an input
> > > 2. Choosing the next empty (zero) cell to test, loop on all valid
> > > values, and callsudokuwith the new board
> > > 3. When a solution (board with no zero values) is found: throw.
>
> > > (defnsudoku[board]
> > >   "solve asudokuproblem"
> > >   (when (complete? board)
> > >     (do
> > >      (println "complete")
> > >      (print-board board)
> > >       (throw nil)))
> > >   (let [cell (next-cell board)
> > >        pos (first cell)
> > >        valid-values (second cell)]
> > >        (when cell
> > >     (doseq [v valid-values]
> > >            (sudoku(assoc-in board pos v)))
> > >     )))
>
> > > Although it does work, we can all agree its pretty ugly, so I would
> > > appreciate your help on the following questions:
> > > 1. How to can I return a solution via the recursive stack with out
> > > throwing an exception? I understand there is no "return-from"
> > > facility.
>
> > The return value of a function is the last expression that was  
> > evaluated. Yoursudokufunction could have a structure like this:
>
> > (defnsudoku[board]
> >    "solve asudokuproblem"
> >    (if (complete? board)
> >      board
> >      (let [...]
> >        ..))
>
> > The problem is then in the let branch, as it can terminate without  
> > returning a valid board.
>
> > > 2. Can this function be implemented as tail recursive (using loop?
> > > recur?)
>
> > As it is, no, because you have multiple recursive calls. However, I  
> > wonder what those are good for. If I understand your algorithm  
> > correctly, it find all valid values for the next cell to be fil

are (swing) elements in a data structures read only?

2009-01-24 Thread Tzach

I'm working a Sudoku GUI interface using swing, and I notice something
strange.
I have a JPanel r, with 9 JTextField added to it.
I created a small function to return the text filed of a panel:

(defn components [container]
  (for [i (range (.getComponentCount container))]
(.getComponent container i)))

When I update one element:
(let [t (first (components r))]
   (.setText t (str 7)))

The text on the panel do update.
When I try to do the same for all the elements:
 (for [t (components r)]
(.setText t (str 6)))

Nothing happened.
What am I missing here?

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



Re: Ugly Sudoku solver

2009-01-26 Thread Tzach

Thanks Konrad
I do not understand the benefit of storing the map-board result.
The map-board function it self is using lazy for loop.
Why isn't the laziness transitive automatically?

On Jan 11, 12:05 pm, Konrad Hinsen  wrote:
> On 11.01.2009, at 08:56,Tzachwrote:
>
> > Following your good advice, I also update the next-cell function to
> > work in a lazy way instead of sorting the values of the entire board.
>
> Good idea!
>
> > (defn next-cell [board]
> >   "return the next potential cell to set, and the valid alternatives"
> >   (first (for [n (range 1 10)]
> >    (filter
> >     #(= n (count (second %)))
> >     (map-board board valid-values)
>
> Your implementation has the disadvantage of recomputing (map-
> board ...) nine times. You can avoid this with a simple modification:
>
> (defn next-cell [board]
>    "return the next potential cell to set, and the valid alternatives"
>    (let [vv (map-board board valid-values)]
>      (first (for [n (range 1 10)]
>           (filter
>            #(= n (count (second %)))
>            vv)
>
> Since the values of the lazy sequence vv are cached, nothing will  
> ever be recomputed.
>
> However, I think there is also a bug in your function: (first  
> (for ..)) will return the result of the first iteration of the for,  
> even if it is emtpy. What you want is the first element of the first  
> non-empty element of the for sequence. You can get this with (first  
> (apply concat (for [n ...))). Concat again creates a lazy sequence,  
> so nothing is computed unless necessary.
>
> Konrad.
--~--~-~--~~~---~--~~
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
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
-~--~~~~--~~--~--~---



Sudoku solver with GUI

2009-02-06 Thread Tzach

Hi all
As my first Clojure project, I decided to finally solve one of
humanity major  problems - Sudoku!
Here is the source
http://code.google.com/p/sudoku-solver/source/browse/trunk/sudoku-solver.clj

I would appreciate your comments.
Thanks Konrad for useful tips.

What is the simplest way to make a standalone executable out of this?

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



Re: Sudoku solver with GUI

2009-02-07 Thread Tzach

Thanks Keith, I update the version with your inputs, should have test
it first.
Laurent, is it consider a bad practice to use hyphen on general, or
just as ns / file names?

Tzach

On Feb 7, 1:36 am, Laurent PETIT  wrote:
> Hello, not related to your question, but  you should consider renaming your
> file sudoku_solver to make it compiler friendly.
>
> Concerning your question, I think there have been some answers recently on
> the ml. Sorry I don't remember the links exactly.
>
> Regards,
>
> --
> Laurent
>
> 2009/2/6 Tzach 
>
>
>
> > Hi all
> > As my first Clojure project, I decided to finally solve one of
> > humanity major  problems - Sudoku!
> > Here is the source
>
> >http://code.google.com/p/sudoku-solver/source/browse/trunk/sudoku-sol...
>
> > I would appreciate your comments.
> > Thanks Konrad for useful tips.
>
> > What is the simplest way to make a standalone executable out of this?
>
> > Thanks
--~--~-~--~~~---~--~~
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
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
-~--~~~~--~~--~--~---



newbie question on compilation

2009-02-10 Thread Tzach

I try to use the following example

(ns clojure.examples.hello
(:gen-class))

(defn -main
  [greetee]
  (println (str "Hello " greetee "!")))

(compile 'clojure.examples.hello)

I got "error in process filter: Wrong number of arguments: nil, 3".
What am I missing here?

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



Re: newbie question on compilation

2009-02-10 Thread Tzach

Thanks for the response
Yes, I'm using Emacs SLIM.

what should be the relation between the file (hello.clj) path to the
classpath?
Is the compile work on the file name, or on the function? in other
word, should I evaluate the function on the REPL before compile?

Thanks
Tzach


On Feb 10, 5:25 pm, Laurent PETIT  wrote:
> Hello,
>
> Can you be more specific about : what is in what file in your example, and
> what is the structure of the directories you use for the test ?
>
> Normally, this should be :
> (say src is the root source directory set in your classpath) :
>
> ---
> file : src/clojure/examples/hello.clj
> ---
> (ns clojure.examples.hello
>    (:gen-class))
>
> (defn -main
>  [greetee]
>  (println (str "Hello " greetee "!")))
> ---
>
> And from the REPL : (compile 'clojure.examples.hello)
>
> I tried it, it works correctly (the compilation works correctly)
>
> Regards,
>
> --
> Laurent
>
> 2009/2/10 Tzach 
>
>
>
> > I try to use the following example
>
> > (ns clojure.examples.hello
> >    (:gen-class))
>
> > (defn -main
> >  [greetee]
> >  (println (str "Hello " greetee "!")))
>
> > (compile 'clojure.examples.hello)
>
> > I got "error in process filter: Wrong number of arguments: nil, 3".
> > What am I missing here?
>
> > Thanks
--~--~-~--~~~---~--~~
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
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
-~--~~~~--~~--~--~---



Local variables in a macro

2009-04-27 Thread Tzach

Hi all
I’m playing on a mechanism for function call results caching.
This can be used to improve performance in case of a complex  or
remote function call.
The basic idea is as follow:

;; The cache is implement as a global map. Function arguments are the
key, the return result is the value.
(def *cache* (hash-map))

;; first check the cache,
;; if result not already exist – call function and store result
(defmacro cache-call [f & args]
  `(let [cached# (get *cache* (list ~...@args))]
(if cached#
  cached#
(let [ret# (~f ~...@args)]
(def *cache* (assoc *cache* (list ~...@args) ret#))
  ret#) )))

usage:
(cache-call + 1 2)

This works fine, but pretty messy.
I would like to scope the hash-map inside the macro and have a hash-
map per function.
How can I do it?
Any style, idioms or personal comments are more than welcome.

Thanks
Tzach

P.S.
At a second phase I would like to let the user extend the caching
properties per function, e.g. cache size, refresh algorithm etc.

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



Sudoku solver written in Clojure using Swing

2009-05-03 Thread Tzach

This the my first (mini) project in Clojure, and it sure was fun!
Thanks to the people here which help me along the way, although some
of my questions was pretty trivial.

The code
http://code.google.com/p/sudoku-solver/source/browse/trunk/sudoku.clj

Download as executable jar:
http://code.google.com/p/sudoku-solver/downloads/list

For most problems, the solver return an answer in a few seconds, but
for some of the hard problems, it may take much longer. I'm still not
sure why.
Any idea is welcome.

Tzach

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



Converting symbols to strings

2009-10-22 Thread Tzach

Hello all
I’m writing a small facility which get vector of vectors of names
(strings) and print them in a special format.
Something like (foo [[“aname” “a-different-name”] [ “oneMoreName”]])
This works fine, but make it hard to write all the quotations when
many parameters are used.

What is the simplest way to define foo as
(foo [[aname a-different-name ] [oneMoreName]]) ?

Is  macro required? (I guess it is)

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



Re: Converting symbols to strings

2009-10-24 Thread Tzach

Thanks for the help!
I created a small function, which convert the nodes of a nested
collection to strings:

(defn rec-to-strs [f]
  "recursively change parameters to strings"
  (vec (for [c f]
(if (coll? c)
(vec (rec-to-strs c))
(str c)

I use this function from a macro as suggested.

On Oct 22, 4:53 pm, Sean Devlin  wrote:
> Or, you could just write foo as
>
> ;;Adjusting for 2d
> (defmacro foo
>   [vec-vec-of-symbols]
>   (let [vec-vec-of-str (vec (map (comp vec (partial map str)) vec-vec-
> of-symbols))]
>   `(foo* ~vec-vec-of-str)))
>
> This let you write foo* to handle strings.  Anyway, the key thing to
> note in both examples is that the heavy lifting is delegated to
> another function.  This is an element of good macro design.
>
> Just my $.02
>
> On Oct 22, 10:29 am, Meikel Brandmeyer  wrote:
>
> > Hi,
>
> > On Oct 22, 4:22 pm, Tzach  wrote:
>
> > > I’m writing a small facility which get vector of vectors of names
> > > (strings) and print them in a special format.
> > > Something like (foo [[“aname” “a-different-name”] [ “oneMoreName”]])
> > > This works fine, but make it hard to write all the quotations when
> > > many parameters are used.
>
> > > What is the simplest way to define foo as
> > > (foo [[aname a-different-name ] [oneMoreName]]) ?
>
> > > Is  macro required? (I guess it is)
>
> > It is, but a simple one. Rename your foo function to foo* and create a
> > simple macro called foo like that:
>
> > (defmacro foo
> >   [vec-of-symbols]
> >   `(foo* (quote ~vec-of-symbols)))
>
> > There you go. (Of course you must now handle Symbols in your foo* star
> > function instead of Strings...)
>
> > Sincerely
> > Meikel
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Multiple dispatch with Multimethods

2009-12-07 Thread Tzach
Hello
What is the idiomatic way to implement the classic collide-with
function in Clojure?
I would like to implement something similar to the following (pseudo
code ahead):

(defmulti collide-with foo)

(defmethod collide-with ["asteroid" "spaceship"] (print "Boom"))
(defmethod collide-with ["asteroid" any] (print " Wiiissh"))
(defmethod collide-with [any "spaceship"] (print "Wooossh"))
(defmethod collide-with [any any] (print "Dead Space"))

The idea is to have a default function for a collision of asteroid
with anything else, and of spaceship with anything else.
What foo can I use?

Thanks

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


Re: Multiple dispatch with Multimethods

2009-12-07 Thread Tzach
Thanks
I didn't know the costume hierarchy at all, it seems to be very
useful.
I understand it is coupled to the Java object hierarchy.
Is there a way to create similar util for strings, as I did on my
first example?

On Dec 7, 12:10 pm, Meikel Brandmeyer  wrote:
> Hi,
>
> On Dec 7, 10:21 am, Tzach  wrote:
>
> > (defmulti collide-with foo)
>
> > (defmethod collide-with ["asteroid" "spaceship"] (print "Boom"))
> > (defmethod collide-with ["asteroid" any] (print " Wiiissh"))
> > (defmethod collide-with [any "spaceship"] (print "Wooossh"))
> > (defmethod collide-with [any any] (print "Dead Space"))
>
> > The idea is to have a default function for a collision of asteroid
> > with anything else, and of spaceship with anything else.
> > What foo can I use?
>
> Eg. a custom hierarchy. Since there is no bottom type, you have to
> create it manually. Deriving Object from ::Any takes care of the Java
> land. For clojure land you have to derive each type from ::Any (or
> anything already deriving from ::Any).
>
> (def space-hierarchy
>   (-> (make-hierarchy)
>     (derive Object ::Any)
>     (derive ::Spaceship ::Any)
>     (derive ::Asteroid ::Any)))
>
> (defmulti collide-with
>   #(vec (map type %&))
>   :hierarchy #'space-hierarchy)
>
> (defmethod collide-with :default
>   [_ _]
>   (print "Dead Space"))
>
> (defmethod collide-with [::Asteroid ::Any]
>   [_ _]
>   (print "Wiiissh"))
>
> (defmethod collide-with [::Any ::Spaceship]
>   [_ _]
>   (print "Wooosssh"))
>
> (defmethod collide-with [::Asteroid ::Spaceship]
>   [_ _]
>   (println "Booom!"))
>
> Maybe you have to disambiguate with prefer-method in certain cases. (I
> don't think, that's the case here, but I haven't tested it...)
>
> Hope this helps.
>
> Sincerely
> Meikel

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


text flow - a trivial generator of RFC like ASCII call flows (sequence diagrams)

2009-12-21 Thread Tzach
Hey all,
During my day job, I often need to create call flows (sequence
diagrams).
For formal documents, I mostly used Visio or EventStudio, but when I
need something quick and dirty in pure ASCII, these tools are too
heavy.
For a while I use my own simple CL solution, and now I ported it to
Clojure

The project home (and an example):
http://code.google.com/p/clojure-textflow/

The code:
http://code.google.com/p/clojure-textflow/source/browse/src/textflow.clj

Although seems trivial (it is), I’m actually using it quite
frequently.
I would appreciate your feedback on the code, improvement suggestions
or any other input.

Regards
Tzach

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


Re: text flow - a trivial generator of RFC like ASCII call flows (sequence diagrams)

2009-12-21 Thread Tzach
Thanks Newman
Useful link!
I'm doing a lot of SIP as well, and I even have a (not so) secret plan
to extend this to create a simple SIP UA simulation.


On Dec 21, 8:57 pm, Richard Newman  wrote:
> > I would appreciate your feedback on the code, improvement suggestions
> > or any other input.
>
> This is fantastic, thank you for sharing! I do SIP during the day, so  
> this will come in handy.
>
> See also
>
> http://www.websequencediagrams.com/
>
> which does a good job for producing images.

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


Re: text flow - a trivial generator of RFC like ASCII call flows (sequence diagrams)

2009-12-22 Thread Tzach
Thanks, look very interesting!
On what SIP server did you try it, only Sailfin?
I'm working mostly with Oracle OCCAS (formerly WebLogic Sip Server).

BTW, I added a minor feature, which make the actor specify optional.

On Dec 22, 2:54 am, Richard Newman  wrote:
> > Thanks Newman
> > Useful link!
> > I'm doing a lot of SIP as well, and I even have a (not so) secret plan
> > to extend this to create a simple SIP UA simulation.
>
> You might be interested in
>
> http://github.com/rnewman/clj-sip

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


Newbie question on XML processing

2010-01-07 Thread Tzach
Hello
I have a simple task of reading an XML structure, manipulate part of
it and writing it back to XML.
For example, adding 1$ for each book with a year element after 2005 in
the following example:



  
Everyday Italian
Giada De Laurentiis
2005
30.00
  
  
Harry Potter
J K. Rowling
2006
29.99
  


clojure.contrib.zip-filter.xml is getting me close to this, but I
still do not see how can I use it (or other library) to modify values.
What would be the idiomatic (and easiest) way to do that?
I apologize in advance if this is too trivial.

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

Re: Newbie question on XML processing

2010-01-08 Thread Tzach
Thanks Sean
This is very helpful.

Tzach

On Jan 8, 5:44 am, Sean Devlin  wrote:
> Tzach,
> I'd start will clojure.xml.  At a very high level, my program would
> look like this
>
> 1.  Load the xml file with clojure.xml/parse
> 2.  Apply your filtering code with something like map-if (see below)
>
> (defn map-if [pred f coll]
>    (map #(if (pred %) (f %) %) coll))
>
> 3.  Use clojure.contrib.prxml to output the data.
>
> Hope this helps,
> Sean
>
> On Jan 7, 11:33 am, Tzach  wrote:
>
> > Hello
> > I have a simple task of reading an XML structure, manipulate part of
> > it and writing it back to XML.
> > For example, adding 1$ for each book with a year element after 2005 in
> > the following example:
>
> > 
> > 
> >   
> >     Everyday Italian
> >     Giada De Laurentiis
> >     2005
> >     30.00
> >   
> >   
> >     Harry Potter
> >     J K. Rowling
> >     2006
> >     29.99
> >   
> > 
>
> > clojure.contrib.zip-filter.xml is getting me close to this, but I
> > still do not see how can I use it (or other library) to modify values.
> > What would be the idiomatic (and easiest) way to do that?
> > I apologize in advance if this is too trivial.
>
> > Thanks
> > Tzach
-- 
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

Minimax in Clojure – request for code review and a question

2010-02-24 Thread Tzach
Hi all
I made a naïve attempt to implement the minimax algorithm in Clojure.
I would appreciate any comment on style, wrong (or right) use of
idioms etc.
Specifically, can I create a “contract” for the function I use, like
heuristic, to formalize minimax requirement from it?

Thanks
Tzach

Pseudocode from http://en.wikipedia.org/wiki/Minimax:

function integer minimax(node, depth)
if node is a terminal node or depth == 0:
return the heuristic value of node
α = -∞
for child in node:   # evaluation is identical
for both players
α = max(α, -minimax(child, depth-1))
return α

My take in Clojure:

(defn minimax [pos depth player]
  "minimax implementation, return a pair of the best value and move.
   Require the following functions:
heuristic - return the heuristic value of a pos
movegen - return a sequence of legal moves
next-pos - return the new position after a move was done"

  (let [moves (movegen pos player)]
(if (or (empty? moves) (zero? depth))
  [(heuristic pos player) nil]
  (apply max-key first
 (cons
  [- nil]
  (for [m moves :let [n-pos (next-pos pos m)]]
[ (- (first (minimax n-pos (dec depth) (opposite player m ]
))

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


Newbie Gloss questions - dynamic buffer structure

2015-01-03 Thread Tzach
I'm trying to work with Gloss binary encoder/decoder, and need some help to 
kick start.

My first task is simple(not for me ;)
I have the following binary buffer to read/write:

   - 4 byte (32 bit) - code (uint)
   - 8 bit - misc flags
   - 3 byte (24 bit) - the entire buffer length
   - 4 byte (32 bit) uint value -  optional, depending on on of the flags.
   - List of 4 byte (uints) data elements - size dependent on the overall 
   size 
   
How should I represent this structure?
Clearly I need to use *prefix* and *repeated* for the last element, but I 
failed to understand how.
I also struggle with the optional element and how to represent a 3 byte 
element.
 
Thanks!

-- 
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: Newbie Gloss questions - dynamic buffer structure

2015-01-12 Thread Tzach
Hi Zach
Thanks for the detailed response

On Wednesday, January 7, 2015 at 7:28:06 AM UTC+2, Zach Tellman wrote:
>
> Hey Tzach,
>
> If I understand what you're trying to do, you want something like this:
>
> [ :uint32
>   :ubyte ;; or bit-seq, depending
>   (delimited-block (prefix uint24 #(- % 5) #(+ % 5))) ]
>
> And then you'll need to parse the final block separately.  I've left 
> defining the uint24 as an exercise for the reader (sorry, long day), but 
> using (compile-frame [:uint16 :uint8] ...) should be pretty straightforward.
>
This is what work for me, in case anyone is interested or want to comment

(defn valid-length [l]
 (and (< l 16777216)
  (>= l 0)))

(def uint24 (compile-frame [:ubyte :uint16]
   (fn [u] 
 {:pre  [(valid-length u)]}
 (let [u8 (bit-shift-right u 16)
   u16 (- u (bit-shift-left u8 16))]
   [u8 u16]))
   (fn [[u8 u16]] 
 {:post [(valid-length %)]}
 (+ (bit-shift-left u8 16) u16

(def avp
  (compile-frame
   [:uint32
:ubyte
(repeated :int32 :prefix (prefix uint24 #(- % 5) #(+ % 5))) ]
))

My next step is to parse the bits from the header :ubyte and base on it, 
read an extra uint32 before the repeated part.
The header is probably the answer.
 

>
> Hope that helps, happy to answer any followup questions.
>

It did help! Thanks

BTW, I spent too much time trying to use (compile-frame [:uint16 :uint8]) 
before realizing :uint8 is not defined.
The result is simply encoding of the :uint8 symbol.
I wonder if its a bug or a feature.

 

>
> Zach
>
> On Saturday, January 3, 2015 11:51:14 PM UTC-8, Tzach wrote:
>>
>> I'm trying to work with Gloss binary encoder/decoder, and need some help 
>> to kick start.
>>
>> My first task is simple(not for me ;)
>> I have the following binary buffer to read/write:
>>
>>- 4 byte (32 bit) - code (uint)
>>- 8 bit - misc flags
>>- 3 byte (24 bit) - the entire buffer length
>>- 4 byte (32 bit) uint value -  optional, depending on on of the 
>>flags.
>>- List of 4 byte (uints) data elements - size dependent on the 
>>overall size 
>>
>> How should I represent this structure?
>> Clearly I need to use *prefix* and *repeated* for the last element, but 
>> I failed to understand how.
>> I also struggle with the optional element and how to represent a 3 byte 
>> element.
>>  
>> Thanks!
>>
>

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


Tron clone in Clojure

2011-04-04 Thread Tzach
I present my take on the classic Tron game in Clojure, inspired by
Paul Richards Clojuroids and Mark Volkmann Snake games (thanks guys)

Code and Jar are available here:
https://sites.google.com/site/tzachlivyatan/tron-clone-in-clojure

Inputs and comments will be appreciated!

specifically, I'm unhappy with the game loop and key press collection.
I'm currently keeping the entire game state in an atom, and update it
from the key listener and game loop.
I suspect there is a better way.
If anyone have a more generic way to deal with it, please let me know
(maybe a Clojure 2d game wrapper lib?)

Regards
Tzach

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


Re: Tron clone in Clojure

2011-04-06 Thread Tzach
Thanks Ryan
I update the game to use similar loop as you have, and add some minor
graphics.
Result Jar and code are here
https://sites.google.com/site/tzachlivyatan/tron-clone-in-clojure

Next step: move to 3d with Clj3D!

Tzach


On Apr 5, 9:46 am, Ryan Sattler  wrote:
> Here's a 2D game I wrote in Clojure for 
> comparision:http://github.com/ShardPhoenix/SAGame
>
> This was my first significant Clojure program so the code is a bit
> rough, but the core game state is handled in a pure-functional way,
> and is continuously passed around the main loop.
>
> --
> Ryan Sattler

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


Sharing a frustrating bug: using an atom to a hash as a hash

2011-04-20 Thread Tzach
I made a stupid mistake, and I want to share. This is not the actual code, I 
simplify it to demonstrate the point. 

I use a atom to a global hash as follow:

 (def *aa* (atom {:x 3 :y 4 :z 17}))


Somewhere on the code, I have a function which look at the state and do 
something:

(defn foo [state]

   (when (:x state) print "yes"))

 

My mistake - call foo as: 

(foo *aa*) 

where I should have call it 

(foo @*aa*)

 

Since (:x (atom *aa*)) is nil, print was not execute.

4 long hours later, I got it, fix it, and added the appropriate 
precondition.

I understand there is no escape from my own silliness, but could I have 
avoided this mistake to begin with?

Any best practice I broke?

 

Thanks

Tzach

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

Re: Problem Running ClojureScript on OpenJDK

2011-08-14 Thread Tzach
I have a similar problem, but I could not solve it like you did:
running on Ubuntu 11.04,
$JAVA_HOME set to /usr/lib/jvm/default-java, and default-java soft
link to java-6-sun

Still when I run script/repl, and
(require '[cljs.compiler :as comp])
(def jse (comp/repl-env))
(comp/repl jse)
CompilerException java.lang.RuntimeException:
java.lang.ClassNotFoundException:
sun.org.mozilla.javascript.internal.Context, compiling:(cljs/
compiler.clj:971)
user=> CompilerException java.lang.RuntimeException: No such
namespace: comp, compiling:(NO_SOURCE_PATH:2)
user=> CompilerException java.lang.RuntimeException: No such
namespace: comp, compiling:(NO_SOURCE_PATH:3)

Any idea?

Thanks
Tzach

On Jul 23, 9:38 am, Sean Corfield  wrote:
> On Fri, Jul 22, 2011 at 7:34 PM, Sean Corfield  wrote:
> > I may just switch to the Sun, er, Oracle JVM since I've a feeling one
> > of my other projects (not yet migrated to my netbook) will require
> > that JVM anyway...
>
> Just an update: I installed Oracle'sJDKand everything is working
> perfectly on myUbuntunetbook :)
> --
> Sean A Corfield -- (904) 302-SEAN
> An Architect's View --http://corfield.org/
> World Singles, LLC. --http://worldsingles.com/
> Railo Technologies, Inc. --http://www.getrailo.com/
>
> "Perfection is the enemy of the good."
> -- Gustave Flaubert, French realist novelist (1821-1880)

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