Re: Creating instances of types not known at compile time

2011-05-10 Thread Meikel Brandmeyer
Hi,

Am 10.05.2011 um 01:49 schrieb Simon Katz:

> Passing the class object does exactly what I want.

Then passing a factory function will work, too—without reflection. Much more 
performance impact then checking a short string for a dot.

Sincerely
Meikel

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


Re: sessions in Ring, Sandbar, Compojure, etc...

2011-05-10 Thread James Reeves
On 10 May 2011 07:24, Shree Mulay  wrote:
> For the life of me, I can't get sessions to work, immaterial of which
> tutorial I try and get going???  Is there any tutorial out there that
> explicitly explains everything for a newb like me? After several
> round, I did successfully get form params to work! YEAH! But now, I'd
> like to create a login so that the user can login and have state - but
> I can't get SESSIONS TO WORK???

There's an example of Ring sessions linked from the Ring wiki:

https://gist.github.com/608048

Does that help?

- James

-- 
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: ANN: A Clojure library for the Facebook Graph API - clj-facebook-graph

2011-05-10 Thread Max Weber
Hi Bojan,

I've added a basic support to post something against the Facebook
Graph API. The documentation for this new feature can be found under
https://github.com/maxweber/clj-facebook-graph

Best regards

Max

On 9 Mai, 20:22, Bojan Jovičić  wrote:
> Hi Max,
> I have started investigating this, and it works very nice.
>
> I am a noob in Clojure/Java and I have a question about posting to FB using
> your library. I tried something like this:
>
> *(def auth-token (facebook-auth-by-name))
> (def auth-name (first (keys auth-token)))
> (with-facebook-auth-by-name auth-name (fb-get [:me :feed (str "message="
> "test")]))
> *
> But it seems not to work for processing parameters. The documentation for
> posting to FB is 
> here(bottom of the 
> page).

-- 
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: Ok, so code is data...

2011-05-10 Thread Bill Robertson
Thank you very much.

