Re: (ab)using STM for longish calculations, network I/O

2010-10-15 Thread Nicolas Oury
dosync is a way of ensuring whole stateful operation is done atomically.
(ie as if everything was happening in one step.)

That contradicts b). During a whole dosync, you can only see one state
of the world.
If you do not need atomicity, do multiple dosync.

You should create another abstraction for the retry part of the problem.
(retry is an artifact of the non-blocking atomicity. If it fails, we
need to restart)

Interesting read if you do not know it already:


http://clojure.org/state

Best,

Nicolas.

-- 
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: precise numbers

2010-10-14 Thread Nicolas Oury
Another proof:

Let study the sequence sn = 0....9 , with n 9s.

Or s0= 0 and s(n+1) = sn + 9 / 10 ^n

lim sn = 0.9...
 and lim sn = 1.

so 
If I remember my meth correctly,
the number 0... does not exist.

This not a legal decimal sequence.
(Any decimal sequence finishing by .. is forbidden to allow a
one to one mapping between real numbers and
 decimal sequence.)

This kind of infinity is one of the reason equality is not devidable
on real numbers.

You can manipulate square root directly. For example by defining
numbers as a map from ratio to ratio.sqr
sqrt(5) is represented by {5 1}
sqrt(5) + 3. sqrt(2) by {5  1 , 2  3}
15 + sqrt(3)  {1 15, 3 1}

adding is just a reduce of one map into the other.
neg is a map.

multiplying is more complex. For each two pairs in the map
[a b] [c d], you check wether (ac) can be written as sqr(e).f
(For example 2 * 6 can be written as  sqr(2)*3)
If it is the case, you return [f (* e  b d)]
else you return [(* a c) (* b d)]

Dividing is more difficult.

I don't know if it is the most efficien way to do that, but it is the
easiest to code.

Best,

Nicolas.

-- 
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: generator in Clojure

