Re: Macro newbie: Trying to create a map template

2009-10-23 Thread John Harrop
On Sat, Oct 24, 2009 at 12:19 AM, samppi  wrote:

> user=> (defmacro b [form]
>  (let [processed-form (a form rec#)]
>`(fn [rec#] processed-form)))
> java.lang.Exception: Unable to resolve symbol: rec# in this context
> (NO_SOURCE_FILE:39)


Try

(defmacro b [form]
  (let [r (gensym)
processed-form (a form r)]
   `(fn [~r] ~processed-form)))

You need to replace rec# with a *reference* to a symbol. If you use a
literal symbol (including a literal gensym) it will try to take its value
even though you haven't assigned it yet. When you called a manually you
passed it a quoted symbol, so a symbol rather than the result of
dereferencing one. To get a symbol as the second argument to a in the macro
you need a pointer TO a gensym, which the first binding in my version of the
let gets you, in r.

You also need to unquote the expressions that should be filled in from the
let bindings. Otherwise you'd get (fn [r] processed-form) instead of (fn
[G_1746] (:a G_1746)) or whatever you actually want.

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



Macro newbie: Trying to create a map template

2009-10-23 Thread samppi

I'm trying to create a macro called "procedure" so that:
  (procedure (and _x (pos? _y)))
expands to
  (fn [rec#] (and (:x rec#) (pos? (:y rec#

I'm stuck because I can't figure out a way to generate a symbol
outside. I'm a newbie at macros, so I don't know if there's a better
way around this:

Clojure 1.0.0-
user=> (require '[clojure.zip :as zip])
nil
user=> (defn a [form rec-symbol]
  (loop [loc (zip/seq-zip form)]
  (if (zip/end? loc)
(zip/root loc)
(recur
  (zip/next
(let [node (zip/node loc)]
  (if (and (symbol? node) (-> node name first (= \_)))
(zip/replace loc (list (-> node name (subs 1) keyword)
'rec))
loc)))
#'user/a
user=> (a '(_a) 'rec)
((:a rec))
user=> (defmacro b [form]
  (let [processed-form (a form rec#)]
`(fn [rec#] processed-form)))
java.lang.Exception: Unable to resolve symbol: rec# in this context
(NO_SOURCE_FILE:39)

--~--~-~--~~~---~--~~
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 can't :let be first in a for

2009-10-23 Thread Meikel Brandmeyer
Hi,

Am 23.10.2009 um 21:16 schrieb Howard Lewis Ship:

> Here's what I wanted to write:
>
> (defn add-script-links-for-imported-javascript-libraries
>   [env dom-nodes]
>   (extend-dom dom-nodes [:html :head] :top
>   (template-for [:let [aggregation (-> env :cascade :resource- 
> aggregation)
> libraries (@aggregation :libraries)]
>   asset-map libraries
>   :let [path (to-asset-path env asset-map)]]
>   :script { :type "text/javascript" :src path } [ 
> linebreak ])))  

Why don't you just go one step further?

(defn add-script-links-for-imported-javascript-libraries
   [env dom-nodes]
   (extend-dom dom-nodes [:html :head] :top
 (template-for [asset-map (-> env
:cascade
:resource-aggregation
deref
:libraries)
:let [path (to-asset-path env asset-map)]]
   :script { :type "text/javascript" :src path } [ linebreak ])))

> But I had to juggle it to this:
>
> (defn add-script-links-for-imported-javascript-libraries
>   [env dom-nodes]
>   (let [aggregation (-> env :cascade :resource-aggregation)
>  libraries (@aggregation :libraries)]
>   (extend-dom dom-nodes [:html :head] :top
>   (template-for [asset-map libraries
>   :let [path (to-asset-path env 
> asset-map)]]
>   :script { :type "text/javascript" :src path } [ 
> linebreak ] 
>
>
> Of course there are any number of ways to write this, but I prefer the
> first option, as it does less of a job of obscuring what the main
> point of the function is: a call to extend-dom.

I don't see any obfuscation. Especially if you add a docstring "Extend  
DOM with ..." to the function.

Sincerely
Meikel



smime.p7s
Description: S/MIME cryptographic signature


Re: Why can't :let be first in a for

2009-10-23 Thread Chouser

On Fri, Oct 23, 2009 at 3:16 PM, Howard Lewis Ship  wrote:
>
> I like to try and keep my level of nesting under control, and this
> often involves hiding or re-structuring the let macro. The for macro
> can implicitly assemble a let macro for you, but with a limitation
> that the :let clause can't be first:
>
> 1:5 user=> (for [:let [z [:foo :bar]] x z] (name x))
> java.lang.IllegalStateException: Can't pop empty vector (repl-1:5)
> 1:6 user=> (for [x [:foo :bar] :let [z (name x)]] z)
> ("foo" "bar")
> 1:7 user=>
>
> Is this limitation intentional?  Could the error message be improved?

It's not an intentional limitation.  One hint of this is that
:let, :while, and :when all work perfectly fine at the beginning
of a doseq.

Improving the error message would be easy.  On the other hand,
a patch to support them at the beginning of a 'for' should be
possible.  I think it might be worthwhile, particulary for macros
that generate 'for' forms.  The machinery inside a 'for'
expansion is hard to do yourself.

Rich, would you consider a patch to support this?

--Chouser

--~--~-~--~~~---~--~~
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: Scientific computing

2009-10-23 Thread Garth Sheldon-Coulson
Hi Rock,

Clojure isn't designed as an array processing language, so it doesn't have
multidimensional array-slicing or matrix algebra tools built in. That's just
not what Clojure's trying to be, and you're right, immutable data structures
might get in the way. There's probably nothing stopping you writing a
library to treat nested vectors as multidimensional arrays (they have
constant lookup after all), but I don't know if it's worth the trouble
considering the matrix algebra routines aren't there.

Incanter http://incanter.org/ is a project that links Clojure to Parallel
Colt for matrix algebra, adding nice statistical features a la R.

Clojuratica http://clojuratica.weebly.com/ is a project that links Clojure
to Mathematica, which (as you probably know) is similar to MATLAB in its
array-processing capabilities and has a whole host of other capabilities as
well. I develop Clojuratica and am working on a new release with better
syntactic integration. I anticipate the announcement of that release within
two weeks, but take a look at what on the site now anyway.

If all you need is a statistical or array-processing language like MATLAB,
my frank view is you're best off staying in R, or Mathematica, or MATLAB, or
Octave, or whatever... they're mature and great at what they do (Mathematica
most of all ;-) ). The reason you might want to use Clojuratica or Incanter
is if you're building applications that need Clojure's awesome features in,
say, concurrency, *plus* array processing.

Garth

On Fri, Oct 23, 2009 at 2:01 PM, Rock  wrote:

>
> What if I wanted to use Clojure for scientific computing, and in
> particular for doing linear algebra and matrix computations a la
> MATLAB?
>
> What would my options be for representing matrices, not to mention,
> especially, MULTIDIMENSIONAL ARRAYS?
>
> Would java arrays be the way to go, or nested vectors?
>
> I don't like the nested vector solution too much, but to be honest,
> there really doesn't seem to be an adequate solution to this problem
> at the moment. For instance, once you have a multidimensional array
> (whatever kind it is) you can't, I believe, access any property of
> that object at runtime to know its rank and dimensions, like you can
> do with MATLAB or Scientifc Python (NumPy), or other similar
> languages. And slicing, such as a[2:20:4, 1:9:3], which means every
> element in a with indices that go from start to finish with a certain
> step, seem to be problematic.
>
> Actually the whole buiseness of having to deal with immutable (and
> lazy) data structures appears to make computing with matrices and
> multidimensional arrays a lot more difficult. Maybe this is just an
> impression. Opinions?
>
> Does Rich intend to address this issue of having multidimensional
> arrays (for scientific computing purposes) sooner or later?
>
> Thanks.
>
> Rock
>
> >
>

--~--~-~--~~~---~--~~
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: Scientific computing

2009-10-23 Thread Konrad Hinsen

Rock a écrit :

> What if I wanted to use Clojure for scientific computing, and in
> particular for doing linear algebra and matrix computations a la
> MATLAB?
> 
> What would my options be for representing matrices, not to mention,
> especially, MULTIDIMENSIONAL ARRAYS?
> 
> Would java arrays be the way to go, or nested vectors?

There are a couple of Java libraries that you can use. Unfortunately the 
  efforts to make everyone agree on a single library a few years ago 
have failed, so now you have to choose between a couple of incompatible 
libraries.

For matrix computations and linear algebra, your best choice is probably 
  the Colt library developed at CERN, or the somewhat parallelized 
version called Parallel Colt. Colt has arrays up to three dimensions for 
a couple of data types, enough for matrix stuff but not much more.

If you need higher-dimensional arrays, e.g. to store complex data sets, 
your best bet would be the netCDF implementation for Java, which 
contains a very complete array library that handles any number of 
dimensions. However, it provides no linear algebra at all.

Another library in this category is JAMA. It looks rather similar to 
Colt, but I never looked at it more closely.

There's also an array package developed by IBM in conjunction with an 
optimizing Java compiler to go with it, but apparently it is no longer 
maintained.


> at the moment. For instance, once you have a multidimensional array
> (whatever kind it is) you can't, I believe, access any property of
> that object at runtime to know its rank and dimensions, like you can
> do with MATLAB or Scientifc Python (NumPy), or other similar
> languages. And slicing, such as a[2:20:4, 1:9:3], which means every
> element in a with indices that go from start to finish with a certain
> step, seem to be problematic.

All the libraries listed above provide most of this functionality. Like 
NumPy, they represent an array as a 1D Java array containing the data 
elements plus bookkeeping information about the shape and strides.

> Actually the whole buiseness of having to deal with immutable (and
> lazy) data structures appears to make computing with matrices and
> multidimensional arrays a lot more difficult. Maybe this is just an
> impression. Opinions?

The array packages listed above all permit modification of the array 
elements. Fully immutable arrays are probably of little practical 
interest, but something similar to Clojures transients (data types that 
can be initialized once by the function that creates them and then 
become immutable) would probably be sufficient to change this. There is 
little practial experience with this at the moment.


Konrad.


__ Information provenant d'ESET NOD32 Antivirus, version de la base des 
signatures de virus 4537 (20091023) __

Le message a été vérifié par ESET NOD32 Antivirus.

http://www.eset.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
-~--~~~~--~~--~--~---



Why can't :let be first in a for

2009-10-23 Thread Howard Lewis Ship

I like to try and keep my level of nesting under control, and this
often involves hiding or re-structuring the let macro. The for macro
can implicitly assemble a let macro for you, but with a limitation
that the :let clause can't be first:

1:5 user=> (for [:let [z [:foo :bar]] x z] (name x))
java.lang.IllegalStateException: Can't pop empty vector (repl-1:5)
1:6 user=> (for [x [:foo :bar] :let [z (name x)]] z)
("foo" "bar")
1:7 user=>

Is this limitation intentional?  Could the error message be improved?

Here's what I wanted to write:

(defn add-script-links-for-imported-javascript-libraries
[env dom-nodes]
(extend-dom dom-nodes [:html :head] :top
(template-for [:let [aggregation (-> env :cascade 
:resource-aggregation)
  libraries (@aggregation :libraries)]
asset-map libraries
:let [path (to-asset-path env asset-map)]]
:script { :type "text/javascript" :src path } [ 
linebreak ])))  


(the formatting is probably scrambled)

But I had to juggle it to this:

(defn add-script-links-for-imported-javascript-libraries
[env dom-nodes]
(let [aggregation (-> env :cascade :resource-aggregation)
   libraries (@aggregation :libraries)]
(extend-dom dom-nodes [:html :head] :top
(template-for [asset-map libraries
:let [path (to-asset-path env 
asset-map)]]
:script { :type "text/javascript" :src path } [ 
linebreak ] 


Of course there are any number of ways to write this, but I prefer the
first option, as it does less of a job of obscuring what the main
point of the function is: a call to extend-dom.

-- 
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-5210
http://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: Clojure in a big Java solution

2009-10-23 Thread Luc Prefontaine
If the Java layers are not already thread safe, you are facing a real
problem if you just try to layout some Clojure code on top of them...
You would have to be careful about isolating chunks that are thread safe
then look at what's left.

The difficulty then would be to find a way to introduce Clojure code at
some point where it will be paying off without requiring
months to rewrite the Java code.

If you want to benefit from parallel processing in Clojure your greatest
rewards will be to move away from Java structures but that might be
a huge effort if these data structures are present everywhere in the
Java code that will remain. A mid-approach would be to make
these structures thread-safe first and then share them between Clojure
and Java as a first step and later move away from them or at least
concealed them at the periphery in the java code.

I do not see anything else than a tailored and prudent approach if you
want to avoid a total rewrite.
That tailored approach depends a lot on the structure of the
application...

What's the code size ?

On Fri, 2009-10-23 at 05:41 -0700, vanallan wrote:

> Hi!
> I am currently investigating if it is possible to convert a part of a
> big Java system to Clojure. The reason for this is to make this part
> run in parallel and hence ease the implementation by porting it to
> Clojure. The problem is that these parts of the system is today
> setting and changing a lot mutable data in a lot of different objects.
> 
> Which approach do you think i should have when i try to port these
> Java methods? Should i try to retain the Java structure and change
> these objects or is it better to create my own data structure and
> somehow manage them? Does anyone have any experience in integrating
> Clojure in a big Java solution? Also i should mention that i'm quite
> new to Clojure, and functional programming overall. :)
> 
> Thanks
> 
> > 
> 

Luc Préfontaine

Armageddon was yesterday, today we have a real 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
-~--~~~~--~~--~--~---



Writing binary data using http.agent and duck-streams

2009-10-23 Thread Baishampayan Ghose
Hello,

I was trying to download a zip file using clojure.contrib.http.agent and
 writing it to a file using clojure.contrib.duck-streams.

Apparently the zip file is getting corrupt because I was trying to treat
the stream as a string.

Is there any way to use duck-streams to write data as binary?

I looked at the code but I am not sure about the exact way to do it. I
can probably use straight Java interop, but I would rather use the
contrib lib.

Thanks in advance.

Regards,
BG

-- 
Baishampayan Ghose 
oCricket.com



signature.asc
Description: OpenPGP digital signature


Scientific computing

2009-10-23 Thread Rock

What if I wanted to use Clojure for scientific computing, and in
particular for doing linear algebra and matrix computations a la
MATLAB?

What would my options be for representing matrices, not to mention,
especially, MULTIDIMENSIONAL ARRAYS?

Would java arrays be the way to go, or nested vectors?

I don't like the nested vector solution too much, but to be honest,
there really doesn't seem to be an adequate solution to this problem
at the moment. For instance, once you have a multidimensional array
(whatever kind it is) you can't, I believe, access any property of
that object at runtime to know its rank and dimensions, like you can
do with MATLAB or Scientifc Python (NumPy), or other similar
languages. And slicing, such as a[2:20:4, 1:9:3], which means every
element in a with indices that go from start to finish with a certain
step, seem to be problematic.

Actually the whole buiseness of having to deal with immutable (and
lazy) data structures appears to make computing with matrices and
multidimensional arrays a lot more difficult. Maybe this is just an
impression. Opinions?

Does Rich intend to address this issue of having multidimensional
arrays (for scientific computing purposes) sooner or later?

Thanks.

Rock

--~--~-~--~~~---~--~~
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: Removing duplication of redis/with-server in every function?

2009-10-23 Thread Kevin Downey

I think the point of this style of api is you just define your functions like

(defn one [] )
(defn two [] )

and call  the function like

(redis/with-server *db*
(one) (two))


On Thu, Oct 22, 2009 at 2:44 PM, Radford Smith  wrote:
>
> I'm trying out redis-clojure. Right now, my code looks like this:
>
> (defn one []
>  (redis/with-server *db*
>    (...)))
>
> (defn two []
>  (redis/with-server *db*
>    (...)))
>
> (defn three []
>  (redis/with-server *db*
>    (...)))
>
> It feels wrong to repeat myself every time I need to use the database.
> Is there a way to abstract out the (redis/with-server) part?
>
> >
>



-- 
And what is good, Phaedrus,
And what is not good—
Need we ask anyone to tell us these things?

--~--~-~--~~~---~--~~
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: Removing duplication of redis/with-server in every function?

2009-10-23 Thread Jonathan Smith

When figuring these things out it can sometimes help to look at the
implementations of stuff like defn (in clojure.core).

I'll leave it as an exercise to you, but you should note that you may
want to name-space qualify the database (depending on what you are
doing with it and where the database symbol is), or make it so that
you can pass the database from the top level macro.

(defn reddis-wrap-body [[params & body]]
(list params `(reddis/with-server *db* ~...@body)))

(defn reddis-wrap-bodies? [params-bodies?]
  (if (vector? (first params-bodies?))
(reddis-wrap-body params-bodies?)
(map reddis-wrap-body params-bodies?)))

(defn defreddis-sub
  ([name doc-string attr-map params-body]
 `(defn ~name ~doc-string ~attr-map ~@(wrap-bodies? params-body)))
  ([name doc-string-or-attr-map params-body]
 `(defn ~name ~doc-string-or-attr-map ~@(wrap-bodies? params-
body)))
  ([name params-body]
 `(defn ~name ~@(wrap-bodies? params-body

(defmacro defreddis [name & fdecl]
  (cond (or (vector? (first fdecl)) (list? (first fdecl)))
(defredis-sub name fdecl)
(or (vector? (second fdecl)) (list? (second fdecl)))
(defredis-sub name (first fdecl) (rest fdecl))
(or (vector? (nth fdecl 2)) (list (nth fdecl 2)))
(defredis-sub name (first fdecl) (second fdecl) (rest (rest
fdecl)

---
I have tested and macro-expanded the macro (it looks right to me), but
I can't be sure it actually runs as I don't have reddis setup.
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Clojure in a big Java solution

2009-10-23 Thread vanallan

Hi!
I am currently investigating if it is possible to convert a part of a
big Java system to Clojure. The reason for this is to make this part
run in parallel and hence ease the implementation by porting it to
Clojure. The problem is that these parts of the system is today
setting and changing a lot mutable data in a lot of different objects.

Which approach do you think i should have when i try to port these
Java methods? Should i try to retain the Java structure and change
these objects or is it better to create my own data structure and
somehow manage them? Does anyone have any experience in integrating
Clojure in a big Java solution? Also i should mention that i'm quite
new to Clojure, and functional programming overall. :)

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: Private multimethods possible?

2009-10-23 Thread Meikel Brandmeyer

Hi,

On Oct 23, 8:45 am, Christophe Grand  wrote:

> Other solutions are to use @#'ns/private-var to access private vars from the
> macro or to make the macro shallow using a public (usually higher-order)
> helper function (is this possible in the general case?).

It is not 100% possible in the general case but it the vast majority
of the cases, I'd say.

Concerning the private Vars: I'd actually suggest to dispose them
entirely. Namespaces can be split into an interface space contain the
public interface. It can 'use' a special internal namespace, which
does not belong to the public API of the library. All defs are normal,
public defs.

This approach solves all problems:
- The user knows, what the public API is.
- The macros can access everything they need.
- Code completion only finds the public stuff.
- It doesn't need modifications like a 'semi-private' or 'protected'
flag.

I think, Stephen exercised this approach in c.c.sql.

Sincerely
Meikel

PS: My opinion: design your library to be used with require and :as
instead of use. Document the public API. That is the contract. The
contract stays valid whether the system enforces it or not.

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