Re: Language similarities

2010-01-01 Thread mbrodersen
Paul Graham might be correct that main stream languages will
incorporate features that Lisp had many years ago but the result won't
be Lisp.

-- 
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: Language similarities

2010-01-01 Thread mbrodersen
Everything looks the same if you use a loose enough equality function
I guess.

Using a loose enough equality function, you can argue that Clojure is
just Lisp running on top of a mutable, Object Oriented framework (the
JVM). Immutable data structures and controlled changes of immutable
values is nothing new.

Make the equality function even more loose and you can argue that
Clojure and C# is pretty much the same. Both languages JIT compiles to
machine code, are executed by a CPU and modify states (memory,
magnetic plates, the temperature of the PC etc.)

However, in reality the "small differences" makes a big difference for
people using the languages. I personally feel that it makes a big
difference using C# instead of C or Clojure instead of Lisp.

-- 
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 OOPS concepts to Clojure? Doable or Wrong thinking?

2009-12-14 Thread mbrodersen
"an abuse of macros?"

HAHAHA you are funny Laurent :-)

On Dec 13, 10:32 pm, Laurent PETIT  wrote:
> Unless I've missed something implicit in your post, why do present us (and
> particularly ajay, who's trying to understand the language) what I would
> consider an abuse of macro, where regular functions would do the job very
> well ?
>
> 2009/12/13 mbrodersen 
>
> > There is nothing "wrong" about objects in Clojure as long as the
> > objects are immutable.
>
> > Some people tend to think that "functional" is anti-"object oriented".
> > That is IMHO the wrong way to think. They complement each other. Why
> > limit the tools in your toolbox? F# (for example) shows how it can be
> > done elegantly.
>
> > Here is one (quick and dirty done in 5 min) way of using objects in
> > Clojure that (again IMHO) is fully compatible with the "Clojure style"
> > of programming because the objects (maps) are immutable:
>
> > (defmacro ? [object value]
> >        "Get an object value - same as object.value in OO but immutable"
> >        `(get ~object (keyword (str '~value
>
> > (defmacro ! [object method & args]
> >        "Call an object method - same as object.foo(); in C# but immutable"
> >        `((get ~object (keyword (str '~method))) ~object ~...@args))
>
> > (defmacro != [object name value]
> >        "Set an object value - same as object.value = foo; in OO but
> > immutable"
> >        `(assoc ~object (keyword (str '~name)) ~value))
>
> > (defmacro !fn [object name fn & args]
> >        "Execute a function on an object value and sets the object value to
> > the result - roughly same as using mutable object but done in an
> > immutable style"
> >        `(assoc ~object (keyword (str '~name)) (~fn (get ~object (keyword
> > (str '~name))) ~...@args)))
>
> > --
> > 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: Mapping OOPS concepts to Clojure? Doable or Wrong thinking?

2009-12-12 Thread mbrodersen
Cleaned it up a bit:

(defmacro ? [object value]
"Get an object value - same as object.value; in C# but immutable"
`(get ~object (keyword (str '~value

(defmacro ! [object method & args]
"Call an object method - same as object.method(args); in C# but
immutable"
`((get ~object (keyword (str '~method))) ~object ~...@args))

(defmacro != [object name value]
"Set an object value - same as object.name = value; in C# but
immutable"
`(assoc ~object (keyword (str '~name)) ~value))

-- 
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 OOPS concepts to Clojure? Doable or Wrong thinking?

2009-12-12 Thread mbrodersen
There is nothing "wrong" about objects in Clojure as long as the
objects are immutable.

Some people tend to think that "functional" is anti-"object oriented".
That is IMHO the wrong way to think. They complement each other. Why
limit the tools in your toolbox? F# (for example) shows how it can be
done elegantly.

Here is one (quick and dirty done in 5 min) way of using objects in
Clojure that (again IMHO) is fully compatible with the "Clojure style"
of programming because the objects (maps) are immutable:

(defmacro ? [object value]
"Get an object value - same as object.value in OO but immutable"
`(get ~object (keyword (str '~value

(defmacro ! [object method & args]
"Call an object method - same as object.foo(); in C# but immutable"
`((get ~object (keyword (str '~method))) ~object ~...@args))

(defmacro != [object name value]
"Set an object value - same as object.value = foo; in OO but
immutable"
`(assoc ~object (keyword (str '~name)) ~value))

(defmacro !fn [object name fn & args]
"Execute a function on an object value and sets the object value to
the result - roughly same as using mutable object but done in an
immutable style"
`(assoc ~object (keyword (str '~name)) (~fn (get ~object (keyword
(str '~name))) ~...@args)))

-- 
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: roll call of production use?

2009-11-23 Thread mbrodersen
I use an internal DSL (Domain Specific Language)  in Clojure to
generate C++ and C# code.

Cheers
Morten

On Nov 24, 10:00 am, Raoul Duke  wrote:
> hi,
>
> i'd be interested to hear who has successfully used clojure in
> production. i know of some, as some folks have been vocal; any other
> interesting-but-so-far-silent uses people'd be willing to fess up
> about?
>
> many thanks.

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: Understanding Clojure Closures

2009-11-11 Thread mbrodersen
Thanks Howard. Another great answer.

Morten

On Nov 12, 2:58 am, Howard Lewis Ship  wrote:
> Symbols are late resolved to functions.
>
> (def t (fn ...)) means define a Var bound to symbol t, and store the
> function in it. In JVM terms, the function becomes a new class that is
> instantiated.
>
> (t (dec x)) means locate the Var bound to symbol t -- at execution
> time (not compilation time) --- de-reference the function object
> stored there and invoke it.
>
> In fact, t can be temporarily rebound (using the binding macro) or
> even completely replaced (using alter-var-root!) at any time. The Var
> remains the same but the contents of the Var are changed.
>
> Because of how Clojure is structured, the Var object need only be
> resolved from the symbol once (in the generated Java bytecode, the Var
> appears as a static final field).
>
>
>
> On Wed, Nov 11, 2009 at 5:17 AM, mbrodersen  
> wrote:
> > A quick question about how closures work in Clojure.
>
> > In this simple recursive expression:
>
> > (def t (fn [x] (if (zero? x) 0 (+ x (t (dec x))
>
> > The fn special form is evaluated within a context where t is not yet
> > bound.
>
> > t is only bound AFTER fn has captured its environment.
>
> > In other words, the closure captured by fn doesn't know anything about
> > t (or maybe only that t is unbound).
>
> > And yet it still works if I call t:
>
> > (t 5) => 15
>
> > My question is how Clojure ensures that t is bound to the closure
> > after the closure has already been captured?
>
> > Thanks
> > Morten
>
> > --
> > 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
>
> --
> Howard M. Lewis Ship
>
> Creator of Apache Tapestry
>
> The source for Tapestry training, mentoring and support. Contact me to
> learn how I can get you up and productive in Tapestry fast!
>
> (971) 678-5210http://howardlewisship.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


Re: Understanding Clojure Closures

2009-11-11 Thread mbrodersen
Great answer Alex. Thanks!

Morten

On Nov 12, 12:34 am, Alex Osborne  wrote:
> mbrodersen wrote:
> > In this simple recursive expression:
>
> > (def t (fn [x] (if (zero? x) 0 (+ x (t (dec x))
>
> > The fn special form is evaluated within a context where t is not yet
> > bound.
>
> > t is only bound AFTER fn has captured its environment.
>
> > In other words, the closure captured by fn doesn't know anything about
> > t (or maybe only that t is unbound).
>
> > And yet it still works if I call t:
>
> > (t 5) => 15
>
> > My question is how Clojure ensures that t is bound to the closure
> > after the closure has already been captured?
>
> t is a var.  The var is created before the body of def is evaluated.  At
> that point it is unbound.  The function closes over the var not the
> value of the binding.  That's why you can re-evaluate a function,
> changing it's behaviour, without having to re-evaluate everything that
> calls it.
>
> You'll notice that with locals (which aren't vars, they're immutable) it
> doesn't work:
>
> (let [t (fn [x] (if (zero? x) 0 (+ x (t (dec x)] (t 2))
>
> Also if you try to use the var in the bindings you'll get an unbound
> error, not an unable to resolve symbol error:
>
> (def foo foo)

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


Understanding Clojure Closures

2009-11-11 Thread mbrodersen
A quick question about how closures work in Clojure.

In this simple recursive expression:

(def t (fn [x] (if (zero? x) 0 (+ x (t (dec x))

The fn special form is evaluated within a context where t is not yet
bound.

t is only bound AFTER fn has captured its environment.

In other words, the closure captured by fn doesn't know anything about
t (or maybe only that t is unbound).

And yet it still works if I call t:

(t 5) => 15

My question is how Clojure ensures that t is bound to the closure
after the closure has already been captured?

Thanks
Morten

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


(into) improvement

2009-11-03 Thread mbrodersen

Currently (into) takes 2 parameters (destination and source):

(into [] [1 2]) => [1 2]

but not more than 2:

(into [] [1 2] [3 4]) => Wrong number of args...

It would be nice to have (into) work for more parameters:

(into [] [1 2] [3 4] [5 6 7]) => [1 2 3 4 5 6 7]

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



(info) improvement?

2009-11-03 Thread mbrodersen

Currently (into) takes 2 parameters (destination and source)

(into [] [1 2]) => [1 2]

but not more than 2:

(into [] [1 2] [3 4]) => Wrong number of args...

It would be nice to have (into) work for more parameters:

(into [] [1 2] [3 4]) => [1 2 3 4]

--~--~-~--~~~---~--~~
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: Dedicated thread for agent or creating thread pool for agent?

2009-10-19 Thread mbrodersen

Good point about the thread pools.

I would have preferred to do it in a more platform neutral way but yep
that looks like a good solution :-)

Thanks Christophe!

--~--~-~--~~~---~--~~
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: Printing to *out* in another thread

2009-10-18 Thread mbrodersen

> Using atoms is not a good idea. Unless you don't mind if the same
> message is sometimes printed more than once. Atoms are implemented
> using spin locks with automatic retry.

Hmmm...unless add-watch => observer is called only once.

So I might be wrong :-)

Interesting. Anybody knows more about this?

Thanks
Morten

--~--~-~--~~~---~--~~
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: Printing to *out* in another thread

2009-10-18 Thread mbrodersen

Using atoms is not a good idea. Unless you don't mind if the same
message is sometimes printed more than once. Atoms are implemented
using spin locks with automatic retry.

Cheers
Morten

--~--~-~--~~~---~--~~
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: Dedicated thread for agent or creating thread pool for agent?

2009-10-18 Thread mbrodersen

> SWT's equivalent to Swing's SwingUtilities.invokeLater is
> Display.asyncExec; the equivalent of SwingUtilities.invokeAndWait is
> Display.syncExec (all four take a Runnable as an argument).

Thanks pmf! That at least solves the immediate problem.

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
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: Printing to *out* in another thread

2009-10-17 Thread mbrodersen

Hmmm...it works with the normal Clojure REPL. So it must be an Emacs
thingy.
--~--~-~--~~~---~--~~
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: Dedicated thread for agent or creating thread pool for agent?

2009-10-17 Thread mbrodersen

I don't know SWT well enough to answer that. I am new to the JVM
platform (after 20+ years of writing native C++ code).

However, the question is not SWT specific. There will be other cases
(for example OpenGL) where something like InvokeLater doesn't exist.

Cheers
Morten

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



Dedicated thread for agent or creating thread pool for agent?

2009-10-17 Thread mbrodersen

Hi all,

Is there a way to ensure that only one dedicated thread is used for an
agent?

The same question was asked some time ago but I don't think it was
resolved.

My specific problem is that SWT throws an exception if I use different
threads to call it even though I am using an agent to avoid race
conditions.

I can of course avoid the problem by only callling SWT from the main
thread. However there will be situations where you want to use 2 or
more components with the same constraint.

The other solution of course is to start an old style java thread to
handle SWT and coordinate via Ref's. The problem is that it means
mixing two ways to handle concurrency in the same application (the
agent way and the old-style-thread way) which adds accidental
complexity to the application.

An ideal solution might be to be able to create a thread pool for an
agent. You can then specify the max size of the pool. Specifying the
thread pool size to be 1 will fix the problem.

Any ideas?

Thanks
Morten

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

2009-10-17 Thread mbrodersen

It would be great to have while-let in contrib. Then I don't have to
maintain let-while myself :-)

Morten

--~--~-~--~~~---~--~~
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: Printing to *out* in another thread

2009-10-17 Thread mbrodersen

If you want to print to stdout from multiple threads without getting
the printing garbeled you can do something like the following (it also
logs all printed values):

(def wa-debug-agent)

(defn wa-debug-make []
(def wa-debug-agent (agent [])))

(defn wa-debug-print
"This makes it possible to print from multiple threads without
overlapping each other"
[& args]
(send wa-debug-agent
(fn [list v]
(apply println v)
(conj list v)) args))

Cheers
Morten

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

2009-10-17 Thread mbrodersen

That's a nice improvement Christophe. Exactly the kind of answer I was
looking for.

Thanks!
Morten

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

2009-10-17 Thread mbrodersen

> I don't think the multiple evaluation matters in this case, since it's
> the name parameter, which will in all sane cases be a simple symbol.
> gensyms are needed when multiple evaluation could cause side-effects
> or an expensive function to be computed multiple times, which is not
> the case here.

Yes that is correct Jarkko. The name parameter will only be evaluated
as needed and it serves the same purpose as x in the following:

(doseq [x [1 2 3]] (println x))

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

2009-10-17 Thread mbrodersen

I think I understand what you are trying to do John but it doesn't
have the same semantics though.

If you use the first definition, the following expression:

(let-while [x (if (> (rand) 0.2) "Yep!" nil)] (println x) (println
(str x

will work correctly (it will print "Yep!" and "Yep!Yep!" zero or more
times when called).

If you use the 2nd macro definition, the x value will not be bound to
the result of the (if ...) expression => (println x) will trigger a
"Unable to resolve symbol: x" error.

--~--~-~--~~~---~--~~
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 is two!

2009-10-16 Thread mbrodersen

Congratulations everybody.

Clojure is the answer.

On Oct 17, 3:12 am, Rich Hickey  wrote:
> http://clojure.blogspot.com/2009/10/clojure-is-two.html
>
> Thanks again to all!
>
> Rich
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



let-while

2009-10-16 Thread mbrodersen

Hi,

I am new to Clojure and I am wondering if there is anything similar to
the following macro already built in:

(defmacro let-while
"Makes it easy to continue processing an expression as long as it is
true"
[[name expr] & forms]
`(loop []
(let [~name ~expr]
(when ~name
~...@forms
(recur)

I find it really useful when (for example) reading from a non-blocking
socket. Here is a quick (silly) example:

(let-while [x (if (> (rand) 0.2) "Yep!" nil)]
(println x)
(println (str x)))

The idea is that x is bound to the if expression and if x is true the
println's are executed. Repeat until x is false/nil.

Thanks
Morten

--~--~-~--~~~---~--~~
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: Runtime exception when agent calls sort

2009-01-19 Thread mbrodersen

Thanks Steve. The code is called by another function that handles the
agent state so no it is not called directly. Here is the additional
code (it is a simple Newbie web server learning project):

Here is how the web server is run:

(ws-run 3000 wh-handler)

The web server:
===

(import '(java.io BufferedReader InputStreamReader PrintWriter))

;***;

(defn reader-make-from-socket [socket]
(BufferedReader. (InputStreamReader. (.getInputStream socket

;***;

(def lines-read-max 100)

(defn lines-read [reader lines]
(let [line (.readLine reader)]
(cond
(= line nil)
[]
(= line "") 
lines
(> (count lines) lines-read-max)lines
true
(lines-read reader (concat lines [line])

;***;

(defn http-line-to-arg [cmd line]
(let [key (first (re-seq #"\S+:" line))]
(let [value (apply str (drop (count key) line))]
(conj cmd {(apply str (drop-last key)) (apply str (drop 1
value))}

(defn http-line-to-url [line]
(let [[cmd url http] (re-seq #"\S+" line)]
{"Cmd" cmd "Url" url "Http" http}))

;***;

(defn cmd-make-from-http-lines2 [cmd lines]
(if (= (first lines) nil)
cmd
(cmd-make-from-http-lines2 (http-line-to-arg cmd (first lines))
(rest lines

(defn cmd-make-from-http-lines [lines]
(if (= (first lines) nil)
{}
(cmd-make-from-http-lines2 (http-line-to-url (first lines)) 
(rest
lines

(defn cmd-print [cmd]
(let [ks (sort (keys cmd))]
(println "Cmd:")
(doseq [k ks]
(println (format "  \"%s\" => \"%s\"" k (cmd k))

(defn cmd-read [socket]
(let [reader (reader-make-from-socket socket) lines (lines-read
reader []) cmd (cmd-make-from-http-lines lines)]
(cmd-print cmd)
(conj cmd {:socket socket})))

;***;

(defn ws-agent [agentCounter socket fn]
(let [cmd (cmd-read socket)]
(fn cmd)
(.close socket)
(+ agentCounter 1)))

(defn ws-accept [socket tasks fn]
(println (format "Server accepting (clients done %d)" (deref tasks)))
(let [client (.accept socket)]
(send-off tasks ws-agent client fn)))

(defn ws-run [port fn]
(let [socket (java.net.ServerSocket. port) tasks (agent 0)]
(dotimes [i 3] (ws-accept socket tasks fn))
(await tasks)
(println (format "Server done (%d clients)" (deref tasks)))
(.close socket)))

The web handler


(load-file "Web Server.clj")

;***;

(defn http-write-ok [out]
(.write out "HTTP/1.1 200 OK\r\n\r\n"))

;***;

(defn html-write-head [out]
(.write out ""))

(defn html-write-tail [out]
(.write out ""))

; THIS IS WHERE IT GOES WRONG IF I CHANGE (keys cmd) to (sort (keys
cmd)):

(defn html-write-cmd [out cmd]
(doseq [key (keys cmd)]
(.write out
(format "\"%s\" => \"%s\"" key (cmd key)

(defn html-write-test [out cmd]
(http-write-ok out)
(html-write-head out)
(.write out "Hello from Clojure!")
(.write out (str "" (System/currentTimeMillis) ""))
(html-write-cmd out cmd)
(html-write-tail out))

;***;

(defn wh-make-writer [socket]
(PrintWriter. (.getOutputStream socket)))

(defn wh-write-test [cmd]
(let [out (wh-make-writer (cmd :socket))]
(html-write-test out cmd)
(.flush out)))

;***;

(defn wh-handler [cmd]
(wh-write-test cmd))

;***;

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To post to this group, send email to clojure@googlegroups.com
To unsubscribe from this group, send email to 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



Runtime exception when agent calls sort

2009-01-18 Thread mbrodersen

Hi

The following agent code works fine:

(defn html-write-cmd [out cmd]
(doseq [key (keys cmd)]
(.write out
(format "\"%s\" => \"%s\"" key (cmd key)

but if I change (keys cmd) to (sort (keys cmd)) then the following
runtime exception is triggered:

java.lang.RuntimeException: Agent has errors (NO_SOURCE_FILE:0)

The (sort (keys cmd)) works fine outside the agent.

Any idea what it can be?

Thanks
Morten

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To post to this group, send email to clojure@googlegroups.com
To unsubscribe from this group, send email to 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



Re: Newbie problem

2009-01-17 Thread mbrodersen

Yes that does indeed fix the problem :-)

Thanks Shawn!

Cheers
Morten

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To post to this group, send email to clojure@googlegroups.com
To unsubscribe from this group, send email to 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



Newbie problem

2009-01-17 Thread mbrodersen

Hi

I am having fun learning Clojure but have a problem with the following
code. If you run the code with the ;OK removed then it works. If you
run the code with ;ERROR removed then it doesn't.

The code is my own implementation of splitting a string into
individual words (just a learning exercise). The error message is:

java.lang.IllegalArgumentException: No matching method found:
isWhitespace (error.clj:0)

Which is strange because the ;ERROR line has nothing to do with
isWhitespace?

Any help would be appreciated.

Thanks
Morten
-

(defn line-skip-ws [line]
(cond
(not (first line))  
""
(Character/isWhitespace (first line))   (line-skip-ws (rest 
line))
true
(apply str line)))

(defn line-split-1 [word line]
(let [c (first line)]
(println "line-split-1" "word:" word "line:" line "c:" c
"class:" (class c))
(cond
(not c) [word 
""]
(Character/isWhitespace c)  [word (apply str line)]
true
(line-split-1 (str word c) (rest line)

(defn line-split [line]
(let [split (line-split-1 "" (line-skip-ws line))]
(if (= (first split) "")
[""]
;ERROR  (concat [(first split)] (line-split (rest split))
;OK (concat [(first split)] []

(doseq [x (line-split "  Hello   world!  ")] (println (format "\"%s\""
x)))

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To post to this group, send email to clojure@googlegroups.com
To unsubscribe from this group, send email to 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---