2010-10-14 Thread Nicolas Oury
(defn iterate [s]
(let [a (atom s)]
(fn []
   (let [s @a]
  (reset! a (next s))
  (first s))

but it's not very idiomatic in clojure.
(In Lisp it is traditional to hide a state in a closure. A lot of toy
object language work like that)



On Thu, Oct 14, 2010 at 9:38 PM, Moritz Ulrich
ulrich.mor...@googlemail.com wrote:
 Are you sure you need to capture the state in next-sine? It's not very
 clojure-ly to have functions with state. I would capture the state in
 the caller as an integer and just use get or nth on the lazy seq.
 If you want to stick to your impure function, please mark it with a !
 at the end: next-sine!

 On Thu, Oct 14, 2010 at 9:52 PM, clwham...@gmail.com
 clwham...@gmail.com wrote:
 I need a function that produces the 'next' value from a lazy-seq --
 something like a Python generator. I imagine it would have to be some
 sort of closure like:

 (def next-sine
    (let [sines (atom (cycle (map sin (range 0 6.28 0.01]
        #(swap! sines rest)))

 Is there a more idomatic way of doing this? I don't see a lot of use
 of closures in clojure...

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



 --
 Moritz Ulrich
 Programmer, Student, Almost normal Guy

 http://www.google.com/profiles/ulrich.moritz
 BB5F086F-C798-41D5-B742-494C1E9677E8

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



-- 
Sent from an IBM Model M, 15 August 1989.

-- 
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: precise numbers

2010-10-13 Thread Nicolas Oury
On Tue, Oct 12, 2010 at 8:35 PM, cej38 junkerme...@gmail.com wrote:
 The more that I think about it, the more I would rather have a set of
 equalities that always work.  float= was a good try.



The only way to do so is to have numbers with infinite precision.

For example as lazy-seq of their digits.

But:
- it is slow
- equality is semi-decidable only.

-- 
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: precise numbers

2010-10-12 Thread Nicolas Oury
If you want to be really precise, most real numbers are an infinite
number of decimals.
If you encode them as a lazy seq of decimals, + - and other ops are doable.

Comparison is semi-decidable only: it terminates only in certain case
(finite number of decimals)
or when the number are different.

big-decimal or fractions are a good approximation, though.

-- 
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: clojure-cake

2010-10-08 Thread Nicolas Oury
I had a similar error last time I tried.
Didn't manage to solve it.

On Fri, Oct 8, 2010 at 10:49 AM, Sunil S Nandihalli
sunil.nandiha...@gmail.com wrote:
 I forgot to mention the versions...
 My cake version is 0.4.18
 and ruby version is ruby 1.8.7 (2010-01-10 patchlevel 249) [i486-linux]
 On Fri, Oct 8, 2010 at 3:17 PM, Sunil S Nandihalli
 sunil.nandiha...@gmail.com wrote:

 Hello everybody,
  I am trying to use cake .. was just exploring it with a very simple
 default
  cake new expcake
  cake deps
 but cake deps gives me the following error
 http://gist.github.com/616564
 can anybody suggest as to what could be going wrong?
 Thanks,
 Sunil.

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



-- 
Sent from an IBM Model M, 15 August 1989.

-- 
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: Clojure 1.3 alpha 1 report - bitwise operations extremely slow

2010-10-01 Thread Nicolas Oury
There is  no java definition for (Number, long) or (Number, int).

As 1 is now a primitive, I think it cannot find any corresponding function.

Strangely, putting 1M or (num 1) might be faster.

Can someone try?

On Fri, Oct 1, 2010 at 10:28 AM, David Powell djpow...@djpowell.net wrote:

 So, if it is true that range produces objects and dotimes produces
 primitive longs, then I believe that it is the odd interaction between
 bit-shift-left's inlining and long objects (as opposed to primitives)
 that is causing the disparity in your measurements, not something
 inherent in the mechanism of doseq vs dotimes.

 [Oops - sorry for the blank email]

 I notice, that If you enable *warn-on-reflection*, you can see that a call 
 using the inline version gets compiled as a reflective call. If you remove
 the inline definition, no such reflective call is made. Not sure why this is.

 --
 Dave

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



-- 
Sent from an IBM Model M, 15 August 1989.

-- 
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: Clojure 1.3 alpha 1 report - bitwise operations extremely slow

2010-10-01 Thread Nicolas Oury
 David pointed out what should have been the obvious overhead (I'll
 blame it on being up at 2am), and Nicolas pointed out the specific
 problem.
two solutions:

  - writing all combinations of unboxed/boxed for every function
  - having a more clever code generator that try to box every
primitive if it allows to prevent a reflective call.
(And maybe emit a boxing warning)

-- 
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: anonymous fn or partial?

2010-09-30 Thread Nicolas Oury
The two styles are ok.
Matter of taste.
(partial ...) have probably a slight cost I wouldn't worry about
except if profiler tells me to worry.


The (partial...) style is called point-less, because you directly
manipulate the arrows and not the points.
It is the same kind of question as : should you use composition or
call (f (g x))?
Should I use the do-monad notation or a clever combination of m-bind,
map, and composition?

No good answer. Do what you like best in each situation.

If you want to have something looking like (+ 2) with multiple args
possible, I would advocate the best way might be to
add a reader macro to clojure expanding to partial.  #p(+ 2) for example.
It is a better idea than using having evaluation depending of the context, IMHO.

-- 
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: question regarding macro (ab)usage

2010-09-28 Thread Nicolas Oury
I hadn't time to read the whole post, but, I I understand it well, this snipset

(defmacro with-context [options  body]
 `(let [context# (create-context options)
thread# (create-thread context)
sources# (atom {})
receivers# (atom {})]
(binding [init-receiver (partial init-receiver-on context# receivers#)
  init-source (partial init-source-on context# sources#)
  publish (partial publish-on context# sources#)
  receiver (partial receive-on context# receivers#)]
  (try
(do ~...@body)
(finally
  ... close stuff, cleanup)

can be written more or less:

(defn with-context [options action]
   (let [context ..])
(binding [init-receiver .]
 (try (action) (finally )))

That can be called with

(with-context  options #(body))

You can then wrap this call in a macro to remove the #().

I am not sure it is a good idea, but it is always good to know for
which precise feature you need the macro.

Here tou need it to prevent to have to pass some code around as a function.
That's one frequent usage of macro, it can help readability and (very
little) performance.
But depending of the situation. you might want to trade these
advantages for composability and ease of programming.


Best,

Nicolas.

-- 
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: How often do you use REPL?

2010-09-28 Thread Nicolas Oury
On Tue, Sep 28, 2010 at 12:03 PM, David Cabana drcab...@gmail.com wrote:
 My standard practice is to split the (Emacs) screen, one side is a
 Clojure mode edit session, the other a repl.  Best of both worlds.
 One can easily build up complex expressions as required,  and still
 easily evaluate expressions in either side of the screen.



Same here. Dream of having a bigger screen to have more than one file
and a REPL.

Use REPL all the time to test function and to try expression while
waiting complex functions.

-- 
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: Hiccup with Sequence

2010-09-27 Thread Nicolas Oury
doseq do not return anything. (It is for side-effect only).

You might be looking for 'for'.

(doc for)
-
clojure.core/for
([seq-exprs body-expr])
Macro
  List comprehension. Takes a vector of one or more
   binding-form/collection-expr pairs, each followed by zero or more
   modifiers, and yields a lazy sequence of evaluations of expr.
   Collections are iterated in a nested fashion, rightmost fastest,
   and nested coll-exprs can refer to bindings created in prior
   binding-forms.  Supported modifiers are: :let [binding-form expr ...],
   :while test, :when test.

  (take 100 (for [x (range 1) y (range 100) :while ( y x)] [x y]))


On Mon, Sep 27, 2010 at 11:25 AM, Paul paul_bow...@yahoo.com wrote:
 Hi all,

 I'm trying to output the items from a sequence into a HTML list using
 the Hiccup HTML builder, but am having problems.

 Here is the code I think 'should' work, but nothing after the ':ul' is
 passed back to the response:

 (html
        [:html
                [:body
                        [:ul
                                (doseq [item myseq] [:li item])
                        ]
                ]
        ]
 )

 Can anyone see why this is not working?

 Thanks,

 Paul.

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



-- 
Sent from an IBM Model M, 15 August 1989.

-- 
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: Macro expansion problem

2010-09-27 Thread Nicolas Oury
Difficult problem.

macro are syntactic tools. So they are not made to evaluate things at runtime.
You could expand to something that call eval at runtime but it is not
a good idea (It involves the compiler at each call)
If your (rest alist) is known at macro-expansion time, then it can
work but to help, I would need to know the context in which you are
using it.


What you want to do would be done by apply for functions.

However, as far as I know, apply do not work on method calls.
If you look at the problem from the java point of view, it is quite hard to
write some code that call a method with an unkown number of argument
without using reflection.

If you have a fix number of argument, you can do this:

(defmacro call-5
 Calls an instance method on a given object with a list of params.
 [obj method-name params]
 `(apply (fn [x1# x2# x3# x4# x5#] (. ~obj ~(symbol method-name) x1#
x2# x3# x4# x5#) ) ~params))

You can also write a macro that takes the arity as a parameter and do
that trick.

You can also generate an anonymous function with multiple arity
`(apply
  (fn ([x1#] (. ~obj ~(symbol method-name) x1# ))
   ([x1# x2#] (. ~obj ~(symbol method-name) x1#  x2#))
   ([x1# x2# x3#] (. ~obj ~(symbol method-name) x1#  x2# x3#))
  ... up to enough (20 should do the trick)  ~params)

However, ti might be easier to explain what you are trying to achieve
in a bigger context to see if there is a simpler path.


Best,

Nicolas.



On Mon, Sep 27, 2010 at 6:52 AM, stefanmuenchow stefanmuenc...@gmx.de wrote:
 I am a macro newbie... I want to create a macro that calls a function
 with a given name and a parameter list on a given object. My first
 idea was like this:

 (defmacro call
  Calls an instance method on a given object with a list of params.
  [obj method-name params]
  `(. ~obj ~(symbol method-name) ~...@params))

 It works fine, if the param list is a simple list, like (1 2 3 4),
 but if params is created from an existing list, like (rest alist)
 then it doesn't work. So params has to be evaluated first and then the
 single params has to be expanded. How do I do that? I tried it with a
 let block then I got other errors.

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



-- 
Sent from an IBM Model M, 15 August 1989.

-- 
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: Some code dramatically slower in Clojure 1.3 Alpha 1?

2010-09-25 Thread Nicolas Oury
 Your code was simple enough for me to make a couple of educated guesses. For
 more complex code I'd use VisualVM, https://visualvm.dev.java.net/
 David



I use that too.
Sampling for a first look, profiling with instrumentation for a more
precise answer.
(Here, the sampling gives even? and the profiling gives the reflection methods)

-- 
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: finding value nearest x

2010-09-25 Thread Nicolas Oury
On Sat, Sep 25, 2010 at 3:40 PM, Jules julesjac...@gmail.com wrote:
 Maybe this: (min-key #(abs (- % 136)) xs)

Wouldn't that be (apply min-key #(abs (- % 136)) xs)?

-- 
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: Some code dramatically slower in Clojure 1.3 Alpha 1?

2010-09-24 Thread Nicolas Oury
After profiling even seems effectively the culprit.
Some method reflector shows up too.

On Fri, Sep 24, 2010 at 6:15 PM, David Nolen dnolen.li...@gmail.com wrote:
 (defn next-term [n]
   (if (= (mod n 2) 0) (/ n 2)
       (inc (* n 3
 (defn count-terms [n]
   (if (= 1 n) 1
       (inc (count-terms (next-term n)
 (time
  (let [pair (juxt identity count-terms)
        pairs (map pair (range 1 10))]
    (println (first (apply max-key second pairs)
 It looks even? is the culprit here. The code above executes in  1 sec on my
 machine.
 So it looks like even? needs to repaired to account for the numeric changes.
 David

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



-- 
Sent from an IBM Model M, 15 August 1989.

-- 
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: Some code dramatically slower in Clojure 1.3 Alpha 1?

2010-09-24 Thread Nicolas Oury
Try

(defn even?
  Returns true if n is even, throws an exception if n is not an integer
  {:added 1.0
   :static true}
  [n] (zero? (bit-and (long n) (long 1


before your example.

It is fast on my computer.
(I believe there is a reflective call, without the explicit cast.)

-- 
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: Isn't STM good at building an ant colony?

2010-09-20 Thread Nicolas Oury
If you have a fixed geometry of cells, it is quite easy to have one
ref per cell.
Which reduce a lot of contention.

For example, on a grid where ant can go instead of representing the
world as a ref
to a matrix, you can represent the world as a matrix of refs.

Those refs can then be update concurrently. Provided there are a lot
of cells and not too much contention, iti should work well.



On Sun, Sep 19, 2010 at 11:01 PM, Hozumi fat...@googlemail.com wrote:
 Hi.
 I posted following question.

 The more threads that changes the Clojure's ref are, the more does the
 rate of retries per threads rise?
 http://stackoverflow.com/questions/3746893/the-more-threads-that-changes-the-clojures-ref-are-the-more-does-the-rate-of-re

 I think increasing retries in O(thread^2) means that refs should not
 be used to store the data that is aletered by too many threads like
 big ant colony map or newral network nodes.
 If modifying the data by too many threads is planned, instead of refs,
 are agents best way to store it?
 Any other solution?

 Thanks.
 Takahiro Hozumi

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



-- 
Sent from an IBM Model M, 15 August 1989.

-- 
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: why the big difference in speed?

2010-09-19 Thread Nicolas Oury
A first good start is to put
(set! *warn-on-relection* true) at the start of the file and removes
all reflective access.

Before the 1.3 release, function cannot receive/returns primitive so
you might consider
(defmacro next-gaussian []
  `(.nextGaussian  ^Random r))

(^Random is here to make sure r is seen with the right type)

in both function, add ^doubles before arr at the binding point.
(defn ... [^doubles arr])

then set will not be rflective and be as fast as a set in java.

Ask other questions of you need more help.
The best reference on all that: clojure.org/java_interop


On Sun, Sep 19, 2010 at 3:29 PM, Ranjit rjcha...@gmail.com wrote:
 Hi,

 I'm trying learn Clojure to see if I can use it in my simulations, and
 one thing I need to do is generate arrays of normally distributed
 numbers.

 I've been able to come up with the following two ways of doing this.
 gaussian-matrix2 is a lot faster than gaussian-matrix1, but I'm not
 sure why. And it's still slower than it should be I think. Is there
 anything I can do to speed this up still further?

 Thanks,

 -Ranjit

 (import java.util.Random)
 (def r (Random. ))

 (defn next-gaussian [] (.nextGaussian r))

 (defn gaussian-matrix1 [arr L]
     (doseq [x (range L) y (range L)] (aset arr x y (next-gaussian

 (defn gaussian-matrix2 [L]
     (into-array (map double-array (partition L (repeatedly (* L L)
 next-gaussian)

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



-- 
Sent from an IBM Model M, 15 August 1989.

-- 
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: partition-starting-every : yet another partition function

2010-09-17 Thread Nicolas Oury
I was just saying that not returning something that is a pair, for
example nil, is good enough.

 (unfold (fn [x] (when (= x 10)  [(* x x) (inc x)])) would work.

Both function can be written with each other anyway.

And they don't have the same number of args so they are compatible
with each other.

On Thu, Sep 16, 2010 at 8:05 PM, Gijs S. gijsstuur...@gmail.com wrote:
 Finished is a predicate which designates when the seed is exhausted.
 Because seed is not necessary a sequence, finished is not always
 empty?.



 For instance:



 = (unfold (fn [x] [(* x x) (inc x)]) #( % 10) 0)

 (0 1 4 9 16 25 36 49 64 81 100)



 Or the zipmap (zip2) example from the wikipedia page.



 Although the first example wanders back into territory where the
 existing sequence functions such as iterate, take-while and for would
 suffice.

 -Gijs

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



-- 
Sent from an IBM Model M, 15 August 1989.

-- 
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: partition-starting-every : yet another partition function

2010-09-17 Thread Nicolas Oury
(defn unfold
  ([grow seed]
   (lazy-seq
(if-let [[elt next-seed] (grow seed)]
(cons elt (unfold grow next-seed)
  ([grow  finished? seed]
   (unfold #(when (not (finished? %)) (grow %)) seed)))

(unfold (fn [x] [(* x x) (inc x)]) #( % 10) 0)
(0 1 4 9 16 25 36 49 64 81 100)

(unfold (fn [x] (when (= x 10) [(* x x) (inc x)]))  0)
(0 1 4 9 16 25 36 49 64 81 100)

I think it can be proved that any sequence can be build with a call to unfold.
Which makes it useful and solves the
why reduce is not lazy? question.

-- 
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: partition-starting-every : yet another partition function

2010-09-16 Thread Nicolas Oury
I think that unfold (or co-reduce, or generate) should find its way in contrib.

I am not sure we need finished arg though. The traditional finish in
the seq family is nil.

My own version of unfold:

(defn unfold
  (unfold seed grow) use the seed and a function grow that returns an
element and another seed
   to creates a lazy seq.
   The seq is stopped the grow function returns nil.
  [seed grow]
  (if-let [[elt next-seed] (grow seed)]
(cons elt
  (lazy-seq (unfold next-seed grow)))
()))

Whether the cons is in or outside the lazy-seq is debatable. (I like
to think of seq as the fixpoint of the functor (X - Cons Object (lazy
X)))

Best,

Nicolas.

-- 
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: Feature idea: meta-macros

2010-09-15 Thread Nicolas Oury
Your example can be solved with (binding ...)

For the proposal, I think it's a bad idea : huge potential for abuse
(and importing abuse from other namespaces written by other people)
and little benefit.

I wouldn't be so strongly against it if it was in a delimited scope.

In any case, you can probably implement it as a library on top of the
compiler, with a code walker.

-- 
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: Feature idea: meta-macros

2010-09-15 Thread Nicolas Oury
You can also use binding eval evil brother : alter-var-root.

On Wed, Sep 15, 2010 at 8:04 PM, Richard Newman holyg...@gmail.com wrote:
 My suggestion is inline with other commenters: use binding. If that doesn't
 satisfy you, consider using or writing a preprocessor like m4.

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



-- 
Sent from an IBM Model M, 15 August 1989.

-- 
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: why doesn't a function have a type ?

2010-09-14 Thread Nicolas Oury
On Tue, Sep 14, 2010 at 2:35 PM, Belun alexandrurep...@gmail.com wrote:
 why isn't the type of a function : clojure.lang.IFn ?


Actually you assume the existence of an unique type.
In most OO-language, and Clojure inherits that from its host, an
object has multiple types.
Indeed an object can be seen as of type its class or any of its
super-class or any of the interface it implements.
(Formally a : T' and T'  T  = a  T).

When you asked with type, clojure answer the most specific type. The
class it has created for this
function compilation.

However, if you ask instance?, you should see that any function implements IFn.
Best,

Nicolas.

-- 
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: clojurescript and 1.2.0

2010-09-13 Thread Nicolas Oury
Hi,

I soon realised that applying the patch won't be easy...
After going through the different rejected diffs, I realised I wasn't
going to manage to make it work.

I switched to Parenscript for my small JS project, even if I's rather
have used Clojure than CL (especially a very small subste of CL)

I agree it will be a better idea to wait for Clojure-in-Clojure.

Could you mail the list, when you want to start working back on this?
I would really be happy to help you with the clojurization of
clojurescript if you need more hands...


Best regards,

Nicolas.

On Mon, Sep 13, 2010 at 5:59 AM, Chouser chou...@gmail.com wrote:
 On Sun, Sep 12, 2010 at 9:04 AM, Nicolas Oury nicolas.o...@gmail.com wrote:
 Oooops.

 Ansered my question.

 Haven't seen the patch in the git repository.
 Will try to apply it to clojure 1.2.

 I imagine you'll have some difficulty with that.

 ClojureScript was last updated to work with Clojure 1.0 (with the
 patch applied) ... or perhaps even an earlier version than that.
 I've given up working on it until more of Clojure-in-Clojure is
 complete, which will make bringing ClojureScript up to date and
 keeping it there dramatically easier.

 --Chouser
 http://joyofclojure.com/

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



-- 
Sent from an IBM Model M, 15 August 1989.

-- 
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: clojurescript and 1.2.0

2010-09-13 Thread Nicolas Oury
Thanks for the link.
Very interesting indeed.
Didn't know about it.

On Mon, Sep 13, 2010 at 11:38 AM, Daniel Werner
daniel.d.wer...@googlemail.com wrote:
 On Sep 13, 9:40 am, Nicolas Oury nicolas.o...@gmail.com wrote:
 I switched to Parenscript for my small JS project, even if I's rather
 have used Clojure than CL (especially a very small subste of CL)

 There is also Scriptjure, which is basically JavaScript with Clojury
 syntax, similar to what ParenScript does for CL:

 http://github.com/arohner/scriptjure

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



-- 
Sent from an IBM Model M, 15 August 1989.

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


clojurescript and 1.2.0

2010-09-12 Thread Nicolas Oury
Dear all,

I cannot manage to make ClojureScript work from clojure 1.2.0.

It seems that *compiler-analyse-only* used to exist but do not exist anymore.

Does someone know what it was and what replaced it?

Best regards,

Nicolas.

-- 
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: clojurescript and 1.2.0

2010-09-12 Thread Nicolas Oury
Oooops.

Ansered my question.

Haven't seen the patch in the git repository.
Will try to apply it to clojure 1.2.



On Sun, Sep 12, 2010 at 1:52 PM, Nicolas Oury nicolas.o...@gmail.com wrote:
 Dear all,

 I cannot manage to make ClojureScript work from clojure 1.2.0.

 It seems that *compiler-analyse-only* used to exist but do not exist anymore.

 Does someone know what it was and what replaced it?

 Best regards,

 Nicolas.




-- 
Sent from an IBM Model M, 15 August 1989.

-- 
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: Generating functions programmatically

2010-09-11 Thread Nicolas Oury
 So the problem is solved for me, although I have to use eval. I'm not
 sure exactly how dirty this trick is and especially *why* it is
 considered a smell. I read it on Paul Graham's On Lisp and he
 vehemently opposes its use but he doesn't explain why or where it is
 acceptable. Note that he also considered Common Lisp let* a smell,
 which is standard practice in Clojure (and in fact there's no
 equivalent of Common Lisp let). So maybe we are just making too much
 a big deal of this eval thing.



It is considered bad because eval is for dynamic evaluation. In this
case you use it at macro time, which shouldn't be necessary most of
the time.

(defn my-complex-function [] )


(defmacro insert-the-result-of-the-complex-function []
  (my-complex-function))

In your case, as often for strange macro, first write a function that
does the job:

(defn extract-keyword-and-generate-code [].)

(defmacro  generate-methods []
   `(do
~@(extract-keyword-and-generate-the-code)))

If you have to use an eval, I find it nicer to use it to compute the
code, not to actually define things.
(defmacro generate-methods [expr]
   `(do
~@(extract-keyword-and-generate-the-code (eval expr

That way you can often work the eval out, by limiting the kind of
possible exprs.
(If it is only a symbol or a list of keywords, for example)

Anyway, that's just a suggestion. Your solution works too.

Best,

Nicolas.

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


new in ClojureScript

2010-09-11 Thread Nicolas Oury
Dear all,

I am trying to use ClojureScrip for fun. (FOr the REPL now).

I can not find a translation to the javascript:

var image = new Image();

(def image (Image.)) or (def image (new Image)) does not work.

Any idea?

Best regards,

Nicolas.

-- 
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: can't def m-bind because namespace

2010-09-09 Thread Nicolas Oury
Not really, but monad can be a bit tricky at first.

Your first email was a namespace related problem, that has little to
do with monads.

But it can be better to start with other part of clojure and come back
to monads later.

They can be useful to refactor code or write a nicer program, but you
can always do without them.

Best.

Nicolas.

On Wed, Sep 8, 2010 at 2:27 PM, MohanR radhakrishnan.mo...@gmail.com wrote:
 So actually it looks like I need to understand type theory to
 understand this.


 Thanks,
 Mohan

 On Sep 7, 7:04 pm, Nicolas Oury nicolas.o...@gmail.com wrote:
  ...and report your findings here or blog somewhere if you don't mind
  :) I've been reading a lot about monads lately and can't get my head
  around it yet so any help appreciated (I'm coming from Java and
  Clojure is my first real functional language - that's why it causes
  headaches, I believe).

 I have no blog so I will try an explanation here.
 I think monad are quite difficult to grasp, so the more different
 explanations you read, the better.
 Monads are made to represent computations in an abstract way.
 Sometimes the basic computation laws are not those you want
 and then you want to introduce other computation laws.
 Monad is one way to do so (among other like applicative functors or
 Kieisli arrows)
 Monad corresponds to the computations that are best described as an
 imperative program.

 So a monad is a type transformer: MA for all A , in Java words.
 Something of type MA is a computation in the world of computation M
 that returns values of type A.

 (M for example can be
   IO: the computations that do input/outputs to find their results,
   State: the computations that use a and update an internal state
   List: more surprisingly, the non-deterministic computations: each
 computation yeilds a list of possible results)

 There are a few operators you need for a monad:
 - return : A - MA . It says that any computation model must be able
 to handle the lack of computation. Do nothing and return this value
 -  bind : MA - (A - MB) - MB
 If I give you a computation that gives back values in A and for each
 values in A I tell you how to compute a value in B,
  then you can compute a value in B. 
 That's a strong assumption, because it allows to use the whole power
 of Clojure to construct a computation from the result of the first
 computation.
 Some model of computations are more restrictive than monad on that.

 From this two operators you can define two others:
 - map : (A - B) - MA - MB
 If I give you a function from A to B, then you can transform a
 computation that returns value in A in computation that returns values
 in B.
 (map f compA = (bind compA (fn [x] (return (f x)

 - join : MMA - MA
 I can run a subprogram This is again something quite specific to
 monad as computation devices. (Bind can be constructed from join and
 map)
 They are dynamic programs that can compute a program and run it.
 join compMA = (bind compMA identity)

 bind compA f = (join  (map f compA))

 All these operators must respect some laws, that are quite natural.
 Like returning a value and starting a computation is the same as starting a
 computation directly from the value:

 - (bind (return a) f) = (f a)

 

 To work out an example, the state monad is a computation that can use
 and modify a state, so: MA = S - [S, A] . I need a state and return
 a new state and a value A.
 return a = (fn [s] - [s a])
 bind compA f =
   (fn [s1]  -
     ; we give the state to the first comutation
    (let [[s2 a] (compA s1)
           ; we compute the next computation
          compB (f a)]
          ; we give the state to the second computation
          (compB s2)))

 Hope that helps.

 Nicolas.

 --
 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 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: Some Problem with Recursion

2010-09-09 Thread Nicolas Oury
(defn prefix-postfix [expr]
 (if (coll? expr)
   (let [ [op arg1 arg2] expr]
 [ (prefix-postfix arg1) (prefix-postfix arg2) op])) ;; HERE: 2
closing brackets first one close the let. second close the if.
   expr)

In Clojure, (if test expr else) is the if expression.

Here, you do (if test then) , which is valid Clojure and add nil as else.

So here what happens is you execute the if and after that, whatever
eas the value of (coll? expr) you return expr.

That's why I told you the indentation was wrong.

expr should be at the same level than (if , which would make the
error apparent.

Best,

Nicolas.

On Thu, Sep 9, 2010 at 2:27 PM, Stefan Rohlfing
stefan.rohlf...@gmail.com wrote:
 The indentation was correct by got messed up when I copying the code.

 This is how I interpret the function:
 'expr' is only returned if (coll? expr) returns 'false', with is not
 the case with an argument such as '(+ 2 4).
 Next, this expression is destructured in the 'let' and 'prefix-
postfix called again:

 [ (prefix-postfix 2) (prefix-postfix 4) +]

 As 2 and 4 are both not a collection, their values are just returned.
 This should lead to this final result:

 [2 4 +]

 However, no vector is returned, just the initial argument to the
 function.

 I really want to understand what I did wrong here :-)

 Stefan



 On Sep 9, 9:08 pm, Nicolas Oury nicolas.o...@gmail.com wrote:
 Yes.

 Have someone (for example your editor), do your indentation for you.
 By typing on tab on a good editor, you would have has:
 (defn prefix-postfix [expr]
    (if (coll? expr)
      (let [ [op arg1 arg2] expr]
        [ (prefix-postfix arg1) (prefix-postfix arg2) op]))
    expr)

 which is easier to diagnose. (At least for me)

 Have fun with recursion,

 Nicolas.

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



-- 
Sent from an IBM Model M, 15 August 1989.

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


Knowing in advance the complexity of count

2010-09-09 Thread Nicolas Oury
Dear all,


I want to write a generic code that use count when it is O(1) and not
when it is not O(n),

is it a way to do so?

Best regards,

Nicolas

-- 
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: Improved stack traces

2010-09-09 Thread Nicolas Oury
http://github.com/clojure/clojure

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


Strange bug with mutable fields and try

2010-09-08 Thread Nicolas Oury
Dear all,


Clojure 1.2.0

(deftype A [ ^{:unsynchronized-mutable true} foo ]
   Object
   (hashCode
[x] (set! foo :foo)
  5

   ))

works very nicely.


(deftype A [ ^{:unsynchronized-mutable true} foo ]
   Object
   (hashCode
[x] (try (set! foo :foo))

 ))


also works.


(deftype A [ ^{:unsynchronized-mutable true} foo ]
   Object
   (hashCode
[x] (try (set! foo :foo))
 4
 ))

fails:

Cannot assign to non-mutable: foo
  [Thrown class java.lang.IllegalArgumentException]

This is less useless than it seems. (try is called within locking,
which can be useful when setting something unsynchronized)


Is it a known bug? Does it happen to other people than me?

Best regards,

Nicolas.

-- 
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: can't def m-bind because namespace

2010-09-07 Thread Nicolas Oury
http://clojure.org/namespaces

You should require clojure.contrib.monad and bot use it.

(ns my-namespace
 (:require (clojure.contrib.monad :as m))

m/m-bind, for example.

Then you can define your own m-bind without conflict with an existing one.

On Tue, Sep 7, 2010 at 1:13 PM, MohanR radhakrishnan.mo...@gmail.com wrote:
 java.lang.Exception: Name conflict, can't def m-bind because
 namespace: user refers to:#'clojure.contrib.monads/m-bind

 What namespace help doc. should I read to resolve this issue ? Maybe I
 should not read about monads first.

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


Mapping a function to a map

2010-09-06 Thread Nicolas Oury
Dear all,

is there a function to map a function to all values in a map, keeping
the same keys?
Reducing from the seqed map seems a bit slower that what could be done
directly...

Best,

Nicolas.

-- 
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: Mapping a function to a map

2010-09-06 Thread Nicolas Oury
Different reactions:

1. The reduce solution is O(n .log n) + time of mapping elements, as
inserting in a map is O(log n). A tree with n leafs and arity at least
binary is of size at most 2n. So a map on a map could be done
 in O(n) + time of mapping elements, but it would need a bit of
support from runtime.

My question was : is there such support in runtime? Is it planned?
Would such a patch be welcomed or not?


2. lazy maps would not be performant. A seq of pairs would have a O(n)
look up. Clojure hash-map and sorted-map have a O(log n) look up.
Having a seq of pairs can be done manually but is less useful than
real maps.

3. reduce is strict because it is a consumer. The co-operator of
reduce is a producer and should be lazy.
reduce type is (a - b - b) - (seq a) - b
a co-reduce would be : (a- (a , b)) - a - (seq b), meaning, with a
state, you produce an element and another state(a is the type of state
and b of element).
It is often called unfold and would be a nice addition to clojure.contrib.
I think that most lazy sequence can provably be written as (), cons
and unfold, but I am not sure and have no reference in mind.

Best,

Nicolas.

-- 
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: How to use Java array of primitive types as key of Clojure map?

2010-09-01 Thread Nicolas Oury
Multiple things:

the internal type name for int arrays is [I

So you should try something like:
(deftype T [^[I keys ]

)

then you are looking to overload equals and hashCode from the Object interface
(deftype T [^[I keys ]
Object
(equals [x other]
 (java.util.Arrays/equals key (.key other)))

)

And replace other by ^T other, to prevent reflection.
(You should actually check that it is of the right type, by using
(identical? (type other) T). (instance? does not seem to work in its
own deftype) )

Best,

Nicolas.

-- 
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: Help speed up an inner loop?

2010-08-31 Thread Nicolas Oury
I am not convince that (make-array Byte/TYPE *numbytes*) creates an
array of primitives.
And I think byte [] is an array of primitives.

That would make a difference.

I don't know if clojure has a byte-array. It seems that there is no
byte-array as there is int-array.

Could you try your code with int-array, or could you just write two
static methods in a Java class to read and write an int from a byte
array?

Best,

Nicolas.

-- 
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: Help speed up an inner loop?

2010-08-31 Thread Nicolas Oury
On Tue, Aug 31, 2010 at 1:53 PM, Nicolas Oury nicolas.o...@gmail.com wrote:
 I am not convince that (make-array Byte/TYPE *numbytes*) creates an
 array of primitives.
Actually it does, sorry for the noise.

Should check before sending emails.

Best,

Nicolas.

-- 
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: Why do is used in this function?

2010-08-31 Thread Nicolas Oury
One solution to remove it is to use when.

 (when (not (empty? s)
  (println (str Item:  (first s)))
  (recur (rest s

As there is only one case for a when, you can give multiple
instructions for this case without grouping them
Even better:

(when-not (empty? s)
 (println (str Item:  (first s)))
  (recur (rest s

-- 
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: Help speed up an inner loop?

2010-08-31 Thread Nicolas Oury
Replace also  (unchecked-add count 1) with  (unchecked-add count (int 1))

(this should get easier in 1.3)

On Tue, Aug 31, 2010 at 4:20 PM, tsuraan tsur...@gmail.com wrote:
 (defn countnl-lite
  [#^bytes buf]
  (areduce buf idx count (int 0)
           (if (= (clojure.lang.RT/aget buf idx) 10)
             (unchecked-add count 1)
             count)))

 Key points are initializing count to a primitive integer and directly
 calling clojure's aget to avoid an unnecessary integer cast.

 Are you using clojure 1.2?  If I try to set count to be (int 0) rather
 than 0, I get this error:

 Exception in thread main java.lang.RuntimeException:
 java.lang.IllegalArgumentException: recur arg for primitive local:
 count must be matching primitive

 Even if I replace inc with the unchecked-add, or cast the result
 of the inc to be int (replace (inc count) with (int (inc count)) )
 it gives me that error.  Sort of strange...

 --
 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 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: Help speed up an inner loop?

2010-08-31 Thread Nicolas Oury
This one is quite good for me.
(defn countnl
 [#^bytes buf]
 (let [nl (int 10)]
   (areduce buf idx count (int 0)
(if (== (int (aget buf idx)) nl)
  (unchecked-inc count)
  count


It appears that == is not resolved for bytes. So converting to int works fine.

In this situation, inlining (int 10) does not buy much.

This one, though, is even faster and should be not far from the java
one, if you want to try it.

(defn countnl
 [#^bytes buf]
 (let [nl (int 10)
n (int (alength buf))]
   (loop [idx (int n) count (int 0)]
 (if (zero? idx)
 count
 (let [idx (unchecked-dec idx)]
   (if (== (int (aget buf idx)) nl)
 (recur  idx (unchecked-inc count))
 (recur  idx count)))

It would be interesting to know why it is nearly twice as fast as the
areduce version on my computer.

Best,

Nicolas.

-- 
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: Help speed up an inner loop?

2010-08-31 Thread Nicolas Oury
On Tue, Aug 31, 2010 at 8:43 PM, tsuraan tsur...@gmail.com wrote:

 In this situation, inlining (int 10) does not buy much.

 interesting; for me replacing the 10 with (int 10) brings my times
 from 28.7ms to 19.6ms.

I meant putting (int 10) instead of nl and a let.

Anyway, it seems that we can get to java speed, but with some hard work.

I have read somewhere than 1.3 will help a lot to achieve this
performance with less work.

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


Is there a construct-hash macro?

2010-08-29 Thread Nicolas Oury
Dear all,

when you write a deftype and want to define a hashCode method, it
would be really great to have access to a macro

(construct-hash arg1  argn)

that would compute the hash code of each arguments and combine them in
a good way to produce a hash-code.

Is there anything like that in core or contrib?


(The same kind of idea could be applied for a generic equality macro,
that checks identity, type and then the pairs of arguments given)

Best regards,


Nicolas.

-- 
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: Clojure 1.3: Integrating clj-stacktrace?

2010-08-25 Thread Nicolas Oury
I haven't had a lot of problems with stack-traces.

I would be happy to have more information on the context, though.

And maybe better reporting of exception occuring in a delayed context.
(when forcing a seq and the excpetion occurs under a lazy.)
In this situation I have sometimes fonud that the stacks where a bit
srange, but I understand it is a well known problem with lazyness and
difficult to solve.

On Wed, Aug 25, 2010 at 12:54 PM, Peter Schuller
peter.schul...@infidyne.com wrote:
 +1 on improving stack traces (though I haven't had experience with
 clj-stacktrace, other than what I have read on this list).

 --
 / Peter Schuller

 --
 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 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: Clojure 1.2 and the Computer Language Benchmarks Game

2010-08-25 Thread Nicolas Oury
You can probably boost n-body on 1.2 by replacing arrays with deftypes.

(definterface BodyIsh
  (^double getMass [])
  (setMass [^double x])
  (^double getPosX [])
.)


(deftype Body [^double ^{:unsynchronized-mutable true} mass ^double
^{:unsynchronized-mutable true} posX .]
BodyIsh
(getMass [x] mass)
(setMass [x m] (set! mass m)
.)

And then rewriting the program using Body (and  a type annotation on
bodies, of course)
I used interfaces and not protocols here because I am not sure 1.2
supports protocols with primitives.

Best,

Nicolas.

-- 
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: Clojure 1.2 and the Computer Language Benchmarks Game

2010-08-24 Thread Nicolas Oury
 Clojure 1.3's performance improvements will significantly impact perf on 
 some of the benchmarks. If you are trying these out, please try them on 
 both 1.2 and 1.3.


 Has Clojure 1.3 been released?


 No, but since the num/prim/equiv work specifically targets performance, we 
 want to collect people's experiences comparing 1.2 and 1.3. This is totally 
 separate from the benchmark submission process and for our own information.


I have a project that is development and will still be for some time.
I don't mind if it does not work perfectly before 1.3 and I will be
very happy to have it very fast when 1.3 is ready.

I am quite happy to make a branch to check improvement vs 1.2, but I
wouldn't know how to change my code to benefit from the features of
1.3.

Is there some links about that?
Are the prim/equiv work in master yet? And the :static defn with
primitives arg? And the protocols with primitive?
Are there other improvements?

I have a lot of primitives that are spread in a big part of the code,
so I think I would benefit from that?

Best,

Nicolas.

-- 
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: Clojure 1.2 and the Computer Language Benchmarks Game

2010-08-24 Thread Nicolas Oury
On Tue, Aug 24, 2010 at 5:33 PM, Isaac Gouy igo...@yahoo.com wrote:

 Well when Clojure 1.3 is released...

 The phrase idiomatic code often seems to be used to mean - code
 written in a natural way for that language and as if performance
 doesn't matter - whereas I seem to have the strange notion that both
 code written as if performance matters and code written as if
 performance doesn't matter can be  idiomatic code.

For most of the code, being slower (in a reasonable limit) is not
important if you are correct and easily maintable, and reusable.

-- 
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: why data structure

2010-08-23 Thread Nicolas Oury
On Mon, Aug 23, 2010 at 3:45 AM, Victor Olteanu bluestar...@gmail.com wrote:
 Some examples to illustrate this would be very welcome.

Any macro is an example of that.
For example, from clojure/core.clj

(defmacro -
  Threads the expr through the forms. Inserts x as the
  second item in the first form, making a list of it if it is not a
  list already. If there are more forms, inserts the first form as the
  second item in second form, etc.
  {:added 1.0}
  ([x] x)
  ([x form] (if (seq? form)
  (with-meta `(~(first form) ~x ~@(next form)) (meta form))
  (list form x)))
  ([x form  more] `(- (- ~x ~form) ~...@more)))


You can really easily write functions that write programs, as the above -.

If the AST of LISP were more complicated, this kind of program would
be more complicated.

-- 
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: why data structure

2010-08-23 Thread Nicolas Oury
And it's usage is far less generalised than macros in LISP.
It is not an usual solution to write acamlp4 preprocessor.

On Mon, Aug 23, 2010 at 1:06 PM, Meikel Brandmeyer m...@kotka.de wrote:
 Hi,

 On 23 Aug., 14:03, Nicolas Oury nicolas.o...@gmail.com wrote:

 If the AST of LISP were more complicated, this kind of program would
 be more complicated.

 eg. see OCaml's camlp4. I found it complicated compared to Lisp style
 macros.

 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

-- 
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: parallel execution

2010-08-23 Thread Nicolas Oury
 This seemed to be in clojure.parallel, but parallel is deprecated.  Why is 
 it deprecated,
 and what's the replacement for it?

 I'd like to know that as well!

I am not sure, so don't believe this blindly.
I think it is due to changes in the plan for Java 7 and JSR266y.

Some of the dependency of clojure.parallel have been removed of the
plan for Java7 and it is
to be rewritten with what will be included in Java7.

-- 
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: why data structure

2010-08-22 Thread Nicolas Oury
On Sun, Aug 22, 2010 at 11:30 AM, Belun alexandrurep...@gmail.com wrote:
 why does everything have to be a data structure ? like (operation
 parameter parameter ...)

Because it makes really easy to do meta-programming.
If you want to generate some code, it is easier to do so if you just
have to construct a data structure.

And it is very regular and easy to learn and remember.

-- 
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: Implementing a protocol with using a base implementation?

2010-08-21 Thread Nicolas Oury
On Sat, Aug 21, 2010 at 6:33 AM, Toni Batchelli tbatche...@gmail.com wrote:
 P-impl.))
 (dotimes [_ 10] (time (dotimes [_ 1] (.m1 my-simple-P hello ;
 Elapsed time: 131.973 msecs
 Elapsed time: 142.72 msecs
 Elapsed time: 95.51 msecs
 Elapsed time: 95.724 msecs
 Elapsed time: 83.646 msecs

Could you try by calling m and not .m1?

-- 
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: Implementing a protocol with using a base implementation?

2010-08-21 Thread Nicolas Oury
m1 I meant.

Apologies.

On Sat, Aug 21, 2010 at 8:36 AM, Nicolas Oury nicolas.o...@gmail.com wrote:
 On Sat, Aug 21, 2010 at 6:33 AM, Toni Batchelli tbatche...@gmail.com wrote:
 P-impl.))
 (dotimes [_ 10] (time (dotimes [_ 1] (.m1 my-simple-P hello ;
 Elapsed time: 131.973 msecs
 Elapsed time: 142.72 msecs
 Elapsed time: 95.51 msecs
 Elapsed time: 95.724 msecs
 Elapsed time: 83.646 msecs

 Could you try by calling m and not .m1?


-- 
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: Game development in Clojure

2010-08-21 Thread Nicolas Oury
Just my  2 cents:

sometimes these algorithms are easier to implement by constructing an
infinite lazy trees of all the game, and
then independently writing a few strategy to explore the tree.

John Hughes gives an example of that in Section 5 of Why functional
programming matters, that can be found on the internet.

If it can be done that way, this is nice has it separates the tree
construction, which is about the rule of the game, from the
evaluation/exploration part.

On Fri, Aug 20, 2010 at 9:47 PM, Ben Mabey b...@benmabey.com wrote:
  On 8/20/10 12:21 PM, Alan wrote:

 Thanks, I'll look into it. I know minimax ought to be easy to do but
 it's a bit of a weak spot of mine - I can never seem to get it right,
 and the poorish debug support in clojure, even with slime/swank,
 doesn't make it easier.

 I'm reasonably confident minimax/alpha-beta is right for me, since
 it's not really an AI - I'm writing a solution finder which works
 with perfect knowledge and has all the time it needs (though obviously
 faster is better) to find the optimal solution. If anyone's
 interested, it's specifically a double-dummy solver for the card game
 bridge.

 I stumbled across this implementation of alpha-beta in clojure on github:

 http://github.com/rcrr/reversi/blob/master/strategies.clj#L251-282

 It is a re-write of PAIP's CL version.  (I didn't write it- just found it.)

 -Ben

 On Aug 16, 3:08 am, Mike Andersonmike.r.anderson...@gmail.com
 wrote:

 On Aug 13, 5:33 pm, Alana...@malloys.org  wrote:

 Funny you should mention this - I was about to post a question about
 my own game when I saw your article. My issue is, I assume someone has
 written minimax and/or alpha-beta pruning in Clojure (or a java
 library that's easy to interop with). My case is slightly different in
 that the turn order is not fixes - sometimes player X goes twice in a
 row - but it ought to be pretty simple to plug into a standard AI
 library. Does anyone know where I can find such a thing?

 I don't actually use alpha-beta: I opted to spend the effort to
 develop
 a decent evaluation function and then do some simple local
 optimisation
 on the gradient of said function.

 My reasoning was that alpha-beta usually works best when the
 branching
 factor is low and the evaluation function pretty cheap to calculate,
 sadly my situation was pretty much the reverse :-)

 For your case it may be different. Two turns in a row works fine for
 minimax or alpha-beta with a little tweaking (although it is likely
 to
 cut your search depth).

 Be warned though - my experience is that it's rather hard to find an
 AI library that will just plug in nicely to your code. Most of the
 challenge in AI tends to be around special cases, embedding expert
 knowledge and heuristics, plumbing in the right data representations
 etc.
 The search algorithm itself is usually the easy bit

 If you're after resources, there's a decent free online book on AI in
 Java that might be useful, has lots of code examples that should be
 pretty easy to convert to Clojure:

 http://www.scribd.com/doc/6995538/Practical-AI-in-Java

 --
 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 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: let binding ~'=

2010-08-21 Thread Nicolas Oury
Clojure whitin a `(...) context resolves the name space  of symbols.
This is most often what you want and prevent some name collisions at
macro expension.

But as binders can not be name-space resolved  this do not allow to write:

`(let [and ])

this would expand to (let [ns/and ...]) which is forbidden.


The usual solutions (choose the first applicable) :

- use automatic name generation
`(let [and# ] and#)

- generate a symbol name:
(let [my-fresh-sym (gensym foo_)]
`(let [~my-fresh-sym ...])

It is mostly useful when the same symbols occurs in a complex
structure of `(...)s.

- If you really need to choose the name a binding symbol: use ~'symbol.
This says to the evaluation of `() replace by exactly symbol
So
(`let [~'and ...]) will expand to (let [and ...] ) this is a problem
because it can conflict with some other names.
However, in some case it is very useful/necessary. Especially when you
want to expose some symbols to the macro user.














On Sat, Aug 21, 2010 at 3:20 PM, limux liumengji...@gmail.com wrote:
 This is some code in a blog of William Gropper, the useage of ~'=
 confused me, waiting some more detailed explain, advanced thanks

 (defmacro filter
  [pred query]
  `(let [~'and (fn[ xs#] (apply str (interpose  AND  xs#)))
 ~'or (fn[ xs#] (apply str (interpose  OR  xs#)))
 ~'= (fn[x# y#] (sql-exp = x# y#))
 ~' (fn[x# y#] (sql-exp  x# y#))
 ~' (fn[x# y#] (sql-exp  x# y#))
 ~'like (fn[x# y#] (sql-exp like x# y#))
 ~'in (fn[x# xs#]
 (str (sqlize x#)  IN ( (apply str (interpose ,  xs#)) )))]
     (apply str ~query  where  ~pred)))

 --
 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 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: Processing large binary file?

2010-08-21 Thread Nicolas Oury
I am not sure but I think filter will always output a sequence.

You can either:
- write filter-array using a loop/recur.

- read/write lazily the file in a sequence and use sequence function.
If it is possible in your situation, I would advise this one.



On Sat, Aug 21, 2010 at 4:42 PM, Piotr 'Qertoip' Włodarek
qert...@gmail.com wrote:
 I need to process large binary files, i.e. to remove ^M characters.

 Let's assume files are about 50MB - small enough to be processed in
 memory (but not with a naive implementation).

 The following code works, except it throws OutOfMemoryError for file
 as small as 6MB:

 (defn read-bin-file [file]
  (to-byte-array (as-file file)))

 (defn remove-cr-from-file [file]
  (let [dirty-bytes (read-bin-file file)
        clean-bytes (filter #(not (= 13 %)) dirty-bytes)
        changed?    ( (count clean-bytes) (alength dirty-
 bytes))]    ; OutOfMemoryError
    (if changed?
      (write-bin-file file clean-bytes)   ; writing works fine
      nil)))

 How to force 'filter' to be efficient, i.e. create another array
 instead of a memory-blowing list?

 How to approach processing large binary data in Clojure?

 --
 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 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: questions about float operations

2010-08-20 Thread Nicolas Oury

 - why does (+ 4.2 8.4) return 12.601 and (+ 1.5 2.6) 4.1?
 Since 4.2, 8.4 and (+ 4.2 8.4) are java Doubles why does it not behave
 as expected? What does clojure do in the background?

That's not a bug. Doubles have a standard. Clojure implementation
follows the standard.
(as most programming languages does)

The thing is that a floating point number have not infinite precision.

The only warranty is that it does not create this kind of problem on
integer values.
You can learn more on this on wikipedia I believe.

http://en.wikipedia.org/wiki/Floating_point#Representable_numbers.2C_conversion_and_rounding


 - I also have a question regarding float precison in big operations,
 consider the following code:
 (defn ope [a b] (+ a (* b (Math/sqrt b

 (reduce ope (range 100))
 3.9501039E14

 vs

 (loop [i 1 x 0]
  (if ( i 100)
    x
    (recur (inc i) (ope x i
 4.0501039E14 (which is what we get if we do a for loop in
 java)

 I had the same problem in haskell, why are the two results different?


I would say that the last number of range is not included in the sum.
10^6 * 10^(6/2) = 10^9

Which is about the difference. range is exclusive on the right.

(doc range)

-- 
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: cool compiler-project?

2010-08-19 Thread Nicolas Oury
There is a size limit on methods on the jVM.

partial-evaluator would be a cool project, I think.

On Thu, Aug 19, 2010 at 6:38 AM, Tim Daly d...@axiom-developer.org wrote:
 Could the compiler insert phantom method bodies around classes?
 Or does the JVM insist that you can't lie about the code structure?
 Am I being too lispish for the JVM? Clearly the JVM needs to look up
 some factoid out of the method body in order to recur so I'm suggesting
 that the whole set of classes get wrapped in a phantom method.

 The code that comes out of the compiler does not need to mirror
 the code that went into the compiler. That's the whole point of
 doing compiler optimizations.

 Perhaps a literature pointer could make the restriction clear.

 Tim Daly

 Kevin Downey wrote:

 only in the cases already handled by recur.

 jvm's goto instruction only works within method bodies

 On Wed, Aug 18, 2010 at 6:24 PM, Tim Daly d...@axiom-developer.org
 wrote:


 Write a compiler routine to detect tail recursion.

 It is my (limited) understanding that Java can perform
 tail recursion elimination but only under limited
 conditions.

 Is there a way to detect tail recursion in Clojure and
 dynamically rewrite the code to do real recursion rather
 than using recur? Is your solution general enough?

 Mark Derricutt wrote:


 Or a native dalvik compiler!

 --
 Pull me down under...



 On Thu, Aug 19, 2010 at 10:37 AM, Jules julesjac...@gmail.com
 mailto:julesjac...@gmail.com wrote:

   Or a Clojure to Javascript compiler. So many interesting projects!


 --
 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 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 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 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: Impedance mismatch

2010-08-19 Thread Nicolas Oury
In which situation nil is not treated as false in Clojure?

If I understand well, Clojure separates nil/false from (). In
particular, if you use next and not rest, your iteration
example will work if translated into Clojure.

empty? works in all case and is not ugly either.

For the java typing story, I think this problem is one of the cause of
the big activity of Clojure, JRuby, Groovy and the like.

I find that I write very little type annotations in Clojure, mainly on
interfaces, and never far away from a method call on that interface.
(To prevent a call to go through reflection).

In practice, it only prevents me from using the same method call for a
method of the same name in another interface.
It is not something that I often (I have no memory of any case) want
to do, even if I understand that for people coming from a Ruby/Python
background, it can be annoying.

These calls to an interface are in a first layer above the JVM and
tend to be rare in Clojure programs. (Library interface, mostly)

Best,

Nicolas.

-- 
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: Feedback on idiomatic clojure

2010-08-19 Thread Nicolas Oury
A few remarks:

 ## begin
 (defn f-to-seq[file]
  (with-open [rdr (java.io.BufferedReader.
                    (java.io.FileReader. file))]
    (doall (line-seq rdr

Do you need the doall?

 (defn str-sort[str]
  (if (nil? str)
    str
  (String. (into-array (. Character TYPE) (sort str)

(and str
   (String. (into-array (. Character TYPE) (sort str

(if str is nil this will return nil)


 (defn anagram-add[anagrams akey word]
  (if (empty? (get anagrams akey))
    (assoc anagrams akey (hash-map :count 1, :words (list word)))
    (update-in (update-in anagrams [akey :count] inc) [akey :words]
 conj word)))

You could use a literal {:count 1 :words (list words)} instead of hash-map

I believe that (get anagrams akey) will give nil in case there is known.
If it is the case (or (get anagrams key) ...) will be more readable.

(update-in ...) =

(- anagrams
  (update-in [akey :count] inc)
  (update-in [akey :words] conj word))

I will have a look to the rest of the program later.

Best,

Nicolas.

-- 
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: Clojure 1.2 Release

2010-08-19 Thread Nicolas Oury
Congratulations!!
I am very happy with 1.2, as everybody I think. Great improvements to
my favorite language.

Your announcement got me curious: what are the future call linkage improvements?

Thanks to all of you, it's great.

Best,

Nicolas.

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

2010-08-19 Thread Nicolas Oury
I use Ctrl+D. But I am on Linux.

On Thu, Aug 19, 2010 at 4:28 PM, Abraham Varghese abev...@gmail.com wrote:
 How to exit from REPL?

 --
 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 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: Program design

2010-08-19 Thread Nicolas Oury
A big part of inheritance can be done by using defrecord, keywords and
functions instead of method, and getting read of the abstract class.


(defrecord Orange [:mass :energy :juice])

(defrecord Apple [:mass :energy :juice : family])

(defn get-juice [fruit] (:juice fruit))

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


Fwd: Feedback on idiomatic clojure

2010-08-19 Thread Nicolas Oury
Damon reply to me and not the list, so I forward.

On Thu, Aug 19, 2010 at 9:09 PM, Damon Snyder drsny...@gmail.com wrote:
 Hi Nicolas,
 Thanks for the suggestions. Regarding the first one: ah, I see. That
 is a nice compact way to test to see if the str is nil. I added that

I reckon that Meikel's suggestion of using when is surely better in
this situation (for a and).
But the (or something? default) or (or known? recompute?) can be useful.

 in because I was getting null pointer exceptions when the string was
 null. What is the difference between {:count 1 :words (list words)}
 and a hash-map? I was under the impression that {} created a hash.

I just find it easier to read because it looks like a value and not a
function call.

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


Simple question about name-spaces

2010-08-18 Thread Nicolas Oury
Dear all,

I have a simple problem I can't manage to solve.

I have a library of multiple namespaces( a b c) that I want to include
in one namespace lib, so user of
the library can only use/require lib and have access to all the
functions i a, b and c.

What is the standard way to do that?

Best,

Nicolas.

-- 
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: Simple question about name-spaces

2010-08-18 Thread Nicolas Oury
That helps a lot. Thank you very much.

That is not very nice though. I quite would like a :reexport option to use.

Best,

Nicolas.


On Wed, Aug 18, 2010 at 11:17 AM, Meikel Brandmeyer m...@kotka.de wrote:
 There is no standard way of doing that. There is immigrate of an old
 Compojure version, which is a hack at best. If all functions should
 end up in one namespace anyway, you can have a master file, which
 basically loads the other files.

 my/name/space.clj:

 (ns my.name.space
  (:load a b c))

 my/name/{a,b,c}.clj:

 (in-ns 'my.name.space)

 

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


Re: Simple question about name-spaces

2010-08-18 Thread Nicolas Oury
If you intern all the ns-public variable of a namespace, they will be
reexoprted?

Will there be an indirection at runtime or the JVM can sort that 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


accessing a mutable field outside of a deftype

2010-08-18 Thread Nicolas Oury
This works

(deftype A [a])

(.a (A. 5))

This don't

(deftype A [^{:volatile-mutable true} a])

(.a (A. 5))

Is this normal? Is this a bug?

How could I access the field?

Best,

Nicolas.

-- 
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: Today's clojure trick question

2010-08-18 Thread Nicolas Oury
(defmacro fat-if)

On Wed, Aug 18, 2010 at 4:09 PM, Brian Hurt bhur...@gmail.com wrote:
 Consider the following bit of code:

 (let [ x (new java.lang.Boolean false) ] (if x trouble ok))

 As you might guess from the fact that I'm calling it's a trick question, the
 above code returns trouble, not ok.  From experimentation, it looks like
 clojure's if takes the second branch if the value is nil or referentially
 equal to java.lang.Boolean/FALSE.  If you have a boolean object which is not
 referentially equal to Boolean/FALSE, but still holds the value false, this
 is treated as true by Clojure.

 The problem is, I'm not 100% sure if this is a bug or not.  The problem is
 nil-punning.  The if statement can't rely on the test expression evaluating
 to a boolean.  Which means to make this work correctly, if statements would
 have to do an instanceof test (to see if the object was a Boolean), then a
 cast and a call to .booleanValue to get the boolean value.  Which is way
 more expensive than a quick nil test and referential equality test.

 This is, however, more than a little bit surprising and depressing.
 Somewhere, in my 10K lines of clojure code, boolean values are getting boxed
 in exactly that way.  I've fixed the current problem (dropping in a call to
 .booleanValue in the test), but having if statements that can fail makes me
 paranoid.

 Brian

 --
 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 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: accessing a mutable field outside of a deftype

2010-08-18 Thread Nicolas Oury
And they need to be in an interface first?

-- 
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: accessing a mutable field outside of a deftype

2010-08-18 Thread Nicolas Oury
And is the one that works (for non-mutable fields) reliable or just an
implementation accident that could change in the future?

On Wed, Aug 18, 2010 at 4:27 PM, Nicolas Oury nicolas.o...@gmail.com wrote:
 And they need to be in an interface first?


-- 
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: Today's clojure trick question

2010-08-18 Thread Nicolas Oury
I am not an expert. Is it possible on some JDK to put a breakpoint on
Boolean constructor and look at the stack?
Or you can't put a breakpoint on standard library?

-- 
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: Nil Coalesce

2010-08-18 Thread Nicolas Oury
I have a solution :

(defn unfold [f  as]
   (if-let [[hd new-as] (apply f as)]
 (lazy-seq
  (cons hd (apply unfold f new-as)))
  ()))


unfold could be called co-reduce or coreduce. It is the dual operation
of reduce, in category theory.
Instead of reducing a seq to create a value, it generates a seq from a
value. (generate is sometimes a name for that, too)
I don't know if it is somewhere in core or contrib.
With this function, it is very simple:
(defn nil-coalesce [s1 s2]
  (unfold #(and %1
 (if (first %1)
[(first %1) [(next %1) %2]]
 [(first %2) [(next %1) (next %2)]])) s1 s2))

-- 
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: What is the reason Lisp code is not written with closing parenthesis on new lines?

2010-08-18 Thread Nicolas Oury
auto-indentation and parens highlighting are better than lines with
only one parens.

At least for me.

There is no law. Do what is best for you.

You might, or not, change your mind when you have more practice with
all those parens.

-- 
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: defprotocol not working?

2010-08-18 Thread Nicolas Oury
You can create a new project, even if you don't use it:

lein new experiments

inside it will create a project.clj.

Checks the dependencies look like that:

   :dependencies [[org.clojure/clojure 1.2.0-RC3]
 [org.clojure/clojure-contrib 1.2.0-RC3]

Type lein deps in the directory. It will download the latest version of clojure.

lein repl, will open a repl.

You can also have a look to the cljr project
http://github.com/liebke/cljr

It is intended for repl-based project.


Hopes that helps.


On Wed, Aug 18, 2010 at 12:29 PM, Henrik Mohr lupos...@gmail.com wrote:
 updated it to support clojure 1.1, and now when I want a REPL I type


-- 
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: defprotocol not working?

2010-08-17 Thread Nicolas Oury
defprotocol is a new feature of Clojure 1.2.
A fantastic RC3 of this fantastic software is available.

A good way to get it is to use the amazing Leiningen.


On Tue, Aug 17, 2010 at 7:17 AM, Henrik Mohr lupos...@gmail.com wrote:
 Hi there!

 I'm a completely new newbie in the clojure sphere - old dog in Java
 and a handful of other languages.
 But Clojure is my first (new) functional language, and I'm very
 excited about it - complements on your word Rich et. al.! :-)

 To learn clojure I've started with the (beta) book Seven Languages in
 Seven Weeks by Bruce Tate (pragprog.com) - great book by the way.
 He's got a section called defrecord and protocols where he's got
 this example:

 (ns tate.compass)
 (defprotocol Compass (direction [c]) (left [c]) (right [c]))

 The problem for me is this: When I run the code I get this error
 message from the REPL:
 java.lang.Exception: Unable to resolve symbol: defprotocol in this
 context (NO_SOURCE_FILE:4)

 I'm using the stable version of clojure 1.1 and clojure contrib 1.1.
 I'm on OSX 10.6.4 and Apple JDK 1.6.0_20-b02-279-10M3065.

 Please help! I'm stuck, and I don't want to procede (for too long)
 until I figure out why I get this error ;)

 Thanks in advance.

 Kind regards,
 Henrik Mohr

 --
 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 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: Nil Coalesce

2010-08-17 Thread Nicolas Oury

 Clojure golf is the most fun golf!

  (defn nil-coalesce [a b]
    (map #(or %1 %2) a (concat b (repeat nil

 Or if you really want to treat nil and false differently:

  (defn nil-coalesce [a b]
    (map #(if (nil? %1) %2 %1) a (concat b (repeat nil



I am not sure to get it.
Won't map advance in  parrallel in the sequences, jumping over the
values in b where there is something in a?

-- 
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: What is reference?

2010-08-16 Thread Nicolas Oury
It's a clever box containing a value.
You can open the box and read the current value.
But you can't modify the content of the box directly.

You need to change the value in a box in a transaction.
The cleverness of refs comes form the fact that all read and all write
in a transaction are consistent with each other
independently of other threads reading and writing.

For a better and full explanation, as Wilson tells :
http://clojure.org/concurrent_programming




On Mon, Aug 16, 2010 at 4:32 AM, Wilson MacGyver wmacgy...@gmail.com wrote:
 It's clojure's STM(Software Transaction Memory). More info at 
 http://clojure.org/concurrent_programming

 On Aug 15, 2010, at 11:26 PM, HB hubaghd...@gmail.com wrote:

 Hey,
 I don't understand what references are.
 (ref #{})
 This creates a reference to an empty set but what is reference any
 way?
 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 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: Game development in Clojure

2010-08-16 Thread Nicolas Oury
On Mon, Aug 16, 2010 at 10:51 AM, Martin DeMello
martindeme...@gmail.com wrote:
 Sometimes there's simply no way around it. For instance, I recently
 had some python code that (stripped to its simplest form) had two
 classes, Document and Attachment, where Attachment was a specialised
 subclass of Document, but Document had a list of Attachments as one of
 its members. There was no way around packing the Document and
 Attachment classes into one big file, simply because I couldn't do
 circular module dependencies (python files are modules), but any
 refactoring of the code to remove the circular dependency left it
 *more* complicated than before.


In Clojure, a protocol Attachable, could have been made explaining
what are the things you need for an attachment.

-- 
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: Protocols and default method implementations

2010-08-16 Thread Nicolas Oury
On Mon, Aug 16, 2010 at 3:29 AM, Matthew Phillips mattp...@gmail.com wrote:
 ;; Now, if I want any node's in-edges, I can't just call in-edges
 ;; because Node implementations won't have it, so I compute them in
 ;; that case.
 (defn node-in-edges [n]
  (if (isa? Node2)
    (in-edges n)
    (compute-node-in-edges n)))


isa? should be satisfies?, I think.

-- 
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: What is reference?

2010-08-16 Thread Nicolas Oury
In Common LISP, you can modify anything, anywhere.

In the ML family of language, there are  refs, but, if they have the
same name, they do not share the concept. They are closer of
Clojure's atoms. There is no notion of transactions.

Haskell and a few other languages have Software Transactional Memories
and so construct like refs.

That's one of the (many many) great things in Clojure: a very complete
and consistant handling of concurency, with different constructs for
different kind of concurrency states.

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


Bug of instance? when called in deftype

2010-08-15 Thread Nicolas Oury
Dear all,

I spotted something than I thought was unusual in 1.2-RC2 (haven't try RC3 yet)


(defprotocol Test (test [x]))


(deftype Foo [] Test (test [x] (instance? Foo x)))


(test (Foo.)) = false

(instance? Foo (Foo.)) = true


Should I submit or I am missing something obvious?

It is never a show-stopper because you can write (declare foo?) before
(deftype Foo...

But it is sure surprising when you are relying on = to work.

Best,

Nicolas.

-- 
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: Protocols and default method implementations

2010-08-14 Thread Nicolas Oury
On Sat, Aug 14, 2010 at 5:32 AM, Matthew Phillips mattp...@gmail.com wrote:

 One idea that I tried was to use extend-type on a protocol, say to
 extend any Node to be a PrettyPrintableNode. Obviously this didn't
 work, and I'm not sure it actually makes semantic sense, but it's
 interesting that was my intuitive action.

I played with that kind of things a bit and come up with a - very
alpha - library some time ago.

I called it type classes because it does a part of what Haskell's type
classes do.

You can define a rule:

Node = PrettyPrintableNode (implementation of PrettyPrintableNode using Node)

And it extends Object with a default implementatiuon of protocol
PrettyPrintable, that just takes the object it is called on,
looks at its type, and try to apply the rules it can apply to it (You
could have a rule NodeV2-25 = PrettyFormatable and another
PrettyFormatable, ... = PrettyPrintable).
If it finds a non-cyclic path to construct a PrettyPrintable instance,
it extend the types with the right protocols implementation, and
recalls the function.

I planned to improve on it, but it seems there were a really low
interest for this kind of thing on the list when I posted.

I wouldn't recommand to use it in this state, but if you want to have
a look at the code:

https://nicolasoury.repositoryhosting.com/trac/nicolasoury_type-classes


Best,

Nicolas.

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


What is the good way of def-ing during macro expansion

2010-08-13 Thread Nicolas Oury
Dear all,

In my project, I need (in order to share constant trees in rule
definitions), to define new variable during macro expansion.

What I do currently:
I keep all the constant definitions and the rules definitions in an
atom and at the end, I call a macro at top level that define
everything, in the dependency order.

I don't like it very much, because it's ugly and because it forbid to
create constants in a normal macro.

Is there an idiomatic way to do that? Would the direct manipulation of
namespaces in the macro be better?

I mainly need it to work for compile-on-load, but if possible I would
like a solution that works too for aot.

 Best,

Nicolas.

-- 
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: More urgency for CinC CLR given Oracle's lawsuit against Google?

2010-08-13 Thread Nicolas Oury
I believe they do not sue over the JVM but over Dalvik.
The OpenJDK is,I think, a bit protected frompatents by its license and
the fact t has been distributed by the patents' owner.

However, Clojure in Clojure and better support of other platforms
would be great.

On Fri, Aug 13, 2010 at 12:13 PM, Seth seth.schroe...@gmail.com wrote:
 Given Oracle's lawsuit against Google for its use of the JVM, is
 anyone else suddenly much more concerned about the states of Clojure
 in Clojure and CLR compatibility? I know the former is an important
 goal and also that the existence of the latter is due to heroic
 volunteer efforts on behalf of a small number of people. Frankly I've
 been sitting on the sidelines cheering the efforts on.

 But now I'm much more concerned about writing Clojure code that can
 only run as Oracle sees fit. I've got a small bit of code which needs
 OpenJDK on an Linux Amazon EC2 instance. What will the Oracle scry of
 that?

 If it sounds like I'm stirring up FUD I apologize, it's not my intent.
 Oracle has its fiduciary responsibilities and the patent system is
 what it (sigh) is.

 Here's my ideal option: a production quality release of Clojure
 targeting the CLR before the first anniversary of SCOracle day
 (2010-08-12). This would have to be an organized effort since big
 meaty chunks like CinC are probably within scope of only a few
 Clojurians (i.e. not me).

 Just my personal opinion -- questions, comments, and corrections are
 welcome.

 Seth

 --
 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 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: Protocols and default method implementations

2010-08-13 Thread Nicolas Oury
On Fri, Aug 13, 2010 at 1:17 PM, Rich Hickey richhic...@gmail.com wrote:
 not. They *sometimes* use interfaces in the implementation, but not always.
 At no point should one assume that because a type supports a protocol it
 'isA' that protocol (or its interface).


That's very interesting. From a performance point of view, is there a
penalty involved in being in a case
where the implementation does not use interfaces?

-- 
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: Protocols and default method implementations

2010-08-13 Thread Nicolas Oury
On Fri, Aug 13, 2010 at 2:46 PM, Meikel Brandmeyer m...@kotka.de wrote:
 Using extend is slower, but more dynamic (read: mix-in via map merge
 vs. hard-wiring things with inline definition).



Interesting...
Is there a link explaining how it is implemented in the extend
situation and how much slower it is?

Cheers,

Nicolas

-- 
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: What is the good way of def-ing during macro expansion

2010-08-13 Thread Nicolas Oury
No problem Evan,

(def constants (atom ()))
(defn add-constant [name x]
 (let [sym (gensym name)]
   (reset! constants (cons ([sym x]) @constants)
sym
))

Then when I produce code, I call add-constant and use the returned symbol.
(I only use it to produce new constants, which is the annoying part.
However, constants can be function)

At the end I call
(defmacro emit-all []
 `(do ~@(reduce (fn [l [sym definition] ] (cons `(def ~sym
~definition) l) () @constants )
   


That's ugly and I would like to find better.

Nicolas.

-- 
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: Game development in Clojure

2010-08-13 Thread Nicolas Oury
On Fri, Aug 13, 2010 at 2:51 PM, Mike Anderson
mike.r.anderson...@gmail.com wrote:
 2. It would be great to reduce the amount of memory allocations. Yes,
 I know memory is plentiful and GC is very cheap, but it's still not as
 cheap as stack allocation and any noticeable GC pauses are not good
 for the player experience in interactive games. For this reason, I
 find myself using reduce and indexed loops a lot more than I guess
 would normally be idiomatic, and conversely tend to avoid some of the
 lazy constructs and functions that generate sequences. While Clojure
 is great for a strategy game, I'd probably hesitate to use it for a
 real-time 3D game.


This can be made a bit better by turning Escape Analysis on? Have you
tried that?

The G1 collector is supposed to have lower latency. Have you tried it?

http://research.sun.com/jtech/pubs/04-g1-paper-ismm.pdf

-- 
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: Protocols and default method implementations

2010-08-12 Thread Nicolas Oury
I have been thinking of that too.

The approach of extending with maps is great but lacks of access to
the content of the instance variables of the type.


(def Foo [bar]

)

(extend
  clever-automatic-construction
)

As far as I know, clever-automatic-construction cannot use bar in its
implementation.
Which might be a bit annoying, because you have to rely on the
interface of the implementor of one thing
to implement another interface.
Maybe creating a getBar automatically could help?

Or there are other ways to work around that?

Best,
Nicolas.

-- 
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: Protocols and default method implementations

2010-08-12 Thread Nicolas Oury
Oh. I forgot.

It could also be helpful to have an easy access (meta information, for
example) to the
fields (and their types) of a record type.

On Thu, Aug 12, 2010 at 7:57 PM, Nicolas Oury nicolas.o...@gmail.com wrote:
 I have been thinking of that too.

 The approach of extending with maps is great but lacks of access to
 the content of the instance variables of the type.


 (def Foo [bar]

 )

 (extend
  clever-automatic-construction
 )

 As far as I know, clever-automatic-construction cannot use bar in its
 implementation.
 Which might be a bit annoying, because you have to rely on the
 interface of the implementor of one thing
 to implement another interface.
 Maybe creating a getBar automatically could help?

 Or there are other ways to work around that?

 Best,
 Nicolas.


-- 
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: Protocols and default method implementations

2010-08-12 Thread Nicolas Oury
Can I erase my last mails?

I just discovered (.bar (Foo. 5)) = 5

It is a bit embarrassing. Sorry about that. (I am not a Java person, I reckon)

As far as traits are concerned, I think they can be made with a couple
of macros.
I might try to have a go over the week-end.

Best,

Nicolas.

-- 
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: Please help! Really simple function not working.

2010-08-10 Thread Nicolas Oury
On Tue, Aug 10, 2010 at 2:40 AM, Mark Engelberg
mark.engelb...@gmail.com wrote:
 arbitrary-sized lists is a primary goal.  The posted code (minus the
 call to recur), is an elegant recursive formulation that is idiomatic
 in Scheme because Scheme is (typically) implemented in a way that
 makes stack limitations (mostly) a non-issue.

 In Clojure, you have to work around stack limitations imposed by Java,
 and recursive functions must be translated to work on largish lists
 in Clojure.  There is no one recipe to make that translation, but
 there are several common patterns.  If the recursive call is in tail

I want to add a little bit of information. (I am not a specialist of
Scheme but believe what follow to be true)
The original function minus recur + a recursive call on the  second
cond is not tail-recursive.
 So, in this particular case, Scheme does not warranty no exhaustion
of resources.
(A lot of Scheme implementation, and SML/NJ I believe, are
CPS-compiled and will exhaust heap and not stack
but those that are based on Lazy Stack copying would probably exhaust stack).
Anyway, this implementation - in any language - would still eat
memory, constructing a long stack-allocated (or heap-allocated in most
Scheme and SML/NJ) list of continuations to remember what to do when
I have finished. In more fancy term, whether on the stack and on the
heap,
you need to keep a list of continuations to the computation.
When I am at the end of the list, I will add one, and then add one,
and then add one 

A tail-call has the particularity to have a trivial continuation: I
take the result and return it, so you don't have to actually keep any
information.
Most functional languages optimise this, but those on the JVM have a
hard time doing it, because the way the JVM works.
Scheme warranties it. You are not a Scheme implementation if you do
not do tail-call optimisation.

Clojure  does it, for self recursion, when you explicitly ask for it,
emitting a compile-time error if it can't.
I think it is a good idea, because sometimes you write the function
lightly badly and do not realise tail-call optimisation do not happen
until a bug in production.

There are ways to transform a call in tail-call. Adding an
accumulator, using a list passed as argument as a stack, or doing a
Continuation passing Style transformation.
(http://en.wikipedia.org/wiki/Continuation-passing_style , in a word:
to put the future in a closure we pass to the recursive call.)
It is harder to transform a program to only self tail-call, but I
think CPS+trampoline always do the trick.
(Without the restriction to only do self calls, CPS does always the trick)

Best regards,

Nicolas.

-- 
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: Basic Lisp Compiler: How to tell which functions to compile?

2010-08-10 Thread Nicolas Oury
On Tue, Aug 10, 2010 at 12:43 AM, Jules julesjac...@gmail.com wrote:
 It is impossible (undecidable) to tell precisely which functions a
 function will call. Therefore you will need to consider not exactly
 set of functions that a function will call, but some superset of that.
 Why not take as your superset all functions? That is, always compile
 all functions.


There is a smaller decidable superset: all functions occurring in the
static call graph for a function.
But, I agree, most compilers would just compile everything and it's
probably a better idea.

-- 
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: Please help! Really simple function not working.

2010-08-10 Thread Nicolas Oury
On Tue, Aug 10, 2010 at 9:55 AM, Mark Engelberg
mark.engelb...@gmail.com wrote:
  In Clojure, I find stack limitations are a real issue unless
 I transform the algorithms into a tail-recursive accumulator style.
 For lists, it's usually not hard.  For trees, it can be a bit of a
 pain.

Try CPS+trampoline
It is more or less what scheme does internally...

-- 
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: Please help! Really simple function not working.

2010-08-10 Thread Nicolas Oury
On Tue, Aug 10, 2010 at 10:08 AM, Laurent PETIT laurent.pe...@gmail.com wrote:
 Naive question from someone who has not really used Scheme in practice :
 beyond the memory footprint problem (which may or may not be a problem
 depending on the memory size of an element in the initial list, and also
 depending on whether you're recurring over a datastructure, or a presumably
 very long lazy seq), isn't there an impact on CPU usage too ?

It would probably be up to twice as slow, I would say.
For a list that is continuous in memory and continuations that are
allocated perfectly in memory,
you would need to go through twice the same amount of memory.
(I assume that the main cost here is going through the memory)

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


  1   2   3   >