On May 10, 1:32 am, Justin Kramer  wrote:
> 'read' and 'read-string' are what you're looking for. They each read a
> single Clojure object from an input source (PushbackReader for read,
> String for read-string).
>
> Alternatively, something like this can read all top-level forms from a
> file:
>
> (defn read-all
>   "Reads all top-level forms from f, which will be coerced by
>   clojure.java.io/reader into a suitable input source. Not lazy."
>   [f]
>   (with-open [pbr (java.io.PushbackReader. (clojure.java.io/reader
> f))]
>     (doall
>      (take-while
>       #(not= ::eof %)
>       (repeatedly #(read pbr false ::eof))
>
> (read-all "/some/clojure/file.clj")
> => ((foo :bar) (baz))
>
> Justin
>
> On May 9, 11:36 pm, Bill Robertson  wrote:
>
>
>
>
>
>
>
> > How do I actually manipulate it?  I have some complicated logic that I
> > would like to transform into html (or maybe xml) for display
> > purposes.
>
> > I'm generating the Clojure code by parsing some nasty Java and
> > outputting s-expressions.  The Java is basic, but quite deeply
> > nested.  I want to generate working Clojure to demonstrate what that
> > can do for us, and get this nastiness documented of course.
>
> > Other than going into the source files and transforming it by hand
> > into a series of defs and quoted lists e.g.  (def my_func '(+ 1 2))
> > How do I actually just load code w/o evaluating it?
>
> > I've found the pretty printer macros, seems like that might be useful
> > if I wanted to statically transform the code.  I found the walker,
> > that looks like it might be useful in actually generating the output
> > (e.g. visit things, spit out (x|ht)ml.
>
> > I looked at the various load functions in clojure.core.  With the
> > exception of load (http://clojure.github.com/clojure/clojure.core-
> > api.html#clojure.core/load) they all seem to load and evaluate.  Is
> > load the answer or is it something I haven't found yet?
>
> > Once I get the code loaded, I don't think I need anything out of the
> > ordinary like macros or multi-methods.  I think I can just manipulate
> > the lists.  Does that sound correct?
>
> > I'm sorry if this are a stupid questions, but I've never done anything
> > in Clojure of any significance, and any helpful answers you provide
> > would could save me days of stumbling about.
>
> > 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: A simulation of the Monty Hall Problem

2011-05-10 Thread Adam Burry
Ken:

FYI, the best treatment of this problem I have seen is this one:
http://www.cs.utoronto.ca/~hehner/PPP.pdf

Adam

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


Aw: Ok, so code is data...

2011-05-10 Thread Stefan Kamphausen
Hi,

from what you write I get the feeling, that you may be doing things in a too 
complicated manner.  Maybe I am wrong.

In any case, although it may not be Clojure but Common Lisp, you might want 
to take a look at the HTML generation in Peter Seibels excellent Practical 
Common Lisp: See chapter 30 and 31 in http://gigamonkeys.com/book/

After reading that, diving into Enlive may be easier to understand.  See 
https://github.com/cgrand/enlive

But then again, as I said, maybe I just misunderstood what you really want.


Kind regards,
Stefan

-- 
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: Creating instances of types not known at compile time

2011-05-10 Thread Simon Katz
On May 10, 8:27 am, Meikel Brandmeyer  wrote:
> Hi,
>
> Am 10.05.2011 um 01:49 schrieb Simon Katz:
>
> > Passing the class object does exactly what I want.
>
> Then passing a factory function will work, too—without reflection. Much more
> performance impact then checking a short string for a dot.

Right. Thanks.

Simon

-- 
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: Enlive update transformation

2011-05-10 Thread Thorsten Wilms

On 05/09/2011 10:15 PM, Thorsten Wilms wrote:


I tried to turn that into a transformation:

(defn substitute-token
[& values]
#(map (fn [m] (update-in m [:content]
(fn [c] (apply (partial replace-str
"token" %) c
values))


Apparently I had my brain knotted, but raek and fliebel on IRC helped 
with untying some misconceptions :)


So the solution for applying a function, (partial replace-str "token" 
to) in this case, on the content in html nodes as Enlive transformation is:


(defn substitute-token
  "Replace text 'token' with to."
  [to]
  #(update-in % [:content]
 (fn [c] (apply (partial replace-str "token" to) c



--
Thorsten Wilms

thorwil's design for free software:
http://thorwil.wordpress.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: Ok, so code is data...

2011-05-10 Thread Jonathan Fischer Friberg
"How do I actually just load code w/o evaluating it?"
mostly when you want something like this, you want a macro. Manipulating
code is also done within macros.

http://www.gigamonkeys.com/book/macros-standard-control-constructs.html
http://www.gigamonkeys.com/book/macros-defining-your-own.html

Jonathan

On Tue, May 10, 2011 at 5:36 AM, Bill Robertson
wrote:

> How do I actually manipulate it?  I have some complicated logic that I
> would like to transform into html (or maybe xml) for display
> purposes.
>
> I'm generating the Clojure code by parsing some nasty Java and
> outputting s-expressions.  The Java is basic, but quite deeply
> nested.  I want to generate working Clojure to demonstrate what that
> can do for us, and get this nastiness documented of course.
>
> Other than going into the source files and transforming it by hand
> into a series of defs and quoted lists e.g.  (def my_func '(+ 1 2))
> How do I actually just load code w/o evaluating it?
>
> I've found the pretty printer macros, seems like that might be useful
> if I wanted to statically transform the code.  I found the walker,
> that looks like it might be useful in actually generating the output
> (e.g. visit things, spit out (x|ht)ml.
>
> I looked at the various load functions in clojure.core.  With the
> exception of load (http://clojure.github.com/clojure/clojure.core-
> api.html#clojure.core/load) they all seem to load and evaluate.  Is
> load the answer or is it something I haven't found yet?
>
> Once I get the code loaded, I don't think I need anything out of the
> ordinary like macros or multi-methods.  I think I can just manipulate
> the lists.  Does that sound correct?
>
> I'm sorry if this are a stupid questions, but I've never done anything
> in Clojure of any significance, and any helpful answers you provide
> would could save me days of stumbling about.
>
> Thanks!
>
>
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clojure@googlegroups.com
> Note that posts from new members are moderated - please be patient with
> your first post.
> To unsubscribe from this group, send email to
> clojure+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To 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: Ok, so code is data...

2011-05-10 Thread Timothy Baldridge
On Tue, May 10, 2011 at 10:26 AM, Jonathan Fischer Friberg <
odysso...@gmail.com> wrote:

> "How do I actually just load code w/o evaluating it?"
> mostly when you want something like this, you want a macro. Manipulating
> code is also done within macros.
>


Yeah, I would consider the whole code-is-data thing to refer more to using
macros than eval. For something fun to read up on this check out this
tutorial:

http://www.learningclojure.com/2010/09/clojure-faster-than-machine-code.html

Here the author goes and takes a tree data structure, and on-the-fly
generates function that represents that structure. The result is some
insanely fast code.

Timothy

-- 
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: Reading clojure code of larger domain specific projects

2011-05-10 Thread Paul deGrandis
You might want to take a look at the clojars project:
https://github.com/ato/clojars-web

It's not quite to the scale you're looking for, but it's a start.

Also keep in mind that a lot of business apps in Clojure look more
like libraries (for a specific domain problem) than applications, so
looking at large libraries might not be a bad substitute.

I'm also working on a project to augment clojars called clopi (Clojure
Package Index), that will let you filter results based on project
metrics (for example, line count), project activity, etc.  It also
displays the dependency information (the default rank score for
searching), allowing you mostly to answer the question "Which library
should I choose?" but also can help you find possible business
applications (edge/external-nodes in the graph, ie: projects which
nothing else depend upon).  I'll ping you when it's live.

Paul


On May 9, 8:32 pm, Christian Schuhegger
 wrote:
> Many thanks for all of your contributions so far! I definitely will
> (and already have to some extent) go through the recommended links.
> One additional project I came across that looks interesting is 
> midje:https://github.com/marick/Midje/wiki
>
> I see the point of having a forum to discuss best solutions, but that
> is a different matter from what I am talking about.
>
> Code looks different if you look at some lines of code or if you look
> at projects that have a relevant size (more than 100k lines of code)
> and where more than 10 people have worked on over an extended period
> of time. In addition the reason for looking for a project that is less
> technical and more domain specific is that typically developers have a
> very good understanding of how a technical solution should look like.
> They have the requirements already in their heads, because they are
> the final audience, the end-users. In a domain specific project the
> requirements come from outside and the business domain is typically
> not clear from the start. The project has to develop ways on how to
> deal with the evolution of the understanding/insight into the problem
> domain.
>
> You get a balanced view across different styles by looking at several
> projects of relevant size. Different teams find different solutions to
> similar problems.
>
> Projects that are not open-source but would seem to fall in the
> category that I am looking for are FlightCaster and 
> TheDeadline:http://www.infoq.com/articles/deadline-clojure-appengine
>
> Do you know of any projects of that type that are open-source to be
> able to analyse the code?
>
> Many thanks!
> Christian

-- 
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: ANN: A Clojure library for the Facebook Graph API - clj-facebook-graph

2011-05-10 Thread Bojan Jovičić
Dear Max,
this works like a charm.

Thank you very much for real fast response.

-- 
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 merge two ArrayMaps?

2011-05-10 Thread clj123123
(def e1 {:a 1 :b "b" :c 300 })
(def e2 {:a1 :d "blah"})

how to merge to get that?

{:a 1 :b "b" :c 300 :d "blah"}


also is there a way to merge if the ArrayMap was in a vector?

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: Hi, simple problem from a newbie

2011-05-10 Thread Armando Blancas
Don't be put off by these initial difficulties; this stuff is really
different. I found this paper a very good read:
http://www.cs.kent.ac.uk/people/staff/dat/miranda/whyfp90.pdf

It puts the finger on a common problem:

"Such a catalogue of “advantages” is all very well, but one must not
be surprised
if outsiders don’t take it too seriously. It says a lot about what
functional
programming isn’t (it has no assignment, no side effects, no flow of
control) but
not much about what it is. The functional programmer sounds rather
like a
mediæval monk, denying himself the pleasures of life in the hope that
it will
make him virtuous. To those more interested in material benefits,
these “advantages”
are totally unconvincing."

Then goes on to explain why those "advantages" actually are
advantages.

On May 8, 4:59 pm, iamcreasy  wrote:
> Hi everybody :)
>
> I am an experienced C++ programmer. Recently I decided to try out
> clojure(I have some java experience).
>
> I read some tutorials of the basics clojure. Now I want to implement
> some simple algorithms. Starting with Insertion sort.
>
> But, when I have tried to start, I find myself lost. It feels like I
> have to write a big line to solve it.
>
> Can anyone help me to have a head start!
>
> 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 merge two ArrayMaps?

2011-05-10 Thread Jonathan Fischer Friberg
use (merge e1 e2)

Jonathan

On Tue, May 10, 2011 at 6:11 PM, clj123123  wrote:

> (def e1 {:a 1 :b "b" :c 300 })
> (def e2 {:a1 :d "blah"})
>
> how to merge to get that?
>
> {:a 1 :b "b" :c 300 :d "blah"}
>
>
> also is there a way to merge if the ArrayMap was in a vector?
>
> Thanks.
>
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clojure@googlegroups.com
> Note that posts from new members are moderated - please be patient with
> your first post.
> To unsubscribe from this group, send email to
> clojure+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To 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 Clojure protocols are implemented internally?

2011-05-10 Thread Krukow


On May 9, 12:09 am, David Nolen  wrote:
> On Sun, May 8, 2011 at 4:43 PM, Dmitry Kakurin 
> wrote:
>
> > Very well, something along these lines would be my guess too.
> > But that would mean that in case 2 protocols are no faster
> > than multimethods.

They are much faster than multimethods because you know up front that
dispatch is on the type of the first argument.

>
> Not true.
>
> > And I've got an impression that protocols are described to be as fast as
> > interface dispatch (callvirt).

Almost as fast in case one. There is an indirection because of
dispatch.

> > So either my impression is wrong (which is totally possible) or there is
> > some clever trick available on JVM to do case 2 below faster.
> > That's why I'm asking.
>
 And yes, there is a difference in performance if you extend after you
define your type.


To get accurate we need Rich, here.

> They are not as fast, but they are quite fast. Look at the implementation.
>
> David

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


Re: How to merge two ArrayMaps?

2011-05-10 Thread clj123123
I wanted to merge ArrayMap in a vector. Is there a way to do that?
I put this together but wondering it there's already a library for
that.

(defn merge-by-field
 "xt must have field vals unique"
 [xs xt field]
  (let [fmerge (fn [id s]
 (merge s (first (filter #(= id (val (find % field)))
xt]
(for [i xs]
  (fmerge (field i) i

(def e1 [{:a 1 :b "b" :c 300 }{:a 2 :b "a" :c 500 }])
(def e2 [{:a 1 :d "blah"}{:a 2 :d "blah2"}])

(merge-by-field e1 e2 :a)


output:
({:d "blah", :a 1, :b "b", :c 300} {:d "blah2", :a 2, :b "a", :c 500})


On May 10, 9:31 am, Jonathan Fischer Friberg 
wrote:
> use (merge e1 e2)
>
> Jonathan
>
>
>
> On Tue, May 10, 2011 at 6:11 PM, clj123123  wrote:
> > (def e1 {:a 1 :b "b" :c 300 })
> > (def e2 {:a1 :d "blah"})
>
> > how to merge to get that?
>
> > {:a 1 :b "b" :c 300 :d "blah"}
>
> > also is there a way to merge if the ArrayMap was in a vector?
>
> > 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- Hide quoted text -
>
> - Show quoted text -

-- 
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 merge two ArrayMaps?

2011-05-10 Thread Alan
user> (let [inputs (concat e1 e2)]
(map (partial apply merge)
 (vals (group-by :a inputs
({:d "blah", :a 1, :b "b", :c 300} {:d "blah2", :a 2, :b "a", :c 500})

On May 10, 10:10 am, clj123123  wrote:
> I wanted to merge ArrayMap in a vector. Is there a way to do that?
> I put this together but wondering it there's already a library for
> that.
>
> (defn merge-by-field
>  "xt must have field vals unique"
>  [xs xt field]
>   (let [fmerge (fn [id s]
>                  (merge s (first (filter #(= id (val (find % field)))
> xt]
>     (for [i xs]
>       (fmerge (field i) i
>
> (def e1 [{:a 1 :b "b" :c 300 }{:a 2 :b "a" :c 500 }])
> (def e2 [{:a 1 :d "blah"}{:a 2 :d "blah2"}])
>
> (merge-by-field e1 e2 :a)
>
> output:
> ({:d "blah", :a 1, :b "b", :c 300} {:d "blah2", :a 2, :b "a", :c 500})
>
> On May 10, 9:31 am, Jonathan Fischer Friberg 
> wrote:
>
> > use (merge e1 e2)
>
> > Jonathan
>
> > On Tue, May 10, 2011 at 6:11 PM, clj123123  wrote:
> > > (def e1 {:a 1 :b "b" :c 300 })
> > > (def e2 {:a1 :d "blah"})
>
> > > how to merge to get that?
>
> > > {:a 1 :b "b" :c 300 :d "blah"}
>
> > > also is there a way to merge if the ArrayMap was in a vector?
>
> > > 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: A simulation of the Monty Hall Problem

2011-05-10 Thread Konrad Hinsen
On 10 May, 2011, at 13:50 , Adam Burry wrote:

> FYI, the best treatment of this problem I have seen is this one:
> http://www.cs.utoronto.ca/~hehner/PPP.pdf

There's also a compact Clojure solution based on the probability monad:

https://github.com/richhickey/clojure-contrib/blob/master/src/examples/clojure/clojure/contrib/probabilities/examples_finite_distributions.clj

Konrad.

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


San Francisco Clojure Users

2011-05-10 Thread David Jagoe
G'day everyone,

Forgive me if this is not the appropriate place for this message, but
I'm in San Francisco for a few days from Johannesburg, South Africa.
Any clojure users keen on meeting up? Any clojure events going on that
I haven't spotted on-line?

I run a business using Clojure for web development.


Cheers,
David

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


Re: A simulation of the Monty Hall Problem

2011-05-10 Thread Ken Wesson
On Tue, May 10, 2011 at 2:59 PM, Konrad Hinsen
 wrote:
> On 10 May, 2011, at 13:50 , Adam Burry wrote:
>
>> FYI, the best treatment of this problem I have seen is this one:
>> http://www.cs.utoronto.ca/~hehner/PPP.pdf
>
> There's also a compact Clojure solution based on the probability monad:
>
> https://github.com/richhickey/clojure-contrib/blob/master/src/examples/clojure/clojure/contrib/probabilities/examples_finite_distributions.clj

Interesting. It is, as I thought, very short with monads -- though
that version doesn't really use randomness, but instead enumerates all
the possibilities. You'd need slightly different monads to thread a
random bit-stream through instead of enumerating all the alternatives.
The difference is like that between classical randomness and quantum
many worlds, with the probability distribution monads doing the many
worlds version. :)

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


can this program be sped up?

2011-05-10 Thread Carl Cotner
Hi,

Is it possible to make Clojure run the following toy program faster
(without changing the algorithm)?

(defn primeQ [n] (primes 2 (/ n 2) n))

(defn divQ [d n] (= (mod n d) 0))

(defn primes [d2 t2 n2]
  (loop [d (int d2) t (int t2) n (int n2)]
(if (divQ d n) false
(if (> d t) true
(recur (inc d) t n)

Running this on my machine:

user> (time (primeQ 71378569))
"Elapsed time: 11047.721488 msecs"

is roughly 13 times slower than running the comparable Java program.
It seems like one should be able to do much better for such a
straightforward program. Is there anything I can do to make this
program faster?

Thanks,
Carl

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


Re: can this program be sped up?

2011-05-10 Thread Ken Wesson
On Tue, May 10, 2011 at 2:21 PM, Carl Cotner  wrote:
> Hi,
>
> Is it possible to make Clojure run the following toy program faster
> (without changing the algorithm)?
>
> (defn primeQ [n] (primes 2 (/ n 2) n))
>
> (defn divQ [d n] (= (mod n d) 0))
>
> (defn primes [d2 t2 n2]
>  (loop [d (int d2) t (int t2) n (int n2)]
>    (if (divQ d n) false
>        (if (> d t) true
>            (recur (inc d) t n)
>
> Running this on my machine:
>
> user> (time (primeQ 71378569))
> "Elapsed time: 11047.721488 msecs"
>
> is roughly 13 times slower than running the comparable Java program.
> It seems like one should be able to do much better for such a
> straightforward program. Is there anything I can do to make this
> program faster?

Several things, though at the expense of idiomaticity of the code in many cases.

1: Algorithmic smarts. Division is expensive. Replace divQ's
implementation in terms of mod with the algorithm binary GCD and it
will probably be faster. Likewise the / n 2 in primeQ can be changed
to a bit-shift-right.

2: Algorithmic smarts. There are more sophisticated algorithms for
checking for primality, for example only checking for divisibility by
primes smaller than or equal to the square root of the number rather
than every number from 2 to half the number.

3: Technical speedups. Avoid function calls inside inner loops, which
cause boxing and require var dereferencing. Fold divQ into primes,
since it's only used once in there.

4: Technical speedups. Use unchecked primitive math throughout, if you
know the range of a Java long will not be exceeded. With Clojure 1.2,
use unchecked-inc and such (also in the binary GCD that will now be in
primes in place of the divQ call) and make sure the primitives stay
unboxed -- in particular, use loop/recur in primes (which means you
may as well fold primes into primeQ as well) to avoid boxing at the
end of each iteration.

5: Technical speedups. Make sure to use java -server to run it.

6: Benchmarking accuracy. Don't include the JIT compilation, the
overhead of calling the primeQ function and of the benchmarking
itself, or, God forbid, the JVM startup time; do a few thousand
repetitions of the whole thing with a small (e.g. four-digit) prime to
get the JITting done and then do a big number several times and
average the results, using a timing macro of some sort from inside the
running Clojure session to avoid counting the JVM startup time. The
timing/call overhead will be lost in the noise if the number tested in
the timed runs is big enough.

By the way, you can (and generally should) name your Clojure
predicates with a question-mark, e.g. div? instead of divQ or div-p. I
take it you're used to another Lisp?

If you're trying to do an apples-to-apples comparison of Java and
Clojure, though, rather than make the fastest Clojure primality
tester, then you will want to ignore points 1 and 2 above unless you
make the same algorithmic improvements to both versions and you will
want the timing loop to be internal (rather than a shell thing
wrapping the JVM invocation) in both cases (Clojure has more
dependencies, so takes a bit longer to start up the JVM, but this
won't matter in any sane situation, whereas math speed will).

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


Re: can this program be sped up?

2011-05-10 Thread Ken Wesson
Addendum: if you are using Clojure 1.3 alpha, function calls may avoid
boxing, but still have a call overhead, so you probably still want to
avoid them in your loops, and unchecked primitive math is done
differently. Check the documentation on that.

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


Re: can this program be sped up?

2011-05-10 Thread David Nolen
On Tue, May 10, 2011 at 2:21 PM, Carl Cotner  wrote:

> Hi,
>
> Is it possible to make Clojure run the following toy program faster
> (without changing the algorithm)?
>
> (defn primeQ [n] (primes 2 (/ n 2) n))
>
> (defn divQ [d n] (= (mod n d) 0))
>
> (defn primes [d2 t2 n2]
>  (loop [d (int d2) t (int t2) n (int n2)]
>(if (divQ d n) false
>(if (> d t) true
>(recur (inc d) t n)
>
> Running this on my machine:
>
> user> (time (primeQ 71378569))
> "Elapsed time: 11047.721488 msecs"
>
> is roughly 13 times slower than running the comparable Java program.
> It seems like one should be able to do much better for such a
> straightforward program. Is there anything I can do to make this
> program faster?
>
> Thanks,
> Carl


It doesn't answer your question but maybe this will help you get some ideas
on how to optimize your code:
http://dosync.posterous.com/lispers-know-the-value-of-everything-and-the

For this kind of thing to be competitive w/ Java it's best that you work
with a 1.3 alpha or the master branch.

David

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

Re: San Francisco Clojure Users

2011-05-10 Thread Zach Tellman
Unfortunately, you just missed the monthly Bay Area user group meetup,
which was yesterday.  But with Google I/O going on, maybe there are
enough people around that an impromptu meetup would be plausible.

Zach

On May 10, 1:07 pm, David Jagoe  wrote:
> G'day everyone,
>
> Forgive me if this is not the appropriate place for this message, but
> I'm in San Francisco for a few days from Johannesburg, South Africa.
> Any clojure users keen on meeting up? Any clojure events going on that
> I haven't spotted on-line?
>
> I run a business using Clojure for web development.
>
> Cheers,
> David

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


Re: How Clojure protocols are implemented internally?

2011-05-10 Thread Christophe Grand
On Tue, May 10, 2011 at 7:13 PM, Krukow  wrote:

> > > And I've got an impression that protocols are described to be as fast
> as
> > > interface dispatch (callvirt).
>
> Almost as fast in case one. There is an indirection because of
> dispatch.
>

There is special support at the call site by the compiler so in "good" cases
(ie interface) you just have a type check (on the interface) and the
callvirt.


> > So either my impression is wrong (which is totally possible) or there is
> > > some clever trick available on JVM to do case 2 below faster.
> > > That's why I'm asking.
> >
>  And yes, there is a difference in performance if you extend after you
> define your type.
>

There are also differences in flexibility : inline impls are subject to the
same problems as interfaces, namely methods names clashes between interfaces
or interfaces and Object.

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: San Francisco Clojure Users

2011-05-10 Thread David Jagoe
On 10 May 2011 15:00, Zach Tellman  wrote:
> Unfortunately, you just missed the monthly Bay Area user group meetup,

Yeah I saw that... gutted!

> which was yesterday.  But with Google I/O going on, maybe there are
> enough people around that an impromptu meetup would be plausible.

That would be great. There aren't too many Clojure developers in South Africa!!

>
> Zach
>
> On May 10, 1:07 pm, David Jagoe  wrote:
>> G'day everyone,
>>
>> Forgive me if this is not the appropriate place for this message, but
>> I'm in San Francisco for a few days from Johannesburg, South Africa.
>> Any clojure users keen on meeting up? Any clojure events going on that
>> I haven't spotted on-line?
>>
>> I run a business using Clojure for web development.
>>
>> Cheers,
>> David
>
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clojure@googlegroups.com
> Note that posts from new members are moderated - please be patient with your 
> first post.
> To unsubscribe from this group, send email to
> clojure+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en

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


Bug? (vary-meta (promise) assoc :foo 1) hangs in Clojure 1.2

2011-05-10 Thread Ken Wesson
(vary-meta (promise) assoc :foo 1) hangs in Clojure 1.2. I suspect
that vary-meta is somehow triggering an attempt to deref the promise.

-- 
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: Bug? (vary-meta (promise) assoc :foo 1) hangs in Clojure 1.2

2011-05-10 Thread Alan
(promise) hangs in 1.2, if you try to print it from the repl. There's
a ticket somewhere for better handling of non-delivered promises.

On May 10, 5:01 pm, Ken Wesson  wrote:
> (vary-meta (promise) assoc :foo 1) hangs in Clojure 1.2. I suspect
> that vary-meta is somehow triggering an attempt to deref the promise.

-- 
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: Bug? (vary-meta (promise) assoc :foo 1) hangs in Clojure 1.2

2011-05-10 Thread Ken Wesson
On Tue, May 10, 2011 at 8:04 PM, Alan  wrote:
> (promise) hangs in 1.2, if you try to print it from the repl. There's
> a ticket somewhere for better handling of non-delivered promises.

Ah, damn. It should see if it's delivered or not and print
# or similar in that case rather than try to print it.

Anyway, maybe this will come in handy:

(defn timeout-promise
  "A promise with a timeout. When dereffed, returns the
delivered value if already delivered, otherwise blocks
until delivered or the deadline expires, whichever comes
first. Returns the delivered value if delivered in time and
if-timeout if the deadline expires first.

Ex.: user=> (def q (promise 1000 :timed-out))
 user=>(do (future (Thread/sleep 900) (deliver q 42)) @q)
 42
 user=> (def q (promise 1000 :timed-out))
 user=>(do (future (Thread/sleep 1100) (deliver q 42)) @q)
 :timed-out"
  [timeout-in-ms if-timeout]
  (let [d (java.util.concurrent.CountDownLatch. 1)
v (atom nil)]
(reify
 clojure.lang.IDeref
  (deref [_] (if (.await d timeout-in-ms ms)
   @v
   if-timeout)).lang.IFn
  (invoke [this x]
(locking d
  (if (pos? (.getCount d))
(do (reset! v x)
(.countDown d)
this)
(throw (IllegalStateException.
 "Multiple deliver calls to a promise"

Since the above code is derived from the existing implementation of
promise, it therefore automatically inherits the Clojure license terms
as the terms for its use, if I understand copyright law correctly.

The only changes from the implementation of promise are:

1. Docstring.
2. The two arguments.
3. Calls the await method of CountDownLatch that takes a timeout.
4. Conditions on the return value and returns if-timeout on failure.

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


[OFF-TOPIC] - CA Status

2011-05-10 Thread Bruno Oliveira
I sent my contributor agreement through mail, months ago. I would to
know if was received, my requests was to clojure and clojure-contrib
projects

I didn't received any feedback yet.

Thanks a lot

-- 
“Success is having to worry about every damn thing in the world,
except money.” - Johnny Cash
-
http://gitshelf.com
http://abstractj.com
http://github.com/abstractj
-
Volenti Nihil Difficile

-- 
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: sessions in Ring, Sandbar, Compojure, etc...

2011-05-10 Thread Matthew Boston
Maybe try looking at the source of a project on github using logins/
sessions.  One I'm currently using for reference is from 4clojure
https://github.com/dbyrne/4clojure

On May 10, 2:24 am, Shree Mulay  wrote:
> For the life of me, I can't get sessions to work, immaterial of which
> tutorial I try and get going???  Is there any tutorial out there that
> explicitly explains everything for a newb like me? After several
> round, I did successfully get form params to work! YEAH! But now, I'd
> like to create a login so that the user can login and have state - but
> I can't get SESSIONS TO WORK???
>
> frustrated!,
>
> shree

-- 
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: [OFF-TOPIC] - CA Status

2011-05-10 Thread Christopher Redinger
Hi Bruno,
Unless Rich has received your  CA within the past couple weeks, I would say 
he never received it.

The compete list of CAs that he is received (within the past couple weeks) 
is here:
http://clojure.org/contributing

I'm assuming you sent it to the PO Box listed? Did you do anything special 
with the CA - like send it Certified or Registered?

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

possibly non-intuitive behaviour of clojure.set/rename-keys and possible enhancement suggestion

2011-05-10 Thread Sunil S Nandihalli
Hello everybody,
 I tried to use clojure.set/rename-keys .. I kind of felt it has a little
odd behaviour... for example ..

(clojure.set/rename-keys {1 :a 2 :b 3 :c}  {1 2 2 3 3 4})

returns

{4 :a}

I think a more reasonable behavior would be to have it return

{2 :a 3 :b 4 :c}

a further enhancement suggestion would be to accept a function instead of a
map that way even the existing code will not break since the map already
implements IFn

a sample implementation which takes the above into account would be ..

(defn rename-keys [mp kmap-fn]
  (into {} (map (fn [[key val]] [(kmap-fn key) val]) mp)))


Thanks,
Sunil.

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

Re: can this program be sped up?

2011-05-10 Thread Carl Cotner
Thanks, David, for your reply. I enjoyed your blog post, and I will
try 1.3 alpha!

Carl


On May 10, 5:58 pm, David Nolen  wrote:
> It doesn't answer your question but maybe this will help you get some ideas
> on how to optimize your 
> code:http://dosync.posterous.com/lispers-know-the-value-of-everything-and-the
>
> For this kind of thing to be competitive w/ Java it's best that you work
> with a 1.3 alpha or the master branch.
>
> David

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


Re: can this program be sped up?

2011-05-10 Thread Carl Cotner
On May 10, 5:47 pm, Ken Wesson  wrote:
> Addendum: if you are using Clojure 1.3 alpha, function calls may avoid
> boxing, but still have a call overhead, so you probably still want to
> avoid them in your loops, and unchecked primitive math is done
> differently. Check the documentation on that.

Thanks, I will try 1.3 alpha!

Carl

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


Re: can this program be sped up?

2011-05-10 Thread Carl Cotner
Thanks for your thorough reply, Ken.

On May 10, 5:43 pm, Ken Wesson  wrote:
>
> [...]
>
> 1: Algorithmic smarts. Division is expensive. Replace divQ's
> implementation in terms of mod with the algorithm binary GCD and it
> will probably be faster. Likewise the / n 2 in primeQ can be changed
> to a bit-shift-right.
>
> 2: Algorithmic smarts. There are more sophisticated algorithms for
> checking for primality, for example only checking for divisibility by
> primes smaller than or equal to the square root of the number rather
> than every number from 2 to half the number.

Yeah, I know, the program is just meant to be something a bit less
trivial than an empty loop, for (very simple) run time comparison
purposes, and also to help me get a taste of various languages.
However, I'm interested in actually learning Clojure for machine
learning applications and so am trying to understand its
implementation details in more depth. (BTW, as you could probably tell
from the code, I am not actually trying to implement a practical test
for primality. On the other hand, you probably couldn't tell that I'm
actually a number theorist. :-)

> 3: Technical speedups. Avoid function calls inside inner loops, which
> cause boxing and require var dereferencing. Fold divQ into primes,
> since it's only used once in there.

Just to make sure I understand: type hints don't cause Clojure to
optimize these things out by itself? (I had been supposing that type
hints would propagate through functions and function calls so that
boxing, for example, would be avoided.)

> 4: Technical speedups. Use unchecked primitive math throughout, if you
> know the range of a Java long will not be exceeded. With Clojure 1.2,
> use unchecked-inc and such (also in the binary GCD that will now be in
> primes in place of the divQ call) and make sure the primitives stay
> unboxed -- in particular, use loop/recur in primes (which means you
> may as well fold primes into primeQ as well) to avoid boxing at the
> end of each iteration.
>
> 5: Technical speedups. Make sure to use java -server to run it.

Thanks, I will do that.

> 6: Benchmarking accuracy. Don't include the JIT compilation, the
> overhead of calling the primeQ function and of the benchmarking
> itself, or, God forbid, the JVM startup time; do a few thousand
> repetitions of the whole thing with a small (e.g. four-digit) prime to
> get the JITting done and then do a big number several times and
> average the results, using a timing macro of some sort from inside the
> running Clojure session to avoid counting the JVM startup time. The
> timing/call overhead will be lost in the noise if the number tested in
> the timed runs is big enough.

Does the built-in Clojure (time ...) expression that I used from the
SLIME REPL actually include things like JVM startup time?

  user> (time (primeQ 71378569))
  "Elapsed time: 11047.721488 msecs"

Also, do you happen to know of a good reference for how/when the JVM
does JIT compilation?

> By the way, you can (and generally should) name your Clojure
> predicates with a question-mark, e.g. div? instead of divQ or div-p. I
> take it you're used to another Lisp?

Thanks, I will do that in the future - I just read about that Clojure
convention shortly after posting. :-)

> If you're trying to do an apples-to-apples comparison of Java and
> Clojure, though, rather than make the fastest Clojure primality
> tester, then you will want to ignore points 1 and 2 above unless you
> make the same algorithmic improvements to both versions and you will
> want the timing loop to be internal (rather than a shell thing
> wrapping the JVM invocation) in both cases (Clojure has more
> dependencies, so takes a bit longer to start up the JVM, but this
> won't matter in any sane situation, whereas math speed will).

Yeah, I am trying to do an apples-to-apples comparison while using the
tools that Clojure provides (e.g., type hints) to make the code as
efficient as possible.

Thanks again for your help.

Carl

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


Re: can this program be sped up?

2011-05-10 Thread Ken Wesson
On Tue, May 10, 2011 at 10:14 PM, Carl Cotner  wrote:
> Thanks for your thorough reply, Ken.

You're welcome.

>> 3: Technical speedups. Avoid function calls inside inner loops, which
>> cause boxing and require var dereferencing. Fold divQ into primes,
>> since it's only used once in there.
>
> Just to make sure I understand: type hints don't cause Clojure to
> optimize these things out by itself? (I had been supposing that type
> hints would propagate through functions and function calls so that
> boxing, for example, would be avoided.)

Not in Clojure 1.2. 1.3 can sometimes avoid boxing across function
calls, but I think you have to do specific things when defining the
called functions to support it.

There's also no type inference across function call boundaries. Hint
the arguments of every function where it will matter for speed, or use
*warn-on-reflection* to find the problem areas with non-primitives
lacking type hints and hint them.

>> 6: Benchmarking accuracy. Don't include the JIT compilation, the
>> overhead of calling the primeQ function and of the benchmarking
>> itself, or, God forbid, the JVM startup time; do a few thousand
>> repetitions of the whole thing with a small (e.g. four-digit) prime to
>> get the JITting done and then do a big number several times and
>> average the results, using a timing macro of some sort from inside the
>> running Clojure session to avoid counting the JVM startup time. The
>> timing/call overhead will be lost in the noise if the number tested in
>> the timed runs is big enough.
>
> Does the built-in Clojure (time ...) expression that I used from the
> SLIME REPL actually include things like JVM startup time?
>
>  user> (time (primeQ 71378569))
>  "Elapsed time: 11047.721488 msecs"

Anything run from the REPL won't include JVM startup time (unless it
launches a separate JVM process and waits for it to do something).

On the other hand the time macro prints a string to stdout; for more
complex benchmarking (such as running several trials, ignoring the
first few which weren't JITted yet and dropping outliers and then
averaging the rest) you want something that discards the timed
expression's return value and returns the time itself. It's not hard
to slap something like that together with defmacro.

> Also, do you happen to know of a good reference for how/when the JVM
> does JIT compilation?

No, sorry.

Generally, running a loop a few times will JIT all the heavy number
crunching in it, and then you'll get consistent, JIT-optimized times
if you run it a few more times.

-- 
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: Ok, so code is data...

2011-05-10 Thread Bill Robertson
Thanks for the feedback and pointers.



On May 10, 11:37 am, Timothy Baldridge  wrote:
> On Tue, May 10, 2011 at 10:26 AM, Jonathan Fischer Friberg <
>
> odysso...@gmail.com> wrote:
> > "How do I actually just load code w/o evaluating it?"
> > mostly when you want something like this, you want a macro. Manipulating
> > code is also done within macros.
>
> Yeah, I would consider the whole code-is-data thing to refer more to using
> macros than eval. For something fun to read up on this check out this
> tutorial:
>
> http://www.learningclojure.com/2010/09/clojure-faster-than-machine-co...
>
> Here the author goes and takes a tree data structure, and on-the-fly
> generates function that represents that structure. The result is some
> insanely fast code.
>
> Timothy

-- 
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: possibly non-intuitive behaviour of clojure.set/rename-keys and possible enhancement suggestion

2011-05-10 Thread Sunil S Nandihalli
I agree there are going to be problems with implementation as-well .. for
example if not all the keys in the current map are not in the kmap ... but
situation can be handled however, if we passed a function this is going to
be harder .. ..

On Wed, May 11, 2011 at 8:26 AM, Sunil S Nandihalli <
sunil.nandiha...@gmail.com> wrote:

> Hello everybody,
>  I tried to use clojure.set/rename-keys .. I kind of felt it has a little
> odd behaviour... for example ..
>
> (clojure.set/rename-keys {1 :a 2 :b 3 :c}  {1 2 2 3 3 4})
>
> returns
>
> {4 :a}
>
> I think a more reasonable behavior would be to have it return
>
> {2 :a 3 :b 4 :c}
>
> a further enhancement suggestion would be to accept a function instead of a
> map that way even the existing code will not break since the map already
> implements IFn
>
> a sample implementation which takes the above into account would be ..
>
> (defn rename-keys [mp kmap-fn]
>   (into {} (map (fn [[key val]] [(kmap-fn key) val]) mp)))
>
>
> Thanks,
> Sunil.
>

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

Re: A simulation of the Monty Hall Problem

2011-05-10 Thread Konrad Hinsen

On 10 May 2011, at 22:46, Ken Wesson wrote:


Interesting. It is, as I thought, very short with monads -- though
that version doesn't really use randomness, but instead enumerates all
the possibilities. You'd need slightly different monads to thread a
random bit-stream through instead of enumerating all the alternatives.


Indeed. What's nice about monads is that you can use the same problem  
specification for both approaches. Just change the monad name to  
switch between enumeration and Monte-Carlo simulation.


Konrad.

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