Issue with casting integers with clojure.lang.ifn's

2009-09-14 Thread Jeff Gross

I have a vector that I need to count the size of and do a simple math
calculation with.
Say for example:
(defn vect1 [1 2 3 4])

Typing (count vect1) returns the size of 4 that I need. I thought that
I could simply use (count vect1) in an a simple infix expression, ie:
(* (count vect1) 5)however, I get a class cast exception error
that the java.lang.integer cannot be cast to clojure.lang.ifn. How
would I go about doing this correctly?

--~--~-~--~~~---~--~~
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: Issue with casting integers with clojure.lang.ifn's

2009-09-14 Thread Baishampayan Ghose
Jeff Gross wrote:
> I have a vector that I need to count the size of and do a simple math
> calculation with.
> Say for example:
> (defn vect1 [1 2 3 4])
> 
> Typing (count vect1) returns the size of 4 that I need. I thought that
> I could simply use (count vect1) in an a simple infix expression, ie:
> (* (count vect1) 5)however, I get a class cast exception error
> that the java.lang.integer cannot be cast to clojure.lang.ifn. How
> would I go about doing this correctly?

I don't know why it's not working for you. The following works perfectly
fine for me:

(def vect1 [1 2 3 4])
(count vect1)
(* (count vect1) 5)

Are you sure you used def and now defn while defining vect1?

Regards,
BG

-- 
Baishampayan Ghose 
oCricket.com



signature.asc
Description: OpenPGP digital signature


Re: Issue with casting integers with clojure.lang.ifn's

2009-09-14 Thread Adrian Cuthbertson

Hi Jeff, you're using defn which defines a function instead of def
which defines a var;

(def vect1 [1 2 3 4])
#'user/vect1
user=> (* (count vect1) 5)
20

Rgds, Adrian.

On Mon, Sep 14, 2009 at 8:05 AM, Jeff Gross  wrote:
>
> I have a vector that I need to count the size of and do a simple math
> calculation with.
> Say for example:
> (defn vect1 [1 2 3 4])
>
> Typing (count vect1) returns the size of 4 that I need. I thought that
> I could simply use (count vect1) in an a simple infix expression, ie:
> (* (count vect1) 5)    however, I get a class cast exception error
> that the java.lang.integer cannot be cast to clojure.lang.ifn. How
> would I go about doing this correctly?
>
> >
>

--~--~-~--~~~---~--~~
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: Issue with casting integers with clojure.lang.ifn's

2009-09-14 Thread Adrian Cuthbertson

Alternatively, if really meant to use defn then it should have been;

(defn vect1 [] [1 2 3 4])
#'user/vect1
user=> (* (count (vect1)) 5)
20

On Mon, Sep 14, 2009 at 2:28 PM, Adrian Cuthbertson
 wrote:
> Hi Jeff, you're using defn which defines a function instead of def
> which defines a var;
>
> (def vect1 [1 2 3 4])
> #'user/vect1
> user=> (* (count vect1) 5)
> 20
>
> Rgds, Adrian.
>
> On Mon, Sep 14, 2009 at 8:05 AM, Jeff Gross  wrote:
>>
>> I have a vector that I need to count the size of and do a simple math
>> calculation with.
>> Say for example:
>> (defn vect1 [1 2 3 4])
>>
>> Typing (count vect1) returns the size of 4 that I need. I thought that
>> I could simply use (count vect1) in an a simple infix expression, ie:
>> (* (count vect1) 5)    however, I get a class cast exception error
>> that the java.lang.integer cannot be cast to clojure.lang.ifn. How
>> would I go about doing this correctly?
>>
>> >>
>>
>

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

2009-09-14 Thread Chouser

On Sun, Sep 13, 2009 at 7:09 PM, Richard Newman  wrote:
>
> Thought I'd share with the group. Clojure's sets are fast!
>
> http://www.holygoat.co.uk/blog/entry/2009-09-13-1

Nice writeup!

...and I hate to be picky, but you can probably compare
strings sizes using a faster mechanism than the examples you
gave.

While 'count' can give the size of a string, it first checks
it's type against a couple other possibilities before
calling the string's .length method.

(defn t [] (dotimes [i 2e7] (= 10 (count "123"
(time (t)) ; run this a few times to let HotSpot work its magic
--> "Elapsed time: 1000.960992 msecs"

If we know it's a string, we can call the .length method
directly.  If you also type-hint the string, this can make
the code rather uglier, and since this is hardly ever the
bottleneck in any application, it may not be worth it.  But
when you're benchmarking tiny opertions repeated a huge
number of times, this sort of detail starts to matter:

(defn t [] (dotimes [i 2e7] (= 10 (.length "123"
(time (t))
--> "Elapsed time: 100.096957 msecs"

There's no need to type hint the string there because it's
a literal and the Clojure compiler already knows its type.

It's time to see if == will help us, since we are after all
dealing with numbers here:

(defn t [] (dotimes [i 2e7] (== 10 (.length "123"
(time (t))
"Elapsed time: 96.791805 msecs"

So that's a bit better, but it's also worth looking at
where our numbers may be getting boxed or unboxed.

(use '[clojure.contrib.repl-utils :only (expression-info)])
(expression-info '10)
--> {:class java.lang.Integer, :primitive? false}

(expression-info '(.length "123"))
--> {:class int, :primitive? true}

So now we're comparing a boxed Integer 10 with an unboxed
int returned by 'length'.  We can do better:

(defn t [] (dotimes [i 2e7] (== (int 10) (.length "123"
(time (t)) ; Watch HotSpot remove orders of magnitude, until:
--> "Elapsed time: 0.044282 msecs"

That's really really fast.  That elapsed time is too small:
we'd have to increase the test size to get any useful
conclusion besides "really fast".  In fact, I almost wonder
if HotSpot is somehow memoizing the expression all by
itself.

Anyway, Clojure's sets by themselves appear to be fast
enough for you, or I assume you wouldn't have described them
as "crazy fast".  However, *if* you discover later that
they're are not fast enough you may be able to do length
tests faster than you were thinking.  This may yet be
a route to explore for improved overall speed.

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



Conjure 0.2 released.

2009-09-14 Thread Matt

I'm happy to inform everyone of the Conjure 0.2 release. Check it out
at: http://github.com/macourtney/Conjure

Download the jar at: http://github.com/macourtney/Conjure/downloads

Conjure is a Rails like framework for Clojure. The new version of
Conjure has many new features including:

* The Ring Library for connecting to web servers.
* Clj-record for persistence
* Clj-html for creating views.
* An h2 embedded database for persistence included (though, with a
little work, you can use pretty much any database clj-record uses)
* A Jetty server to get you up and running quickly.
* Migrations
* A testing framework which builds on test-is.

I've updated the github wiki with an extensive hello world tutorial
which demonstrates most of the features of Conjure. You can find it
at: http://wiki.github.com/macourtney/Conjure/hello-world-tutorial

I have plenty of features yet to add, and would appreciate any help.

I hope you enjoy this release,

Matt Courtney
--~--~-~--~~~---~--~~
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: A problem with compilation Clojure files

2009-09-14 Thread TPJ

On 14 Wrz, 06:01, Richard Newman  wrote:
> (...)
> Possibly it's just that your ns doesn't match up to the source path  
> (examples versus example).

Nope, it's not that easy. I changed "clojure.example.hello" to
"clojure.examples.hello" in the hello.clj file, and the message was
still the same. (Hard to believe, isn't it?)

I spent all the Sunday trying to compile anything, without any
success. It's either some stupid mistake on my side (I believe so, and
I hope so!), or something... (I don't know, what to say.)
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



how to invoke a function using the function name string?

2009-09-14 Thread wireless

(defn func [arg] (println "func: " arg))

I'd like to invoke 'func' using the string name of the function,
something like:
("func" "mytag")

Obviously, this would be answered with "java.lang.ClassCastException:
java.lang.String cannot be cast to clojure.lang.IFn"

I need to find a way to convert the string "func" to "IFn" somehow.
((make-IFn "func") "mytag")

How is this done?

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: how to invoke a function using the function name string?

2009-09-14 Thread Laurent PETIT
Hello,

one possibility:

1:15 user=> (defn func [arg] (println "func: " arg))
#'user/func
1:16 user=> ((ns-resolve *ns* (symbol "func")) "laurent")
func:  laurent
nil
1:17 user=>

HTH,

-- 
Laurent

2009/9/14 wireless 

>
> (defn func [arg] (println "func: " arg))
>
> I'd like to invoke 'func' using the string name of the function,
> something like:
> ("func" "mytag")
>
> Obviously, this would be answered with "java.lang.ClassCastException:
> java.lang.String cannot be cast to clojure.lang.IFn"
>
> I need to find a way to convert the string "func" to "IFn" somehow.
> ((make-IFn "func") "mytag")
>
> How is this done?
>
> 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: Some timing experiments

2009-09-14 Thread Richard Newman

> Nice writeup!

Thanks!

> ...and I hate to be picky, but you can probably compare
> strings sizes using a faster mechanism than the examples you
> gave.

Yeah, I figured I must have been doing something wrong, but I thought  
I'd leave it out there as a bit of ethnographic research :)

> So now we're comparing a boxed Integer 10 with an unboxed
> int returned by 'length'.  We can do better:

That's interesting... this is a case in which I thought HotSpot (or  
even Clojure's compiler) would automatically unbox: by the time it  
emits code for == it knows that it's comparing a primitive int to a  
constant non-primitive Integer (produced by the reader). Put another  
way: I would expect (== 10 10) to do no work at runtime, or at worst  
only primitive math, not boxed math.

Incidentally, this whole thing is a good example of a user expecting a  
sufficiently smart compiler: coming from Common Lisp one would expect  
the count implementation to be specialized for strings and other  
common types (essentially turning into a null check and a call  
to .length), and a similar optimization of ==. After all, the  
compiler(s) knows the input and output types of all the functions, and  
one of the arguments is constant. From that perspective, there's no  
reason why (== 10 (count some-string)) should not produce much the  
same bytecode as (== (int 10) (.length some-string)) -- both forms are  
equivalent, assuming no rebinding of count.

(While I'm very happy with Clojure, I can see the point of view of the  
people who come here and mouth off about Clojure not being as fast as  
Java. They're not strictly correct, but getting fast Clojure code does  
require some familiarity with which things its compiler will take care  
of, and which it leaves up to you.)

>
>   (defn t [] (dotimes [i 2e7] (== (int 10) (.length "123"
>   (time (t)) ; Watch HotSpot remove orders of magnitude, until:
>   --> "Elapsed time: 0.044282 msecs"
>
> That's really really fast.  That elapsed time is too small:
> we'd have to increase the test size to get any useful
> conclusion besides "really fast".  In fact, I almost wonder
> if HotSpot is somehow memoizing the expression all by
> itself.

It might be! In this case I suspect that something is inlining the  
result of (.length "123"), which is after all a compile-time constant.  
It might even be optimizing the entire contents of the loop away,  
replacing them with `false`.

> Anyway, Clojure's sets by themselves appear to be fast
> enough for you, or I assume you wouldn't have described them
> as "crazy fast".  However, *if* you discover later that
> they're are not fast enough you may be able to do length
> tests faster than you were thinking.  This may yet be
> a route to explore for improved overall speed.

You're quite right, and thanks for taking the time to continue the  
exploration!

My main conclusion from all this is that the JVM is really quick, and  
HotSpot is (by and large) a thing of wonder. There's not too much  
point in optimizing away a few hundred microseconds!

--~--~-~--~~~---~--~~
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: Conjure 0.2 released.

2009-09-14 Thread Stuart Halloway

Hi Matt,

This is a good start! Since you are only at 0.2, any chance I can  
convince you to target Clojure 1.1 and later only, and switch to using  
clojure.test (the replacement for clojure.contrib.test-is)?

If you are willing, I will make the switch on my fork and issue a pull  
request.

Stu

> I'm happy to inform everyone of the Conjure 0.2 release. Check it out
> at: http://github.com/macourtney/Conjure
>
> Download the jar at: http://github.com/macourtney/Conjure/downloads
>
> Conjure is a Rails like framework for Clojure. The new version of
> Conjure has many new features including:
>
> * The Ring Library for connecting to web servers.
> * Clj-record for persistence
> * Clj-html for creating views.
> * An h2 embedded database for persistence included (though, with a
> little work, you can use pretty much any database clj-record uses)
> * A Jetty server to get you up and running quickly.
> * Migrations
> * A testing framework which builds on test-is.
>
> I've updated the github wiki with an extensive hello world tutorial
> which demonstrates most of the features of Conjure. You can find it
> at: http://wiki.github.com/macourtney/Conjure/hello-world-tutorial
>
> I have plenty of features yet to add, and would appreciate any help.
>
> I hope you enjoy this release,
>
> Matt Courtney
> >


--~--~-~--~~~---~--~~
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: A problem with compilation Clojure files

2009-09-14 Thread Michael Wood

2009/9/14 TPJ :
>
> On 14 Wrz, 06:01, Richard Newman  wrote:
>> (...)
>> Possibly it's just that your ns doesn't match up to the source path
>> (examples versus example).
>
> Nope, it's not that easy. I changed "clojure.example.hello" to
> "clojure.examples.hello" in the hello.clj file, and the message was
> still the same. (Hard to believe, isn't it?)
>
> I spent all the Sunday trying to compile anything, without any
> success. It's either some stupid mistake on my side (I believe so, and
> I hope so!), or something... (I don't know, what to say.)

What does your "runclojure" script look like?

-- 
Michael Wood 

--~--~-~--~~~---~--~~
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: A problem with compilation Clojure files

2009-09-14 Thread Richard Newman

> Nope, it's not that easy. I changed "clojure.example.hello" to
> "clojure.examples.hello" in the hello.clj file, and the message was
> still the same. (Hard to believe, isn't it?)

What's the value of *compile-path*? Is it in your classpath?


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

2009-09-14 Thread Raoul Duke

> Much of the semantics of the query language is based on Prolog.

as an aside, i was under the impression that Datalog was syntactically
a subset of Prolog, but was not semantically so much so, since in
Prolog order matters e.g. wrt termination.

sincerely.

--~--~-~--~~~---~--~~
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: Conjure 0.2 released.

2009-09-14 Thread Jim Menard

Matt,

There's a missing double quote on line 11 of lancet.sh. After adding
that, I had no problem compiling Conjure. Looking forward to trying
it.

Jim

On Mon, Sep 14, 2009 at 9:49 AM, Matt  wrote:
>
> I'm happy to inform everyone of the Conjure 0.2 release. Check it out
> at: http://github.com/macourtney/Conjure
>
> Download the jar at: http://github.com/macourtney/Conjure/downloads
>
> Conjure is a Rails like framework for Clojure. The new version of
> Conjure has many new features including:
>
> * The Ring Library for connecting to web servers.
> * Clj-record for persistence
> * Clj-html for creating views.
> * An h2 embedded database for persistence included (though, with a
> little work, you can use pretty much any database clj-record uses)
> * A Jetty server to get you up and running quickly.
> * Migrations
> * A testing framework which builds on test-is.
>
> I've updated the github wiki with an extensive hello world tutorial
> which demonstrates most of the features of Conjure. You can find it
> at: http://wiki.github.com/macourtney/Conjure/hello-world-tutorial
>
> I have plenty of features yet to add, and would appreciate any help.
>
> I hope you enjoy this release,
>
> Matt Courtney
> >
>



-- 
Jim Menard, j...@io.com, jim.men...@gmail.com
http://www.io.com/~jimm/

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

2009-09-14 Thread Richard Newman

I put an update in my original post, and I also (like you) timed
without the side effects I'd inserted (empty-string print statements).
Unlike your code, I'm using non-constant strings, which might explain
the difference.

I note:

The 1,000 string length comparisons take 0.184ms; the negative set
test takes 0.281ms; and the positive takes 0.77ms. The string length
check, then, is slightly faster, but if the length matches you still
have the set test to perform. Probably not worth the complexity.

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



Modeling Data Associations in Clojure?

2009-09-14 Thread Brenton

I am starting to write a large web application using Clojure and
Compojure and am running into some design trouble while designing my
data model. To illustrate my problem I am going to make up some fake
data. Suppose you are writing an Insurance application which has the
tables Policy, Person and Vehicle. A policy has a person that is the
policy holder. A policy also has many vehicles. Each vehicle has a
person that is the primary driver. Most of the time the primary driver
is the same as the policy holder. If I were using one of the usual
object-relational mapping frameworks, (Hibernate, ActiveRecord) when I
load a policy I would get an object graph. If the person who is the
policy holder is the same as the person who is the primary driver of
the vehicle then the loaded Person object would be the same object. If
I change the address of the policy holder the primary driver's address
will also be changed.

How do people deal with this sort of thing in a Clojure application
(or any other functional language)? At first I thought that it would
be easy and I would just use nested maps. But this causes all kinds of
problems.  If I load the data into nested maps I now have two distinct
maps for the same person. If I change one of them, the other is not
updated. If I try to save this map back to the database, which person
map has the correct data? It is also awkward to update the person in
the first place. In Java you would just go policy.getPolicyHolder
().setAddress("..."). In Clojure you would have to do something like
(assoc policy :holder (assoc (:holder policy) :address "...")).

I have a feeling that there is a more "functional" way to do this sort
of thing. My question is, how to other people deal with this? The only
thing that I can think of is that I would avoid using nested maps to
model the database associations. I would load the policy into a map.
Then if I need the person, I would load that into a separate map. That
may be the correct functional approach. I was just asking in case
there is some really cool thing that people do that I don't know
about.

I had a look at clj-record to see how associations where handled. It
looks like nested maps are avoided here. Instead functions are created
to retrieve the associated data. Is the correct way of this this?

Thank you,
Brenton
--~--~-~--~~~---~--~~
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: Modeling Data Associations in Clojure?

2009-09-14 Thread Dragan Djuric

I think that you made the mistake in the first step: you cannot
*change* a clojure's map. Thus, you cannot change one particular
"snapshot" of person's data. No problem up to this point. You can only
create a new map with some data reused. There lies the problem - how
to "update the references" to the new map at all the places that link
to it. My hunch tells me to use refs for that :)
As for any examples, I cannot point you to that. It seems to me that
this problem, in my opinion one of the central problems in "real-
world" programming, is suspiciously underinvestigated in the world of
FP...
I am currently developing a DSL for dealing with the same problem, but
as it is in the rather early stage of development, no code is released
to the public yet.

On Sep 14, 11:34 pm, Brenton  wrote:
> I am starting to write a large web application using Clojure and
> Compojure and am running into some design trouble while designing my
> data model. To illustrate my problem I am going to make up some fake
> data. Suppose you are writing an Insurance application which has the
> tables Policy, Person and Vehicle. A policy has a person that is the
> policy holder. A policy also has many vehicles. Each vehicle has a
> person that is the primary driver. Most of the time the primary driver
> is the same as the policy holder. If I were using one of the usual
> object-relational mapping frameworks, (Hibernate, ActiveRecord) when I
> load a policy I would get an object graph. If the person who is the
> policy holder is the same as the person who is the primary driver of
> the vehicle then the loaded Person object would be the same object. If
> I change the address of the policy holder the primary driver's address
> will also be changed.
>
> How do people deal with this sort of thing in a Clojure application
> (or any other functional language)? At first I thought that it would
> be easy and I would just use nested maps. But this causes all kinds of
> problems.  If I load the data into nested maps I now have two distinct
> maps for the same person. If I change one of them, the other is not
> updated. If I try to save this map back to the database, which person
> map has the correct data? It is also awkward to update the person in
> the first place. In Java you would just go policy.getPolicyHolder
> ().setAddress("..."). In Clojure you would have to do something like
> (assoc policy :holder (assoc (:holder policy) :address "...")).
>
> I have a feeling that there is a more "functional" way to do this sort
> of thing. My question is, how to other people deal with this? The only
> thing that I can think of is that I would avoid using nested maps to
> model the database associations. I would load the policy into a map.
> Then if I need the person, I would load that into a separate map. That
> may be the correct functional approach. I was just asking in case
> there is some really cool thing that people do that I don't know
> about.
>
> I had a look at clj-record to see how associations where handled. It
> looks like nested maps are avoided here. Instead functions are created
> to retrieve the associated data. Is the correct way of this this?
>
> Thank you,
> Brenton
--~--~-~--~~~---~--~~
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: Modeling Data Associations in Clojure?

2009-09-14 Thread Luc Prefontaine
We mainly use macros to create functions to deal with associations,
a bit like ActiveRecord except that it is not yet as dynamic. We do not
use the table meta data to find the associations
and create the finders, etc... 
We want eventually to add stuff to clj-record to make it more like
ActiveRecord in this regard. For now it's the urgency to deliver that is
driving the schedule.
We also maintain a cache within these functions to speed up things. The
cache is not yet distributed again because of the schedule.

At least with these macros we are kind of half way toward the final
goal

Luc

On Mon, 2009-09-14 at 14:34 -0700, Brenton wrote:

> I am starting to write a large web application using Clojure and
> Compojure and am running into some design trouble while designing my
> data model. To illustrate my problem I am going to make up some fake
> data. Suppose you are writing an Insurance application which has the
> tables Policy, Person and Vehicle. A policy has a person that is the
> policy holder. A policy also has many vehicles. Each vehicle has a
> person that is the primary driver. Most of the time the primary driver
> is the same as the policy holder. If I were using one of the usual
> object-relational mapping frameworks, (Hibernate, ActiveRecord) when I
> load a policy I would get an object graph. If the person who is the
> policy holder is the same as the person who is the primary driver of
> the vehicle then the loaded Person object would be the same object. If
> I change the address of the policy holder the primary driver's address
> will also be changed.
> 
> How do people deal with this sort of thing in a Clojure application
> (or any other functional language)? At first I thought that it would
> be easy and I would just use nested maps. But this causes all kinds of
> problems.  If I load the data into nested maps I now have two distinct
> maps for the same person. If I change one of them, the other is not
> updated. If I try to save this map back to the database, which person
> map has the correct data? It is also awkward to update the person in
> the first place. In Java you would just go policy.getPolicyHolder
> ().setAddress("..."). In Clojure you would have to do something like
> (assoc policy :holder (assoc (:holder policy) :address "...")).
> 
> I have a feeling that there is a more "functional" way to do this sort
> of thing. My question is, how to other people deal with this? The only
> thing that I can think of is that I would avoid using nested maps to
> model the database associations. I would load the policy into a map.
> Then if I need the person, I would load that into a separate map. That
> may be the correct functional approach. I was just asking in case
> there is some really cool thing that people do that I don't know
> about.
> 
> I had a look at clj-record to see how associations where handled. It
> looks like nested maps are avoided here. Instead functions are created
> to retrieve the associated data. Is the correct way of this this?
> 
> Thank you,
> Brenton
> > 
> 

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



Re: Modeling Data Associations in Clojure?

2009-09-14 Thread Andy Kish

Hi Brenton,

Nested maps are a good way to start, but they're pretty low level as
you want to do more complicated things. If you're talking about data
associations, the relational model is higher level and it's really
worth modeling your data in that way.

Relational data manipulation doesn't require a sql database. If you
want to use an in memory data representation, clojure has a couple of
options. The clojure.set namespace [1] has some useful functions
operating on sets of maps, in a relational way. join, select, and
project will get you pretty far.

A more structured and powerful way of doing things is
clojure.contrib.datalog [2]. I haven't had a chance to play with this
(yet!), but it looks very cool. It's a functional approach and it's
more well integrated with the language than something generating sql.

If your app really does call for connection to an external db, then
there's no reason not to go with clj-record. You wouldn't be the first
person to make a SQL db backed web app. :) Using a database through
clojure feels a lot more natural than via an ORM.

I'm doing a small web app with compojure to familiarize myself with
clojure right now, so report back to the mailing list if something
works well for ya! I'm not at the point where I need a db, but if I do
I think I'll end up trying to use datalog.

Andy Kish.

[1] http://clojure.org/api#toc654
[2] http://richhickey.github.com/clojure-contrib/doc/datalog.html

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



finding paths in clojure with Floyd-Warshall - ugly code

2009-09-14 Thread ajuc

Hello, I'm new in clojure and lisp, and I have problem with making
this code elegant (or at least short).

This is Floyd-Warshall algorithm - very simple algorithm in imperative
form (3 nested for loops).

In clojure the best I've get so far is this monstrosity:

(defn create-graph [nodes distances]
{:nodes nodes :distances distances})

(defn processed [k i j D P]
(let
[S (+ (D [i k] ) (D [k j] ))]
(if
(< S (D [i j] ))
(do
[   (assoc D [i j] S)
(assoc P [i j] k)])
[D P])))

(defn shortest-path-before-k
[k n distances previous]
(loop [i 1 DP [distances previous]]
(if (< i n)
(recur
(+ i 1)
(loop [ j   1
[D P] DP]
(if (< j n)
(recur
(+ j 1)
(processed k i j D P))
[D P])))
DP)))

(defn Floyd-Warshall [G]
(let [n (count (G :nodes))]
(loop   [k 1,
distances (G :distances),
previous {}]
(if
(< k n)
(let
[DP (shortest-path-before-k k n 
distances previous)]
(recur (+ k 1) (first DP) (second DP)))
[distances previous]


;;data for testing
(def G
(create-graph
[0 1 2 3 4 5 6 7 8]
{   [0 1] 1, [0 3] 1,
[1 2] 1,
[2 0] 1,
[3 0] 1, [3 4] 1, [3 6] 1,
[4 5] 1,
[5 3] 1,
[6 3] 1, [6 7] 1,
[7 8] 1,
[8 6] 1}))

(Floyd-Warshall G)

How to do this the Right Way?

--~--~-~--~~~---~--~~
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 for game programming?

2009-09-14 Thread Elliott Slaughter

On Aug 30, 7:14 am, CuppoJava  wrote:
> Hi Elliot,
> I've written a small game using Clojure and LWJGL, and I've run into
> the exact same issue (balancing between mutability and immutability)
> when I was working on it. The three approaches I tried were:

First of all, thanks for your input; I'm still trying to figure out
what model is best for my game. Comments inline.

> The world is 1 ref containing the state of the game. Everything else
> is immutable.

I've thought about this (via zippers), but it seems to me that it
wouldn't be very good for either parallelism or message passing.

> Sprites are maps of refs of their properties. So practically
> everything is mutable.

Keeping a ref for each attribute seems excessive to me.

> And finally, sprites are refs of their states. Their states themselves
> are immutable.

This seems sensible to me.

I'm wondering if it might make sense to use agents to get an implicit
sort of message passing behavior between objects.

> I found the last option to be both the fastest and also the most
> straight-forward to program. The rule of thumb I used was: "Do I care
> about the identity of this object? or do I care only about the value?"
> If Identity is important, (ie. you need to know WHICH sprite this is)
> then I used a ref. If I cared only about the value (ie. what's the
> sprites state? I could care less about WHICH state object it is.) then
> it should be an immutable structure.
>
> Caveat: The only issue I've run into with this scheme is when the
> states of a sprite depend upon the identity of another sprite. (eg.
> This sprite needs to know WHICH weapon he's wielding.) So in this
> case, the state will contain a reference to a ref, which is something
> that's been written as not a very good thing to do.

So if I made a tree holding actors where actors are agents wrapping
maps, then I can imagine that I might want to give an agent a
reference to another agent (to allow them to pass messages back and
forth).

(def root (agent
   {:name :root
:children [(agent
{:name :child})]}))

Are you saying that this is not a suggested way of doing 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: Modeling Data Associations in Clojure?

2009-09-14 Thread Stuart Sierra

Hi Brenton,

I think the simplest solution to this problem is to use functions
instead of maps.  That is, instead of defining your API in terms of
maps with specific keys, define it in terms of functions that read/
write individual fields.

For example, you would have an opaque "Person" object, perhaps in a
Ref, and functions like get-name, set-name, get-policy, etc.  The
underlying storage model can be whatever you want -- sets, SQL,
files,   You just have to train yourself to never touch a model
object except through its defined API.

-SS


On Sep 14, 5:34 pm, Brenton  wrote:
> I am starting to write a large web application using Clojure and
> Compojure and am running into some design trouble while designing my
> data model. To illustrate my problem I am going to make up some fake
> data. Suppose you are writing an Insurance application which has the
> tables Policy, Person and Vehicle. A policy has a person that is the
> policy holder. A policy also has many vehicles. Each vehicle has a
> person that is the primary driver. Most of the time the primary driver
> is the same as the policy holder. If I were using one of the usual
> object-relational mapping frameworks, (Hibernate, ActiveRecord) when I
> load a policy I would get an object graph. If the person who is the
> policy holder is the same as the person who is the primary driver of
> the vehicle then the loaded Person object would be the same object. If
> I change the address of the policy holder the primary driver's address
> will also be changed.
>
> How do people deal with this sort of thing in a Clojure application
> (or any other functional language)? At first I thought that it would
> be easy and I would just use nested maps. But this causes all kinds of
> problems.  If I load the data into nested maps I now have two distinct
> maps for the same person. If I change one of them, the other is not
> updated. If I try to save this map back to the database, which person
> map has the correct data? It is also awkward to update the person in
> the first place. In Java you would just go policy.getPolicyHolder
> ().setAddress("..."). In Clojure you would have to do something like
> (assoc policy :holder (assoc (:holder policy) :address "...")).
>
> I have a feeling that there is a more "functional" way to do this sort
> of thing. My question is, how to other people deal with this? The only
> thing that I can think of is that I would avoid using nested maps to
> model the database associations. I would load the policy into a map.
> Then if I need the person, I would load that into a separate map. That
> may be the correct functional approach. I was just asking in case
> there is some really cool thing that people do that I don't know
> about.
>
> I had a look at clj-record to see how associations where handled. It
> looks like nested maps are avoided here. Instead functions are created
> to retrieve the associated data. Is the correct way of this this?
>
> Thank you,
> Brenton
--~--~-~--~~~---~--~~
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 paths in clojure with Floyd-Warshall - ugly code

2009-09-14 Thread Jonathan Smith

Woah formatting! :-)

This is just a formatting cleanup...
Mostly I pressed ctrl+alt+q in emacs.
I also removed some commas and made things more uniform in a couple
places
---
(defn create-graph [nodes distances]
  {:nodes nodes :distances distances})

(defn processed [k i j D P]
  (let [S (+ (D [i k] ) (D [k j] ))]
(if  (< S (D [i j] ))
  [(assoc D [i j] S) (assoc P [i j] k)]
[D P])))

(defn shortest-path-before-k
  [k n distances previous]
  (loop [i 1, DP [distances previous]]
(if (< i n)
  (recur (+ i 1) (loop [j 1, [D P] DP]
   (if (< j n)
 (recur  (+ j 1) (processed k i j D P))
 [D P])))
  DP)))

(defn Floyd-Warshall [G]
  (let [n (count (G :nodes))]
(loop [k 1, distances (G :distances), previous {}]
  (if (< k n)
(let [DP (shortest-path-before-k k n distances previous)]
  (recur (+ k 1) (first DP) (second DP)))
[distances previous]

;;data for testing
(def G (create-graph
[0 1 2 3 4 5 6 7 8]
{[0 1] 1
 [0 3] 1
 [1 2] 1
 [2 0] 1
 [3 0] 1
 [3 4] 1
 [3 6] 1
 [4 5] 1
 [5 3] 1
 [6 3] 1
 [6 7] 1
 [7 8] 1
 [8 6] 1}))

For readability's sake, you can probably do some stuff as well, by
making it less nested.
One way to do this is to 'unroll' it using let statements, so that it
has a more imperative look...
You don't have to nest the loop inside the recur, you can bind the
result in a let then pass it to recur.
(Only nest statements that make sense to nest... things that serve a
similar purpose).

You can also break this sort of thing up more by using lexically bound
functions.

Another thing is that I like to stick to dashed-out-lowercase for the
lisp code and only use CamelCase when I'm referring to Java calls. I
personally probably wouldn't name a function Floyd-Warshall, but
rather floyd-warshall (but that is somewhat a relic of my common lisp
sensibilities). Same with the variables in the loops, I would probably
stick to one case.

But anyway, I didn't do much, i'm sure someone else can do better! :)

On Sep 14, 5:48 pm, ajuc  wrote:
> Hello, I'm new in clojure and lisp, and I have problem with making
> this code elegant (or at least short).
>
> This is Floyd-Warshall algorithm - very simple algorithm in imperative
> form (3 nested for loops).
>
> In clojure the best I've get so far is this monstrosity:
>
> (defn create-graph [nodes distances]
>         {:nodes nodes :distances distances})
>
> (defn processed [k i j D P]
>         (let
>                 [S (+ (D [i k] ) (D [k j] ))]
>                 (if
>                         (< S (D [i j] ))
>                         (do
>                                 [       (assoc D [i j] S)
>                                         (assoc P [i j] k)])
>                         [D P])))
>
> (defn shortest-path-before-k
>         [k n distances previous]
>         (loop [i 1 DP [distances previous]]
>                 (if (< i n)
>                         (recur
>                                 (+ i 1)
>                                 (loop [ j                       1
>                                                                 [D P] DP]
>                                         (if (< j n)
>                                                 (recur
>                                                         (+ j 1)
>                                                         (processed k i j D P))
>                                                 [D P])))
>                         DP)))
>
> (defn Floyd-Warshall [G]
>         (let [n (count (G :nodes))]
>                 (loop   [k 1,
>                                         distances (G :distances),
>                                         previous {}]
>                         (if
>                                 (< k n)
>                                 (let
>                                         [DP (shortest-path-before-k k n 
> distances previous)]
>                                         (recur (+ k 1) (first DP) (second 
> DP)))
>                                 [distances previous]
>
> ;;data for testing
> (def G
>         (create-graph
>                 [0 1 2 3 4 5 6 7 8]
>                 {       [0 1] 1, [0 3] 1,
>                         [1 2] 1,
>                         [2 0] 1,
>                         [3 0] 1, [3 4] 1, [3 6] 1,
>                         [4 5] 1,
>                         [5 3] 1,
>                         [6 3] 1, [6 7] 1,
>                         [7 8] 1,
>                         [8 6] 1}))
>
> (Floyd-Warshall G)
>
> How to do this the Right Way?
--~--~-~--~~~---~--~~
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 

Re: questions about datalog

2009-09-14 Thread Tom Faulhaber

Did you also read the overview that's part of contrib at
http://richhickey.github.com/clojure-contrib/doc/datalog.html.

I don't know if you already saw that, but since you didn't mention it,
I thought I'd be sure.

Tom

On Sep 12, 6:53 pm, Robert Luo  wrote:
> Thank you Josh for your answer.
>
> I have read the sources of datalog, however, literal.clj and the ideas
> of the query language behind it is unknown for me, thus I can not
> understand it quite well. The same thing happens when I saw magic.clj,
> in which file I saw "magic transformation".
>
> I ran the example you posted on github, which seems return 2 records
> because same room-id merged. However, if I want the result set look
> like this: {room-id: 1 :seat-number [1 2], :players ["joe" "smith"]},
> is it possible? Another question, there are many aggregate function in
> SQL, can it be used in the conjunctive query?
--~--~-~--~~~---~--~~
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 paths in clojure with Floyd-Warshall - ugly code

2009-09-14 Thread Christophe Grand
Hello,

On Mon, Sep 14, 2009 at 11:48 PM, ajuc  wrote:

>
> Hello, I'm new in clojure and lisp, and I have problem with making
> this code elegant (or at least short).
>
> This is Floyd-Warshall algorithm - very simple algorithm in imperative
> form (3 nested for loops).
>
> In clojure the best I've get so far is this monstrosity:


[snip]


> How to do this the Right Way?
>

You are "accumulating" a result, which hints us at 'reduce.
And 'for provides the nested enumeration:

(defn floyd-warshall2 [{:keys [nodes distances]}]
  (reduce (fn [[distances prevs] [k x y]]
(let [d (+ (distances [x k] Double/POSITIVE_INFINITY)
  (distances [k y] Double/POSITIVE_INFINITY))]
(if (< d (distances [x y] Double/POSITIVE_INFINITY))
  [(assoc distances [x y] d) (assoc prevs [x y] k)]
  [distances prevs])))
[distances {}] (for [k nodes x nodes y nodes] [k x y])))

You should refrain from using indices or counters: most of the time, you can
directly test a sequence. Why to maintain a counter to know whe you reach
the end of a coll when (seq s) gives us this information and (next s)
"decrements" the counter. (see
http://clj-me.blogspot.com/2009/05/counting-without-numbers.html)

Christophe


-- 
Professional: http://cgrand.net/ (fr)
On Clojure: http://clj-me.blogspot.com/ (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 datalog

2009-09-14 Thread Mark Engelberg

On Mon, Sep 14, 2009 at 10:02 PM, Tom Faulhaber  wrote:
>
> Did you also read the overview that's part of contrib at
> http://richhickey.github.com/clojure-contrib/doc/datalog.html.
>

So because of this thread, I just went and perused the description of
the Clojure datalog library.

Some things I couldn't determine from the introductory documentation:

Is it essential to index one of the fields in a database?  Which sorts
of pattern queries benefit performance-wise from an index?  Can a
database be indexed on multiple fields?

Is the library fairly stable, or does it still need a lot of work?
How about performance?

Is there a way to query the database in a way that specifies you only
want the first result, rather than all results?

What mechanisms are in place to trace the various rule-chaining that
occurs as a result of a query?

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