Is this a bug?(same key in map n times)

2010-03-08 Thread alux
Hi,

if I put the same keyword in a map literal multiple times, it seems to
stay there somehow:

user=> {:a 1 :a 2 :a 3}
{:a 1, :a 2, :a 3}

The first entry wins here.

user=> (def a {:a 1 :a 2 :a 3})
#'user/a
user=> (:a a)
1

Is this a bug, or a feature?

(tested with clojure 1.1 and yesterdays github-master)

regards, alux

-- 
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: Is this a bug?(same key in map n times)

2010-03-08 Thread Meikel Brandmeyer
Hi,

On Mar 8, 9:19 am, alux  wrote:

> if I put the same keyword in a map literal multiple times, it seems to
> stay there somehow:
>
> user=> {:a 1 :a 2 :a 3}
> {:a 1, :a 2, :a 3}

This is an optimisation for small maps: the keys are not checked for
uniqueness. One has to distinguish array maps from hash maps. Array
maps are like ye olde alists. For small maps this is faster than a
real hash map. Clojure uses array maps for small map literals (up to
eight keys, IIRC). Having duplicate keys in a map literal is usually
considered a bug.

There is a ticket however, that map literals rely to much on the
reader. So maybe there might be a change here, but I'm not up to date
with that discussion.

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: Is this a bug?(same key in map n times)

2010-03-08 Thread alux
Ah, up to 8, yes. Funny:

user=> (def a {:a 1 :a 2 :a 3 :a 4 :a 5 :a 6 :a 7 :a 8})
#'user/a
user=> a
{:a 1, :a 2, :a 3, :a 4, :a 5, :a 6, :a 7, :a 8}
user=> (def a {:a 1 :a 2 :a 3 :a 4 :a 5 :a 6 :a 7 :a 8 :a 9})
#'user/a
user=> a
{:a 9}

Thank you. a.

On 8 Mrz., 09:23, Meikel Brandmeyer  wrote:
> Hi,
>
> On Mar 8, 9:19 am, alux  wrote:
>
> > if I put the same keyword in a map literal multiple times, it seems to
> > stay there somehow:
>
> > user=> {:a 1 :a 2 :a 3}
> > {:a 1, :a 2, :a 3}
>
> This is an optimisation for small maps: the keys are not checked for
> uniqueness. One has to distinguish array maps from hash maps. Array
> maps are like ye olde alists. For small maps this is faster than a
> real hash map. Clojure uses array maps for small map literals (up to
> eight keys, IIRC). Having duplicate keys in a map literal is usually
> considered a bug.
>
> There is a ticket however, that map literals rely to much on the
> reader. So maybe there might be a change here, but I'm not up to date
> with that discussion.
>
> 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


Clojure for financial applications

2010-03-08 Thread jshore
Hi,

Its been 19 years since I last wrote anything serious in lisp (and
that was Scheme).  In the intervening time I've mostly used OO
languages, for lack of more practical functional ones.   There are now
a number of functional or hybrid functional languages available which
have become practical.   I am now trying to decide which language to
use.

Having been in the OO space for a long time I find struggling to
determine how to think about the following in regards to clojure:

Organization of large projects
--
I have 1/2 million lines of my own code. I'm sure a lot of this
compresses to something smaller in clojure.   This is manageable for
me in the OO setting because functions (methods) are bound to their
data (object) and in their own namespaces (classes).

I am aware of clojure multimethods, but still seems to me that I would
end up having a near flat space of thousands of functions.  This would
be hard to track and grok over time.

I see multimethods working very well for data structures, but not
offering much for distinct concepts.

Dealing with State
--
What follows is very domain specific.  I have a trading strategy which
takes a vector of market data as the market ticks.   In the OO
composition, the strategy has a large amount of state and the state
will differ from one strategy to the next.   For instance I would
have:

- some window of prior tick history or returns (a rolling matrix)
- positions
- performance tracking
- various matrices used in online statistical analysis (specific to
strategy)
- instances of other classes used in the analysis (specific to
strategy)
- finite state machine(s) to manage execution and other behavior

I can also have strategies that contain sub-strategies.A high-
level OO interface would look something like:

- method: tick (time, market-vector)
- method: ...
- property: positions (an object with various atributes and
collections)
- property: performance (an object with various attributes)
- property: ...

it sounds like the approach in Clojure would be to have a single large
data structure composed of the common stuff and the specific stuff
that gets passed around to functions.   Of course this is exactly what
happens in the OO world, except that the "class" framework allows one
to simplify the presentation (implementation) of this binding of
state.

Now I suppose the structure / obj equiv can be described as a function
with a let binding and a series of functions accessible through some
means that work on the closure.

As is current, the state of this strategy mutates with each tick.   I
can see theoretically that could be done immutably, seeing each tick
creating a partially modified new strategy state, in effect a
versioning trail of prior strategy states.   I'm not sure whether I
would need to bend over backwards to affect this though.

Conclusion
--
Wondering whether anyone has done something very complex in the algo
space or comparable so can get an idea of how this sort of stuff is
structured idiomatically.   I will also be concerned with performance
and memory use, as one of my strategies creates a few thousand sub-
strategies and/or also uses massive matrices that get updates from
tick to tick.

Thoughts?

Thanks

Jonathan

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

2010-03-08 Thread Terje Norderhaug

On Mar 6, 2010, at 5:27 PM, Sophie wrote:

Re: Emacs + Slime + paredit. I did not see Clojure listed as supported
for Slime and paredit. Do you know if:

- Can you do all Slime stuff in Clojure? evaluate, macro-expand,  
docs, etc?


You can do much of the SLIME stuff including the items listed,  
although the Clojure Swank server used with MCLIDE and emacs SLIME  
does not yet support *all* features implemented in the Common Lisp  
version. Inspection is still incomplete, and it lacks xref  
functionality such as listing the callers of a function.


-- Terje Norderhaug
  te...@in-progress.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: enclojure install killed netbeans 6.8

2010-03-08 Thread strattonbrazil
I am on Linux.  I have a 6.6 and a 6.8 directory in my .netbeans
folder.  6.6 still runs.  I have tried moving individual jars in and
out of that dir, but I still get the error.  I even moved the entire
6.8 dir and still get the same message.

On Mar 7, 10:25 am, Mark Nutter  wrote:
> On Fri, Mar 5, 2010 at 11:34 PM, strattonbrazil
>
>  wrote:
> > Has anyone had problems with netbeans starting with the enclojure
> > alpha?  I restarted my session and it died.  Now when I try to run
> > netbeans, it throws a classpath exception.  Can I remove this plugin
> > or am I missing something in my install?
>
> First of all, do you have the JavaSE module installed and activated in
> NetBeans? That's a piece that will definitely crash your IDE if you
> try running Clojure without it.
>
> If you need to uninstall the plugin in order to reinstall everything,
> and if you're under Linux, look in the .netbeans directory in your
> home directory for any file containing '*cloj*' and I think you'll see
> pretty quickly where the plugin files are.
>
> $ find ~/.netbeans -name '*cloj*' -print
>
> Not sure where the Windows equivalents live, but if you need help with
> that let me know and I can find out.
>
> Mark

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


Problem with simple gui

2010-03-08 Thread stefanmuenchow
I tried to build a Swing gui with Clojure and get an error I don't
understand. Perhaps it's more a swing-problem than a clojure-problem,
but I'm sure the solution is simple and someone here will know the
answer.

I have two frames, one serves as the main-window of the app and the
second one opens if a button is clicked. The purpose of the second one
is to give some status messages to the user while performing a complex
operation.

My problem is: I want to change a component (textbox or label) between
two function calls, but it doesn't work! When the second frame is
opened, it remains grey until all operations are finished. I built a
simple example to show the problem:

(ns guitest)

(import '(javax.swing JFrame JLabel JTextField JButton)
'(java.awt.event ActionListener)
'(java.awt GridLayout))

(defn complex-calc []
(Thread/sleep 2000)
(Math/random ))

(defn testgui []
  (let [frame (JFrame. "Test")
frame2 (JFrame. "Testwin")
temp-text (JTextField.)
convert-button (JButton. "Click!")]
(doto frame
  (.setLayout (GridLayout. 1 1 1 1))
  (.add convert-button)
  (.setSize 100 100)
  (.setVisible true))
(doto frame2
(.setLayout (GridLayout. 1 1 1 1))
  (.add temp-text)
  (.setSize 100 100))
(.addActionListener convert-button
  (proxy [ActionListener] []
(actionPerformed [evt]
  (.show frame2)
  (complex-calc)
  (.setText temp-text "Calc1")
  (complex-calc)
  (.setText temp-text "Calc2")
  (complex-calc)
  (.setText temp-text "Calc3"))
(testgui)

In this example  (.setText temp-text "...") shows no effect. What have
I done wrong?

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


bounded memoize

2010-03-08 Thread Eugen Dück
In many real applications (I guess that rules out fibonacci), memoize
will consume a lot of memory and create OutOfMemoryErrors sooner or
later. Often you want to only keep the latest items in the cache and
forget about older ones.

I've seen a variant of memoize that evicts items based on a time-to-
live bound in this group (http://groups.google.com/group/clojure/
browse_thread/thread/402c489f805ade94/ ).

And here's a variant that evicts elements when the size of the cache
exceeds some limit. In that case, the first item that was put in it
will be dissoc'ed. I'm using an array-map to accomplish this:

(defn bounded-memoize
  [f bound]
  (let [mem (atom (array-map))]
(fn [& args]
  (if-let [e (find @mem args)]
(val e)
(let [ret (apply f args)]
  (swap! mem #(let [new-mem (assoc % args ret)]
(if (> (count new-mem) bound)
  (dissoc new-mem (first (first new-mem)))
  new-mem)))
  ret)

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


Shared symbol context and lazy dataflows

2010-03-08 Thread drrobot
Hi All,

Timid coder here looking for some confirmation/advice on my
understanding of math and concurrent dataflow in Clojure before I dive
in.

Clojure looks like a great language and I am enthusiastic to try it
out on some light numerics programs. Basically, what I have now is
around a hundred equations with a dependency tree based on themselves
and 50 'input' variables in a pretty much standard time-stepped
simulation with lots of numerical integration, root-finding, and a
natural grouping of information into objects (not necessarily
synchronous with the world, however) . Presently, I already have the
math expressed in s-expression format, thanks to Maxima's handy
ability to export any equation as a lisp expression, thus saving a
great deal of time and typing after deriving things symbolically.

Previously when coding in common lisp, I tended to naively push my
data into an object, use with-slots to expose what variables I needed,
and then try to manage all my mutable state in a small number function
with a lot of SETF's that compute things in the proper order. Of
course, it was often tempting to recompute values on the fly than
figure out the mutable state dependency tree again, so there was lots
of wasted computation. At certain timesteps, I did not need to compute
the entire dependency tree at all, only a handful of values, yet I had
no easy way of doing this.

After I heard somebody (Rich Hickey?) say "Mutable data is the new
spaghetti code" I felt a tremendous sense of spiritual agreement and
decided to rethink how I would code my next simulation to exploit
clojure's useful idioms for concurrency and laziness. Dataflow
approaches seems natural for managing a tree of dependent expressions,
but I still was curious as to how people were managing lots of shared
symbols in an aesthetically clean way outside of that framework.

So, specifically I am searching for wisdom about:

1. How should I manage the shared symbolic context of a large number
of variables shared among multiple functions? (Is there an idiomatic
clojure way that is better than packaging all the stuff in a hashmap
and reimplementing something like common lisp's WITH-SLOTS?)

2. Am I right in understanding that contrib.dataflow evaluates eagerly
by default? Is there a rationale for this, or just nobody yet has
wanted (some or all) cells to be lazy by default?

3. Has anybody played around with adding concurrency to dataflow code
so that independent branches of the dependancy tree may be computed
simultaneously?

Happy hacking,


Ivar


;;; Some brain farts as I think about managing lots of symbols and
dataflow...
;;
;; Using the quadratic equation here purely as an example. Curious to
think about how
;; each of these approaches aesthetically scale to a blocks of math
with ~50 symbols.

(use 'clojure.contrib.math)   ;; for sqrt

(def myhash {:a 1 :b 5 :c 6 :x nil})

;; The CL way of binding symbols was reasonably convenient,
;;   but a with-slots equivalent doesn't seem to exist in clojure?
(defn some-eqn0 [obj]
  (with-slots [a b c] obj
(/ (+ (- b) (sqrt (- (* b b) (* 4 a c
   (* 2 a

;; Silly way #1
(defn some-eqn1 [obj]
  (/ (+ (- (:b obj)) (sqrt (- (* (:b obj) (:obj b)) (* 4 (:a obj) (:c
obj)
 (* 2 (:a obj

;; Silly way #2
(defn some-eqn2 [obj]
  (let [a (:a obj)
b (:b obj)
c (:c obj)]
(/ (+ (- b) (sqrt (- (* b b) (* 4 a c
   (* 2 a

;; Do things such as hash-map comprehensions exist? Should they? ;-)
(defn some-eqn3 [obj]
  (let [{:a a :b b :c c} obj]
(/ (+ (- b) (sqrt (- (* b b) (* 4 a c
   (* 2 a



;; What would using contrib.dataflow look like?
(use 'clojure.contrib.dataflow)

(def myobj {:df (build-dataflow [(cell :source a 1)
 (cell :source b 5)
 (cell :source c 6)])
:other "Maybe other, non-cell object data here too."})

(defn add-some-eqn4 [obj]
  (let [df (:df obj)]
(add-cells df [(cell x (do (. Thread sleep 3000)
   (println "X was (re)computed")
   (/ (+ (- ?b) (sqrt (- (* ?b ?b) (* 4 ?a ?c
  (* 2 ?a))
   ))
   (cell y (* -1 ?x))])))


(add-some-eqn4 myobj)
(print-dataflow (:df myobj))

;; Playing around with watchers on x and y, it seems like dataflow
;;  is not lazy by default, and certainly not lazy with watchers
(get-value (:df myobj) 'x)   ;; returns -2
(get-value (:df myobj) 'y)   ;; returns 2

(add-cell-watcher (get-cell (:df myobj) 'x)
nil
(fn [key cell o n]
  (printf "x: %s -> %s\n" o n)))

(add-cell-watcher (get-cell (:df myobj) 'y)
nil
(fn [key cell o n]
  (printf "y: %s -> %s\n" o n)))

(update-values (:df myobj)
   {'a  1
'b -6
'c  9})

(update-values (:df myobj)

apply-ing Java methods

2010-03-08 Thread Michael Gardner
Given a Java instance 'store' with a .connect method that takes a host, port, 
user and password, and given a hash-map 'config' with corresponding keyword 
values, I wanted to do something like:

(apply .connect store (map config [:host :port :user :password]))

rather than:

(.connect store (:host config) (:port config) (:user config) (:password config))

but of course the former doesn't work, since .connect isn't a regular function. 
Is there a concise way to accomplish this?

-Michael

-- 
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: apply-ing Java methods

2010-03-08 Thread Volkan YAZICI
See memfn in Clojure API docs.

-- 
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: Problem with simple gui

2010-03-08 Thread Meikel Brandmeyer
Hi,

On Mar 8, 12:44 am, stefanmuenchow  wrote:

>     (.addActionListener convert-button
>       (proxy [ActionListener] []
>         (actionPerformed [evt]
>           (.show frame2)
>           (complex-calc)
>           (.setText temp-text "Calc1")
>           (complex-calc)
>           (.setText temp-text "Calc2")
>           (complex-calc)
>           (.setText temp-text "Calc3"))

Your actionPerformed method is called on the event dispatch thread of
Swing. You do your calculation but block the thread until it is
complete. So the GUI never gets the chance to update. You have to fire
off a background thread, which does the computation and update the GUI
from there via (SwingUtilies/invokeLater (fn [] (.setText temp-text
"Calc1"))). Have a look at clojure.contrib.swing-utils for convenience
wrappers for Swing.

Reading a little background on how Swing works is also a good idea.

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: I confused myself. How can I solve this simple problem?

2010-03-08 Thread James Reeves
I'd have thought the idiomatic way of doing this would be:

(ns my-namespace
  (:refer-clojure :exclude [println]))

(defn println [s]
  (clojure.core/println s)
  (clojure.core/println "tacked on"))

- James

On 8 March 2010 03:57, CuppoJava  wrote:
> So I just stumbled across this bug in my code, and it arose very
> innocently so I wonder if anyone has an elegant solution to this
> problem.
>
> There is already a function called (println).
>
> Now I would like to write another version of it, which just tacks some
> stuff at the end of what it normally does:
>
> (defn myprintln [str]
>  (println str)
>  (println "tacked on"))
>
> And now, I would like to run some code using my version of println,
> rather than the original.
>
> (binding [println myprintln]
>  (println "Some code"))
>
> Do you see the bug?
>
> Is there an elegant solution to this problem? I'm working around it by
> saving the original println in another variable before creating
> myprintln, but this isn't very clean.
>
> Thanks for your help
>  -Patrick
>
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clojure@googlegroups.com
> Note that posts from new members are moderated - please be patient with your 
> first post.
> To unsubscribe from this group, send email to
> clojure+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en

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


Re: Clojure for financial applications

2010-03-08 Thread Stuart Halloway

Here are a few random thoughts:

(1) You still have namespaces in Clojure, which correspond 1-1 with  
Java packages.


(2) Multimethods are open, so you add to them from multiple places.

(3) Protocols (a 1.2) feature give you an approach to your strategies  
that is more flexible than OO interfaces.


(4) Where performance matters, you can avoid reflection with type  
hints. If you really have to get to the metal, you can use macros to  
work with mutable arrays (ugh), make sure math is unboxed, and control  
inlining.


Stu


Hi,

Its been 19 years since I last wrote anything serious in lisp (and
that was Scheme).  In the intervening time I've mostly used OO
languages, for lack of more practical functional ones.   There are now
a number of functional or hybrid functional languages available which
have become practical.   I am now trying to decide which language to
use.

Having been in the OO space for a long time I find struggling to
determine how to think about the following in regards to clojure:

Organization of large projects
--
I have 1/2 million lines of my own code. I'm sure a lot of this
compresses to something smaller in clojure.   This is manageable for
me in the OO setting because functions (methods) are bound to their
data (object) and in their own namespaces (classes).

I am aware of clojure multimethods, but still seems to me that I would
end up having a near flat space of thousands of functions.  This would
be hard to track and grok over time.

I see multimethods working very well for data structures, but not
offering much for distinct concepts.

Dealing with State
--
What follows is very domain specific.  I have a trading strategy which
takes a vector of market data as the market ticks.   In the OO
composition, the strategy has a large amount of state and the state
will differ from one strategy to the next.   For instance I would
have:

- some window of prior tick history or returns (a rolling matrix)
- positions
- performance tracking
- various matrices used in online statistical analysis (specific to
strategy)
- instances of other classes used in the analysis (specific to
strategy)
- finite state machine(s) to manage execution and other behavior

I can also have strategies that contain sub-strategies.A high-
level OO interface would look something like:

- method: tick (time, market-vector)
- method: ...
- property: positions (an object with various atributes and
collections)
- property: performance (an object with various attributes)
- property: ...

it sounds like the approach in Clojure would be to have a single large
data structure composed of the common stuff and the specific stuff
that gets passed around to functions.   Of course this is exactly what
happens in the OO world, except that the "class" framework allows one
to simplify the presentation (implementation) of this binding of
state.

Now I suppose the structure / obj equiv can be described as a function
with a let binding and a series of functions accessible through some
means that work on the closure.

As is current, the state of this strategy mutates with each tick.   I
can see theoretically that could be done immutably, seeing each tick
creating a partially modified new strategy state, in effect a
versioning trail of prior strategy states.   I'm not sure whether I
would need to bend over backwards to affect this though.

Conclusion
--
Wondering whether anyone has done something very complex in the algo
space or comparable so can get an idea of how this sort of stuff is
structured idiomatically.   I will also be concerned with performance
and memory use, as one of my strategies creates a few thousand sub-
strategies and/or also uses massive matrices that get updates from
tick to tick.

Thoughts?

Thanks

Jonathan

--
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: Shared symbol context and lazy dataflows

2010-03-08 Thread Adrian Cuthbertson
Hi Ivar,

> ;; Do things such as hash-map comprehensions exist? Should they? ;-)
> (defn some-eqn3 [obj]
> (let [{:a a :b b :c c} obj]
>   (/ (+ (- b) (sqrt (- (* b b) (* 4 a c
>  (* 2 a

At least a partial stab at some of your questions...

(def myhash {:a 1 :b 5 :c 6 :x nil})

(defn some-eqn3 [obj]
  (let [{:keys [a b c]} obj]
(/ (+ (- b) (Math/sqrt (- (* b b)
 (* 4 a c (* 2 a

(some-eqn3 myhash)
-2.0

Have a look at http://clojure.org/special_forms, regarding
destructured binding with let.

Still pondering the rest of your post :-).

-Rgds, Adrian.

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

2010-03-08 Thread Stuart Sierra
On Mar 8, 9:29 am, Stuart Halloway  wrote:
> (1) You still have namespaces in Clojure, which correspond 1-1 with  
> Java packages.

More or less.  The namespace "foo.bar.baz" is actually a Class named
"baz" in the package "foo.bar".

-SS

-- 
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: apply-ing Java methods

2010-03-08 Thread Adrian Cuthbertson
Maybe just;
(let [{:keys host port user password} config]
   (.connect store host port user password))

On Mon, Mar 8, 2010 at 9:47 AM, Michael Gardner  wrote:
> Given a Java instance 'store' with a .connect method that takes a host, port, 
> user and password, and given a hash-map 'config' with corresponding keyword 
> values, I wanted to do something like:
>
> (apply .connect store (map config [:host :port :user :password]))
>
> rather than:
>
> (.connect store (:host config) (:port config) (:user config) (:password 
> config))
>
> but of course the former doesn't work, since .connect isn't a regular 
> function. Is there a concise way to accomplish this?
>
> -Michael
>
> --
> 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: apply-ing Java methods

2010-03-08 Thread Meikel Brandmeyer

> (let [{:keys host port user password} config]
>    (.connect store host port user password))

module missing [] around the keys. ;)

And of course this does not work for non-constant keylists. However I
think in the majority of cases the list should be constant.

-- 
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: apply-ing Java methods

2010-03-08 Thread Adrian Cuthbertson
There's a fundamental law of nature that says; if you don't try it at
the repl before posting, you WILL get it wrong :-).

On Mon, Mar 8, 2010 at 5:19 PM, Meikel Brandmeyer  wrote:
>
>> (let [{:keys host port user password} config]
>>    (.connect store host port user password))
>
> module missing [] around the keys. ;)
>
> And of course this does not work for non-constant keylists. However I
> think in the majority of cases the list should be constant.
>
> --
> 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


monkeypatching in clojure

2010-03-08 Thread cageface
I've been reading about some of the clever things Scala does to allow
safer monkeypatching and this started me thinking about Clojure's
approach to this technique. Maybe I'm overlooking something but is
this just a non-issue in Clojure? Since functions aren't attached to
objects it seems to me you can just define a new function on an
existing type or another method clause in a multi or if necessary.
Maybe you have to be careful about messing around with an established
type hierarchy or multi definition but it doesn't seem to me you need
any special machinery to make this work, right?

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

2010-03-08 Thread Volkan YAZICI
On Mar 7, 6:35 pm, jshore  wrote:
> Wondering whether anyone has done something very complex in the algo
> space or comparable so can get an idea of how this sort of stuff is
> structured idiomatically.   I will also be concerned with performance
> and memory use, as one of my strategies creates a few thousand sub-
> strategies and/or also uses massive matrices that get updates from
> tick to tick.

Recently, I have been using Clojure to implement a model we introduced
related with hypergraphs. For this purpose, most of the heavy work is
done in C libraries (JNA is quite easy to use) and pure Java libraries
(thanks to Java interop.), hence no problems so far. And Clojure
supplies a cool interface to pure Java arrays (int-array, float-array,
etc.) in a relatively reasonable way. But AFAIK, there is no way to
use multi-dimensional arrays (i.e. int[][]) asis in Clojure. (I might
be missing something about this, folks told that there are no real
multi-dimensional arrays in Java either.) And it is a PITA to write
imperative code in Clojure, and most algorithms are written in an
imperative style. (You know, "for (i = k; i < c; i++) ..." loops
everywhere.) But it is not hard to implement a few utility macros for
yourself. (Hrm... A contrib library would be really awesome.)
Moreover, Clojure data structures and their J2SE suplements really
eases your work -- think all industrial quality set, list, priority
queue, etc. implementations. To sum up, I must admit that Clojure (or
more generally speaking, Lisp) stands as a quite effective tool to
bridge between algorithms.


Regards.

-- 
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: monkeypatching in clojure

2010-03-08 Thread Konrad Hinsen

On 8 Mar 2010, at 17:39, cageface wrote:


this just a non-issue in Clojure? Since functions aren't attached to
objects it seems to me you can just define a new function on an
existing type or another method clause in a multi or if necessary.
Maybe you have to be careful about messing around with an established
type hierarchy or multi definition but it doesn't seem to me you need
any special machinery to make this work, right?


Right. Assuming you stay in the Clojure world (and don't want to  
monkey-patch Java classes, which I am not sure is possible), you don't  
need any special machinery nor is it really a technique worth putting  
a name on.


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


Job Fair

2010-03-08 Thread Jeff Straszheim
Howdy

The company where I work, Velocitude, is hiring Clojure developers.  We will
be attending a job fair on March 11 in Cambridge, MA.  Details are here:

 http://www.velocitude.com/info/2010/03/01/

Specific Clojure knowledge is not required.  Anyone w/ a LISP or FP
background would be an idea candidate.

-- 
Jeffrey Straszheim

VELOCITUDE - Software Engineer

800 East Cypress Creek Road, Suite 300
Fort Lauderdale, FL 4 USA

Direct: 954-652-2243
Toll Free: 877-84-MOBILE (66245) ext. 2243

-- 
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: apply-ing Java methods

2010-03-08 Thread Nurullah Akkaya
On Mon, Mar 8, 2010 at 9:47 AM, Michael Gardner  wrote:
> Given a Java instance 'store' with a .connect method that takes a host, port, 
> user and password, and given a hash-map 'config' with corresponding keyword 
> values, I wanted to do something like:
>
> (apply .connect store (map config [:host :port :user :password]))
>
> rather than:
>
> (.connect store (:host config) (:port config) (:user config) (:password 
> config))
>
> but of course the former doesn't work, since .connect isn't a regular 
> function. Is there a concise way to accomplish this?
>
> -Michael
>
> --
> 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
>

Using this,

http://paste.lisp.org/display/67182

would allow you to do,

(let [config {:str "fred" :beg 2 :end 3}]
  (apply (jfn 'substring) (map config [:str :beg :end])))

--
Nurullah Akkaya
http://nakkaya.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


Visual Studio plugin

2010-03-08 Thread Eric Thorsen
Is there/is anyone working on/is anyone interested in working on a
Visual Studio plugin for a Clojure REPL for clojureCLR?

My company might be interested in sponsoring this work.

Thanks!
eric

-- 
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: monkeypatching in clojure

2010-03-08 Thread cageface
The one potential downside I've seen to the function-oriented approach
is that you often wind up encoding the argument type in the function
name.

For example, if I write a library to manipulate SQL databases I might
write a lot of functions that start with db- and resultset- and
statement- etc. So you wind up writing a kind of type specifier in
your code anyway.

-- 
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: monkeypatching in clojure

2010-03-08 Thread Raoul Duke
On Mon, Mar 8, 2010 at 11:40 AM, cageface  wrote:
> The one potential downside I've seen to the function-oriented approach
> is that you often wind up encoding the argument type in the function
> name.

uh, hey, wait a second, please note that is about type checking, not
about OO vs. FP!

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: monkeypatching in clojure

2010-03-08 Thread cageface
On Mar 8, 11:48 am, Raoul Duke  wrote:
> uh, hey, wait a second, please note that is about type checking, not
> about OO vs. FP!

Yeah I'm not talking about OO vs FP but about the function-centric
approach that Lisps and languages like Haskell take as opposed to the
object, or noun-centric approach of languages like Java or Ruby.

Intead of db.statement, db.query, db.results etc you have db-
statement, db-query, db-results.

-- 
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: monkeypatching in clojure

2010-03-08 Thread Richard Newman

Yeah I'm not talking about OO vs FP but about the function-centric
approach that Lisps and languages like Haskell take as opposed to the
object, or noun-centric approach of languages like Java or Ruby.


Interesting reading from 2006:

http://steve-yegge.blogspot.com/2006/03/execution-in-kingdom-of-nouns.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


Re: monkeypatching in clojure

2010-03-08 Thread cageface
On Mar 8, 12:47 pm, Richard Newman  wrote:
> Interesting reading from 2006:
>
> http://steve-yegge.blogspot.com/2006/03/execution-in-kingdom-of-nouns...

Yeah I was just reading that this weekend actually.

To be clear, I'm not knocking the verb-oriented approach. I guess I
just think it's interesting that even in dynamically typed languages
or heavily type-inferred languages like Haskell you still winding up
using type prefixes to some extent. For example, my Ocaml and Haskell
experience is very limited but I understand that record field
accessors in both languages share a common namespace, so in practice
you tend to give them type prefixes to prevent clashes. And I guess we
do the same thing in Clojure. We have a repeat function in core and in
the contrib.string module so it's good practice to qualify the import
of contrib.string and use string/repeat.

-- 
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: I confused myself. How can I solve this simple problem?

2010-03-08 Thread CuppoJava
Thanks for the responses. The let form is suitable enough as a
workaround.

The thing that really bothered me about this is because separation of
responsibilities breaks down somewhat.

Imagine I create a library of different println's for use in different
circumstances.

(defn fancy-println [] )
(defn fast-println [] )
(defn binary-println [] )
(defn memoized-println [] )


The user must beware when using these because:
(binding [println fast-println]
  do stuff... )

fast-println better not call the original println! Or else you get a
StackOverflowError.

And yet, the writer of the library shouldn't have to be aware that the
user *might* bind fast-println to println.
And the user of the library shouldn't have to be aware of the
implementation details of fast-println to want to bind println.

  -Patrick

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


symbolmacro tests fail under 1.3.2-SNAPSHOT

2010-03-08 Thread Mark Derricutt
I posted this the other day to the clojure-maven-plugin list but had
no response, so thought I'd ask here.

I'm wanting to push out a new release of the maven plugin, but I
noticed the other day that one of clojure-contrib's tests failed when
running with it:

Testing clojure.contrib.test-macro-utils
FAIL in (symbol-test) (run-test4136966802729217824.clj:41)
expected: (= (macroexpand (quote (with-symbol-macros (+ 1 sum-2-3
(quote (do (+ 1 (plus 2 3)
  actual: (not (= (do (+ 1 sum-2-3)) (do (+ 1 (plus 2 3)
FAIL in (symbol-test) (run-test4136966802729217824.clj:41)
expected: (= (macroexpand (quote (macrolet [(plus [a b]
(clojure.core/seq (clojure.core/concat (clojure.core/list (quote
clojure.core/+)) (clojure.core/list a) (clojure.core/list b] (+ 1
sum-2-3 (quote (do (+ 1 (clojure.core/+ 2 3)
  actual: (not (= (do (+ 1 sum-2-3)) (do (+ 1 (clojure.core/+ 2 3)

I know we've changed around how clojure:test runs under 1.3.2-SNAPSHOT
and I half expected the build to fail, but this wasn't the failure I
expected to see.  Anyone have any idea whats going on here?  It has me
stumped at the moment...

Is this a problem with clojure-contrib, or how the plugin is
bootstrapping things?

mark

--
Pull me down under...

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


Hiccup 0.2.1 release

2010-03-08 Thread James Reeves
Hiccup is a library for generating a string of HTML from a tree of
Clojure vectors. It supports dynamically generating HTML, but since
0.2.0 it statically compiles and optimizes where possible. This makes
Hiccup very, very fast. Under some circumstances, it even outperforms
clj-html.

Here are some recent benchmarks for generating 100,000 HTML pages:

clj-html - 4936ms
hiccup   - 1455ms
hiccup (with type hints) - 1252ms
str with string literals - 1216ms

I've put the benchmark code and more detailed results up as a gist:
http://gist.github.com/326028

In the interests of fairness I should note that clj-html performs 17%
faster than Hiccup if we replace this:

[:ul.times-table
  (for [n (range 1 13)]
[:li n " * 9 = " (* n 9)])]

With this somewhat lengthier code:

[:ul.times-table
  (apply str
(for [n (range 1 13)]
  (clj-html/html [:li n " * 9 = " (* n 9)])]

Hiccup generates this optimization itself, which is probably why it's
out-performing clj-html in this case.

The current released version of Hiccup is 0.2.1. I was planning on
making a post at 0.2.0, but by the time I got around to it, I had
enough commits to make another patch version.

- 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: bounded memoize

2010-03-08 Thread Michał Marczyk
On 8 March 2010 05:31, Eugen Dück  wrote:
> And here's a variant that evicts elements when the size of the cache
> exceeds some limit. In that case, the first item that was put in it
> will be dissoc'ed. I'm using an array-map to accomplish this:

I don't think this will work as you expect it to. There are two reasons:

(1) Array maps are transparently converted to hash maps when they grow
beyond a certain size:

user> (class (array-map :a 1 :b 2 :c 3 :d 4 :e 5 :f 6 :g 7 :h 8))
clojure.lang.PersistentArrayMap
user> (class (assoc (array-map :a 1 :b 2 :c 3 :d 4 :e 5 :f 6 :g 7 :h 8) :i 9))
clojure.lang.PersistentArrayMap
user> (class (assoc (array-map :a 1 :b 2 :c 3 :d 4 :e 5 :f 6 :g 7 :h
8) :i 9 :j 10))
clojure.lang.PersistentHashMap

(2) More importantly, if you dissoc the first key from an array map,
then assoc a new key onto the array map, the newly assoc key will take
the first slot:

user> (first (assoc (dissoc (array-map :a 1 :b 2 :c 3) :a) :d 4))
[:d 4]

I'd suggest a vector instead; they're countable in constant time and
you can use, say, conj and rest for add to end of queue / eject from
front of queue.

The idea is certainly a good one, though, so with the above mentioned
issues fixed, this will be a nice utility function. Thanks for
sharing!

Sincerely,
Michał

-- 
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: bounded memoize

2010-03-08 Thread André Ferreira
On 8 mar, 23:22, Michał Marczyk  wrote:
> I'd suggest a vector instead; they're countable in constant time and
> you can use, say, conj and rest for add to end of queue / eject from
> front of queue.

Conj adds to the end of a vector in constant time, but rest will not
return another vector, but a sequence. Converting that sequence into
another vector will take O(n). If you want queueing behaviour, you
should use a clojure.lang.PersistentQueue:
(-> clojure.lang.PersistentQueue/EMPTY (conj 5) (conj 7) pop (conj 8)
peek)

-- 
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: bounded memoize

2010-03-08 Thread Michał Marczyk
2010/3/9 André Ferreira :
> Conj adds to the end of a vector in constant time, but rest will not
> return another vector, but a sequence. Converting that sequence into
> another vector will take O(n). If you want queueing behaviour, you
> should use a clojure.lang.PersistentQueue:
> (-> clojure.lang.PersistentQueue/EMPTY (conj 5) (conj 7) pop (conj 8)
> peek)

rest is definately a bad choice, thanks for pointing that out! In this
particular case, it could be replaced with subvec (called with two
arguments: (subvec v 1)) which the docs describe as being "O(1) and
very fast", but since it is precisely the semantics of a queue that I
had in mind, your suggestion is probably better.

Sincerely,
Michał

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


Re: Hiccup 0.2.1 release

2010-03-08 Thread Wilson MacGyver
where is the library? I followed the link
http://gist.github.com/326028

but it says it's been deleted.

On Mon, Mar 8, 2010 at 8:44 PM, James Reeves  wrote:
> Hiccup is a library for generating a string of HTML from a tree of
> Clojure vectors. It supports dynamically generating HTML, but since
> 0.2.0 it statically compiles and optimizes where possible. This makes
> Hiccup very, very fast. Under some circumstances, it even outperforms
> clj-html.
>
> Here are some recent benchmarks for generating 100,000 HTML pages:
>
> clj-html                 - 4936ms
> hiccup                   - 1455ms
> hiccup (with type hints) - 1252ms
> str with string literals - 1216ms
>
> I've put the benchmark code and more detailed results up as a gist:
> http://gist.github.com/326028
>
> In the interests of fairness I should note that clj-html performs 17%
> faster than Hiccup if we replace this:
>
> [:ul.times-table
>  (for [n (range 1 13)]
>    [:li n " * 9 = " (* n 9)])]
>
> With this somewhat lengthier code:
>
> [:ul.times-table
>  (apply str
>    (for [n (range 1 13)]
>      (clj-html/html [:li n " * 9 = " (* n 9)])]
>
> Hiccup generates this optimization itself, which is probably why it's
> out-performing clj-html in this case.
>
> The current released version of Hiccup is 0.2.1. I was planning on
> making a post at 0.2.0, but by the time I got around to it, I had
> enough commits to make another patch version.
>
> - 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



-- 
Omnem crede diem tibi diluxisse supremum.

-- 
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: apply-ing Java methods

2010-03-08 Thread Michał Marczyk
I find #(.connect %) to be the most pleasing form.

Sincerely,
Michał

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


Re: Hiccup 0.2.1 release

2010-03-08 Thread James Reeves
On Mar 9, 3:27 am, Wilson MacGyver  wrote:
> where is the library? I followed the linkhttp://gist.github.com/326028
>
> but it says it's been deleted.

D'oh! Telling people where the library is would be kinda a good idea.

The library can be found on GitHub:
http://github.com/weavejester/hiccup

And it's also uploaded to Clojars:
http://clojars.org/hiccup

The gist I somehow messed up and got the wrong link. The right link
is:
http://gist.github.com/326055

My apologies for leaving out this information. I suspect I shouldn't
post in the early hours of the morning.

- 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


Concurrent requests for Java's HTTP Server

2010-03-08 Thread Abhay
Hi All,

I have created a small app using the java's base HTTP server shiped
with JDK.

I observed that the HTTP requests cannot execute simultaneously. For
eg, my request 1 is blocking my request 2. As soon as I unblock the
request, other request gets completed.

Does this HTTP Server provides concurrency?

If yes, Its very difficult to believe that a HTTP severs does not
provide concurrency :(

~Abhay


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


Two potential map-util functions

2010-03-08 Thread Luka
I'm aware of clojure-dev group, but being new to clojure, i thought I
should start here first.
These are two methods I've needed in my project and I didn't found
better way to accomplish these tasks. First function returns vector of
leaf nodes in a map, and second dissociates in nested map. First
question would be is there already a way in clojure to do this? If
not, does these kind of functions deserve place in contrib, and third,
what should I do to make this functions production ready, as in if
they were really part of contrib?

(defn leafs [m]
  (loop [vs (vals m), accum []]
(if (empty? vs)
  accum
  (let [v (first vs)]
(if (map? v)
  (recur (rest vs) (into accum (leafs v)))
  (recur (rest vs) (conj accum v)))

(defn dissoc-in [m v]
  (let [len (count v)
cur (get-in m v)]
(cond
  (nil? cur) m
  (= len 0) m
  (= len 1) (dissoc m (first v))
  true (update-in m (butlast v) #(dissoc % (last v))

Other thing I would like to ask is how can I see what is different in
40 github clones of clojure.contrib without clicking on every clone?

Cheers,
Luka

-- 
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: apply-ing Java methods

2010-03-08 Thread Michael Gardner
On Mar 8, 2010, at 6:50 AM, Volkan YAZICI wrote:

> See memfn in Clojure API docs.

Thanks for the tip. After some experimentation:

(apply (memfn connect a b c d) store (map config [:host :port :user :password]))

but why does memfn require these "dummy" arguments?

-- 
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: apply-ing Java methods

2010-03-08 Thread Michael Gardner
On Mar 8, 2010, at 9:16 AM, Adrian Cuthbertson wrote:

> Maybe just;
> (let [{:keys host port user password} config]
>   (.connect store host port user password))

Having to repeat the variable names rather defeats the purpose, since this 
version is longer than the original and still feels redundant to me. There's a 
similar issue with memfn (see my reply to Volkan), though I can't see why that 
should be the case.

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

2010-03-08 Thread Jonathan Shore
Thanks for the reply.

I could be wrong, but namespaces just provide a package / require mechanism, 
such that only required functionality is in the namespace of some code.This 
seems to be more of a mapping to the package / import mechanism of java or 
something similar in ruby or python.   However, though it hides functions from 
polluting global namespace, I would say it is not nearly equivalent to a 
"class" in the traditional sense.   I may be misunderstanding something here.

Now OO may be antithetical to the traditional way of using lisp, however, I see 
myself needing something close to an OO-style mapping for part of what I do.   
Currently my trading strategies have large and disparate state contained in 
each strategy.   I'm not really sure how can effeciently map into clojure.   So 
for instance if I have 50 different pieces of state, some of which will be:

- matrices
- numbers
- vectors
- booleans
- FSMs

How would I encapsulate this into a data structure to be passed into functions 
efficiently?I could use a map of symbols to various structures, but that 
would be inefficient in access and memory.   I could bind into a closure but 
then how do I evolve the closure.   So for instance could do (excuse 
non-idiomatic usage and/or suggest better):

(defn foo-bar-strategy [...]
(let [
valA ...
valB ...
valC ...]
(fn [...] ...)))

Where the returned function is an accessor into the state, perhaps also 
including functions operating on that state.   In this setup not sure how state 
evolves though.   If all access in and out goes through delegation is going to 
be costly (remember that this is a very performance sensitive application).

I think this application stresses the pure functional approach in that it:

- does small computations at high frequency
- pushes a huge amount of data through at each timestep
- currently can take hours or days to run with mutation.

My worry is that without very careful design, in clojure could end up being 
multiples slower ...


On Mar 8, 2010, at 10:14 AM, Stuart Sierra wrote:

> On Mar 8, 9:29 am, Stuart Halloway  wrote:
>> (1) You still have namespaces in Clojure, which correspond 1-1 with  
>> Java packages.
> 
> More or less.  The namespace "foo.bar.baz" is actually a Class named
> "baz" in the package "foo.bar".
> 
> -SS
> 
> -- 
> 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


Clojure Implementation issues that may affect performance?

2010-03-08 Thread Jonathan Shore
Hi,

I was stepping through a very simply test (not meant to be performant) and 
noticed the following:

(ns test.performance.fibonachi)

(defn fib [a]
  (if (< a 2) 
a 
(+ (fib (- a 1)) (fib (- a 2)

(fib 45)

Stepping into (if ...) noticed that (< a 2) called into the following Java code:

>From Numbers.java:

static public boolean lt(Object x, Object y){
return ops(x).combine(ops(y)).lt((Number)x, (Number)y);
}

Stepping further into ops(x):

static Ops ops(Object x){
Class xc = x.getClass();

if(xc == Integer.class)
return INTEGER_OPS;
else if(xc == Double.class)
return DOUBLE_OPS;
else if(xc == Float.class)
return FLOAT_OPS;
else if(xc == BigInteger.class)
return BIGINTEGER_OPS;
else if(xc == Long.class)
return LONG_OPS;
else if(xc == Ratio.class)
return RATIO_OPS;
else if(xc == BigDecimal.class)
return BIGDECIMAL_OPS;
else
return INTEGER_OPS;
}


Yow!, if this is very inefficient!   If this sort of code is being evaluated 
all of the time for arithmetic the performance is going to suffer 
substantially.I do a lot of fundamental operations on arrays in my 
numerical work at high frequency, so extra overhead will mean that the 
algorithm in some factor x slower. (again I recognize this is not an issue for 
95% of the applications out there).Is there a special optimisation that 
avoids the dispatch?   I note that type annotation does not seem to avoid this 
(or at least what I understand of annotation).

(defn fib [#^Integer a]
  (if (< a 2) 
a 
(+ (fib (- a 1)) (fib (- a 2)

I'm just learning, so I may have overlooked something that mitigates or 
otherwise avoids dispatch.

Jonathan

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

2010-03-08 Thread Jonathan Shore

On Mar 8, 2010, at 11:51 AM, Volkan YAZICI wrote:

> On Mar 7, 6:35 pm, jshore  wrote:
>> Wondering whether anyone has done something very complex in the algo
>> space or comparable so can get an idea of how this sort of stuff is
>> structured idiomatically.   I will also be concerned with performance
>> and memory use, as one of my strategies creates a few thousand sub-
>> strategies and/or also uses massive matrices that get updates from
>> tick to tick.
> 
> Recently, I have been using Clojure to implement a model we introduced
> related with hypergraphs. For this purpose, most of the heavy work is
> done in C libraries (JNA is quite easy to use) and pure Java libraries
> (thanks to Java interop.), hence no problems so far. And Clojure

It is a shame to have to dive down to Java or native (perhaps with the 
exception of some of the massive numerical libraries one does not want to 
rewrite).   I'm hoping to use Clojure or something like clojure as a complete 
replacement.   On the .NET platform F# looks to have a performance profile that 
allows one to write exclusively in the F# variant of Ocaml.My issue is that 
I am not on a MS platform.


> supplies a cool interface to pure Java arrays (int-array, float-array,
> etc.) in a relatively reasonable way. But AFAIK, there is no way to
> use multi-dimensional arrays (i.e. int[][]) asis in Clojure. (I might
> be missing something about this, folks told that there are no real

Well, I imagine one could at least write matrix-style access in terms of a 
single dim array (as is done in many languages).  One could write a set of 
functions that work on a binding of an array with dimension.   

For the sake of understanding, I'm not yet clear on how one *efficiently* binds 
multiple "pieces" of state together in clojure.   How would one create a simple 
matrix for example where I want to bind dimension and a float-array into a 
tightly bound structure.   I can see that a assoc / map could be used (but has 
undesireable overhead) or perhaps a closure with the array, dimensions, and a 
returning function for access?


> multi-dimensional arrays in Java either.) And it is a PITA to write
> imperative code in Clojure, and most algorithms are written in an
> imperative style. (You know, "for (i = k; i < c; i++) ..." loops
> everywhere.) But it is not hard to implement a few utility macros for
> yourself. (Hrm... A contrib library would be really awesome.)
> Moreover, Clojure data structures and their J2SE suplements really
> eases your work -- think all industrial quality set, list, priority
> queue, etc. implementations. To sum up, I must admit that Clojure (or
> more generally speaking, Lisp) stands as a quite effective tool to
> bridge between algorithms.
> 

Hmm, if it is to be a bridge, then could just as well consider jruby.   jruby 
has many of the lisp constructs and conveniences.In fact there is now a 
ruby subset called Duby which provides java-level performance by doing type 
inference (or by using type annotations).

Thanks for you observations.  Has been very useful.

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


benchmarks on a poor-man's matrix concept

2010-03-08 Thread Jonathan Shore
Hi,

I'm still trying to work out the best way to deal with operations on 
heterogenous data in clojure.   I have a complex application which has such 
requirements.   I wrote a simple toy matrix as a means to explore closure 
versus map based implementations.   Note that in this case the "data structure" 
is not pure, rather mutable.Here are 2 implementations I came up with (note 
this is my second day with closure, so my not be idiomatic):

The first impl uses closures and provides access via a function (is there a 
more efficient way to do this?, avoiding the cond-dispatch?):


(defn matrix-1 [nrow ncol]
  (let [data (float-array (* nrow ncol))]
(fn [command & args]
  (condp = command
:dim 
  [nrow ncol]
:get
  (let [[i j] args] (aget data (* i j)))
:set
  (let [[i j v] args] (aset-float data (* i j) v))


The second implementation uses a map:

(defn matrix-2 [nrow ncol]
  { :data (float-array (* nrow ncol)), :nrow nrow, :ncol ncol })

(defn mset! [mat i j v]
  (aset-float (get mat :data) (* i j) v))

(defn mget [mat i j]
  (aget (get mat :data) (* i j)))

(defn mdim [mat]
  [(get mat :nrow) (get mat :ncol)])

Both of these implementations bother me.  The first because of the dispatch, 
the second because maps clearly are not "near the metal".It would seem 
would have to resort to java side classes, unless there is a better way?

BTW, the map implementation is about 3x faster:

(def m1 (matrix-1 10 10))
(def m2 (matrix-2 10 10))
...

(defn benchmark [what times f]
  (let [
Tstart 
  (System/currentTimeMillis)
result 
  (loop [ntimes times]
(f)
(if (> ntimes 0) 
  (recur (- ntimes 1
Tend
  (System/currentTimeMillis)]   
  (println "evaluating" what  "took" (- Tend Tstart) "ms")))

(benchmark "map-based-matrix" 100 (fn [] (mset! m2 3 3 0.1234)))
(benchmark "closure-based-matrix" 100 (fn [] (m1 :set 3 3 0.1234)))




   

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

Programming Clojure, index-filter & when

2010-03-08 Thread Martin Hauner
Hi,

there is an example in programming clojure (Chapter 2.6, Where is my
for loop? on page 52)
that I don't really get.

(defn index-filter [pred coll]
  (when pred
(for [[idx elt] (indexed coll) :when (pred elt)] idx)))

The explanation in the book explains the for/:when but not the
when. ;) What I do not understand is
why there is the outer (when) .

If I remove the (when):

(defn index-filter-2 [pred coll]
(for [[idx elt] (indexed coll) :when (pred elt)] idx))

it seems to do the same as the first version. So what's the reason for
the (when) ?

-- 
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: bounded memoize

2010-03-08 Thread Eugen Dück
Good points! Testing array-map briefly led me to believe they can be
used as the clojure equivalent of Java\s LinkedHashMaps.

Here's a version that uses a vector to remember order of insertion - I
guess I have to use refs and transactions now:

(defn bounded-memoize
  [f bound]
  (let [mem (ref {})
v (ref [])]
(fn [& args]
  (if-let [e (find @mem args)]
(val e)
(let [ret (apply f args)]
  (dosync
   (when (= (count @v) bound)
 (alter mem dissoc (first @v))
 (alter v subvec 1))
   (alter mem assoc args ret)
   (alter v conj args))
  ret)

Haven't looked at clojure's queues yet, they might make the code more
concise, but by looking at that other post, they don't seem to be
exposed in a clojurey way (using a java class name).

On Mar 9, 11:22 am, Michał Marczyk  wrote:
> On 8 March 2010 05:31, Eugen Dück  wrote:
>
> > And here's a variant that evicts elements when the size of the cache
> > exceeds some limit. In that case, the first item that was put in it
> > will be dissoc'ed. I'm using an array-map to accomplish this:
>
> I don't think this will work as you expect it to. There are two reasons:
>
> (1) Array maps are transparently converted to hash maps when they grow
> beyond a certain size:
>
> user> (class (array-map :a 1 :b 2 :c 3 :d 4 :e 5 :f 6 :g 7 :h 8))
> clojure.lang.PersistentArrayMap
> user> (class (assoc (array-map :a 1 :b 2 :c 3 :d 4 :e 5 :f 6 :g 7 :h 8) :i 9))
> clojure.lang.PersistentArrayMap
> user> (class (assoc (array-map :a 1 :b 2 :c 3 :d 4 :e 5 :f 6 :g 7 :h
> 8) :i 9 :j 10))
> clojure.lang.PersistentHashMap
>
> (2) More importantly, if you dissoc the first key from an array map,
> then assoc a new key onto the array map, the newly assoc key will take
> the first slot:
>
> user> (first (assoc (dissoc (array-map :a 1 :b 2 :c 3) :a) :d 4))
> [:d 4]
>
> I'd suggest a vector instead; they're countable in constant time and
> you can use, say, conj and rest for add to end of queue / eject from
> front of queue.
>
> The idea is certainly a good one, though, so with the above mentioned
> issues fixed, this will be a nice utility function. Thanks for
> sharing!
>
> Sincerely,
> Michał

-- 
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: Shared symbol context and lazy dataflows

2010-03-08 Thread drrobot
On Mar 8, 11:57 pm, Adrian Cuthbertson 
wrote:
> Hi Ivar,
>
> > ;; Do things such as hash-map comprehensions exist? Should they? ;-)
> > (defn some-eqn3 [obj]
> > (let [{:a a :b b :c c} obj]
> >   (/ (+ (- b) (sqrt (- (* b b) (* 4 a c
> >      (* 2 a
>
> At least a partial stab at some of your questions...
>
> (def myhash {:a 1 :b 5 :c 6 :x nil})
>
> (defn some-eqn3 [obj]
>   (let [{:keys [a b c]} obj]
>     (/ (+ (- b) (Math/sqrt (- (* b b)
>          (* 4 a c (* 2 a
>
> (some-eqn3 myhash)
> -2.0
>
> Have a look athttp://clojure.org/special_forms, regarding
> destructured binding with let.

Great! I thought there must be something equivalent to WITH-SLOTS in
Clojure, I just hadn't run into it yet. Also, I think I am quite happy
without the mutability that WITH-SLOTS exposes as well...


Ivar

-- 
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: I confused myself. How can I solve this simple problem?

2010-03-08 Thread Armando Blancas
What about coding it as a hook? Though I realize you have to introduce
a new name.

user=> (defn myprintln [str]
  (println str)
  (println "tacked on"))
#'user/myprintln
user=> (let [print-hk myprintln] (print-hk "some code"))
some code
tacked on
nil
user=> (let [print-hk println] (print-hk "some code"))
some code

-- 
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: Concurrent requests for Java's HTTP Server

2010-03-08 Thread Brendan Ribera
This seems to be more of a Java question than a Clojure one, but I'll take a
stab at it anyway. I'm assuming you refer to
com.sun.net.httpserver.HttpServer. Per the docs,

"Management of threads can be done external to this object by providing a
Executor<../../../../../../../../../api/java/util/concurrent/Executor.html?is-external=true>
object.
If none is provided a default implementation is used."


http://java.sun.com/javase/6/docs/jre/api/net/httpserver/spec/com/sun/net/httpserver/HttpServer.html

On Mon, Mar 8, 2010 at 5:02 AM, Abhay  wrote:

> Hi All,
>
> I have created a small app using the java's base HTTP server shiped
> with JDK.
>
> I observed that the HTTP requests cannot execute simultaneously. For
> eg, my request 1 is blocking my request 2. As soon as I unblock the
> request, other request gets completed.
>
> Does this HTTP Server provides concurrency?
>
> If yes, Its very difficult to believe that a HTTP severs does not
> provide concurrency :(
>
> ~Abhay
>
>
> --
> 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: Programming Clojure, index-filter & when

2010-03-08 Thread Brendan Ribera
Should you pass in nil, the 'when' will guard against a NPE.

On Mon, Mar 8, 2010 at 2:32 PM, Martin Hauner  wrote:

> Hi,
>
> there is an example in programming clojure (Chapter 2.6, Where is my
> for loop? on page 52)
> that I don't really get.
>
> (defn index-filter [pred coll]
>  (when pred
>(for [[idx elt] (indexed coll) :when (pred elt)] idx)))
>
> The explanation in the book explains the for/:when but not the
> when. ;) What I do not understand is
> why there is the outer (when) .
>
> If I remove the (when):
>
> (defn index-filter-2 [pred coll]
>(for [[idx elt] (indexed coll) :when (pred elt)] idx))
>
> it seems to do the same as the first version. So what's the reason for
> the (when) ?
>
> --
> 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: apply-ing Java methods

2010-03-08 Thread Michał Marczyk
On 8 March 2010 17:37, Michael Gardner  wrote:
> Thanks for the tip. After some experimentation:
>
> (apply (memfn connect a b c d) store (map config [:host :port :user 
> :password]))

Oh right, you wanted more arguments. So I should have suggested
#(.connect %1 %2 %3 %4).

> but why does memfn require these "dummy" arguments?

It's simple to write this way... And if you provide type hints, I'd
expect the resulting function to be quite performant. If you don't
care about that, here's a flexible alternative using eval:

user> (defmacro methodfn [name]
`(fn [& args#]
   (apply
(eval (concat (list '~`memfn '~name)
  (map (fn [~'_] (gensym)) (rest args#
args#)))
#'user/methodfn
user> ((methodfn substring) "asdf" 1)
"sdf"
user> ((methodfn substring) "asdf" 1 3)
"sd"

Sincerely,
Michał

-- 
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: Two potential map-util functions

2010-03-08 Thread Timothy Pratley
>      true (update-in m (butlast v) #(dissoc % (last v))

No need for an unnamed function:
true (update-in m (butlast v) dissoc (last v))


Regards,
Tim.

-- 
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 Implementation issues that may affect performance?

2010-03-08 Thread Timothy Pratley
On 9 March 2010 04:03, Jonathan Shore  wrote:
> (defn fib [#^Integer a]
>   (if (< a 2)
>     a
>     (+ (fib (- a 1)) (fib (- a 2)
> I'm just learning, so I may have overlooked something that mitigates or
> otherwise avoids dispatch.

You might want to experiment with something like
(defn fib [a]
  (let [ia (int a)]
..))

I know that seems a little weird but anything passed into or out of a
function gets boxed to an object type. (int a) coerces to a primitive
int which for some operations has much better performance.


Regards,
Tim.

-- 
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: I confused myself. How can I solve this simple problem?

2010-03-08 Thread Meikel Brandmeyer
Hi,

On Mar 8, 10:23 pm, CuppoJava  wrote:

> And yet, the writer of the library shouldn't have to be aware that the
> user *might* bind fast-println to println.
> And the user of the library shouldn't have to be aware of the
> implementation details of fast-println to want to bind println.

Maybe this is just bad design? There was a thread about monkey
patching. Using a different binding for println is just that. And as
you see it introduces the same problems as it does in other languages.
As a library developer I would care a  what a user might want to
rebind. If I did, I would not be allowed to trust a single Var. Please
don't let us enter this SPAM country. Vars which are intended to be
rebound should be clearly labeled so - in the docstring and possibly
with *earmuffs*.

If I intend to design my fast-println so, that it can be used as a
replacement of sorts of the usual println, I can protect against such
a problems with the mentioned let. But then my println's don't compose
anymore...

(binding [println fast-println]
  ...
  (binding [println fancy-println]
...)
  ...)

I'm not sure it is worth to tinker with core functions. At least not
in such a simple way.

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: Interested in creating a Clojure course

2010-03-08 Thread Michael Kohl
The new Clojure course on RubyLearning has now officially been
announced with a tentative starting date of mid to late April:

http://rubylearning.com/blog/2010/03/09/clojure-101-a-new-course/

On Tue, Mar 2, 2010 at 12:54 PM, Michael Kohl  wrote:
> *) decide on a 8 curriculum for an 8 week course based on Mark's
> article (which chapters to use in which order etc)

We decided to shorten the course to 4 weeks and have decided on a curriculum.

> *) design exercises for each week

We still could need some help with this, ideally there would be 3-4
exercises each week reflecting the contents of the lesson. If you are
interested in helping out with this, drop Baishampayan or me a line
and we'll add you to the GitHub repo.

It also still would be nice to have 1-2 more assistant teachers. From
my personal experience with teaching the Ruby courses for the past
year, this can easily be done with about half an hour of free time per
day. Just drop into the forums, answer questions and give some
feedback on the exercise solutions. It's fun and quite a rewarding
experience.

Michael

-- 
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: benchmarks on a poor-man's matrix concept

2010-03-08 Thread Timothy Pratley
On 9 March 2010 06:57, Jonathan Shore  wrote:
> the second because maps clearly are not "near the metal"

maps are actually very very efficiently implemented. Small maps are
especially efficient as they are implemented as array maps.
user=> (type {:a 1, :b 2})
clojure.lang.PersistentArrayMap

At ten entries you get hash-maps, and these are still great.
user=> (type {:a 1, :b 2, :c 3, :d 4, :e 5, :f 6, :g 7, :h 8, :i 9, :j 10})
clojure.lang.PersistentHashMap

You can also define structs which gives a slight optimization. I have
yet to find a case where I would prefer a struct over a map though.
You also have the option of writing a Java class. Clojure allows
pretty much any optimization you can throw at a problem. All the tools
are there if you really need to crank out the cpu cycles or memory
bytes. But the provided data structures are really really good.

I would encourage you to embrace the Clojure data structures and use
them as much as possible with as little ceremony as possible, and only
look to optimize when you find it necessary to meet your
performance/memory goals.


For some really specific information on optimizing array access you
might be interested in this article
http://clj-me.cgrand.net/category/interop/
where Christophe Grand explores the effects of type hinting on arrays
and multi-dimensional arrays. The details can be a bit overwhelming so
I only recommend it if you are really keen on diving deep!

You might also be interested in this library by Konrad (I've not used
it but KH libraries are usually great!)
http://code.google.com/p/clj-multiarray/


Regards,
Tim.

-- 
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: bounded memoize

2010-03-08 Thread Meikel Brandmeyer
Hi,

On Mar 9, 4:41 am, Eugen Dück  wrote:
> Good points! Testing array-map briefly led me to believe they can be
> used as the clojure equivalent of Java\s LinkedHashMaps.
>
> Here's a version that uses a vector to remember order of insertion - I
> guess I have to use refs and transactions now:
>
> (defn bounded-memoize
>   [f bound]
>   (let [mem (ref {})
>         v (ref [])]
>     (fn [& args]
>       (if-let [e (find @mem args)]
>         (val e)
>         (let [ret (apply f args)]
>           (dosync
>            (when (= (count @v) bound)
>              (alter mem dissoc (first @v))
>              (alter v subvec 1))
>            (alter mem assoc args ret)
>            (alter v conj args))
>           ret)
>
> Haven't looked at clojure's queues yet, they might make the code more
> concise, but by looking at that other post, they don't seem to be
> exposed in a clojurey way (using a java class name).

How about generalising memoize to allow different strategies?

(defn bound-cache-strategy
  "Implements a bound cache strategy for memoize. At most bound number
of
  argument lists are kept in the cache. They are dropped in order of
  insertion."
  [bound]
  [find
   (let [values (ref clojure.lang.PersistentQueue/EMPTY)]
 (fn [cache args]
   (alter values conj args)
   (if (> (count @values) bound)
 (do
   (alter values pop)
   (dissoc cache args))
 cache)))])

(defn lru-cache-strategy
  "Implements LRU cache strategy for memoize. At most bound number of
  argument lists are kept in the cache. They are dropped in LRU
order."
  [bound]
  (let [values (ref {})]
[(fn lru-cache-strategy-lookup
   [cache args]
   (when-let [e (find cache args)]
 (let [now (System/currentTimeMillis)]
   (dosync (alter values assoc args now))
   e)))
 (fn lru-cache-strategy-update
   [cache args]
   (let [now (System/currentTimeMillis)
 k   (min-key @values (keys @values))]
 (alter values dissoc k)
 (alter values assoc args now)
 (dissoc cache k)))]))

(defn most-used-cache-strategy
  "Implements MU cache strategy for memoize. At most bound number of
  argument lists are kept in the cache. They are dropped in LU order.
  In case elements have the same usage count, the order of drop is
  unspecified."
  [bound]
  (let [values (ref {})]
[(fn most-used-cache-strategy-lookup
   [cache args]
   (when-let [e (find cache args)]
 (dosync (alter values update-in [args] inc))
 e))
 (fn most-used-cache-strategy-update
   [cache args]
   (let [k (min-key @values (keys @values))]
 (alter values dissoc k)
 (alter values assoc args 1)
 (dissoc cache k)))]))

(defn memoize
  "Returns a memoized version of a referentially transparent function.
The
  memoized version of the function keeps a cache of the mapping from
arguments
  to results and, when calls with the same arguments are repeated
often, has
  higher performance at the expense of higher memory use.

  Optionally a cache strategy might be supplied. A strategy is pair of
  functions:
   - one for accessing the cache, returns the map entry on success or
nil
 (cf. find)
   - one, which takes the cache and the argument vector and might
modify
 the cache.
  Possible implementation could be a bounded cache or a LRU strategy.
  Default is a naive strategy keeping all encountered argument lists
forever.
  The cache update function is called in a transaction, the cache
lookup
  function not necessarily so."
  ([f] (memoize f [find (fn [c _] c)]))
  ([f [cache-lookup cache-update]]
   (let [cache (ref {})]
 (fn [& args]
   (if-let [e (cache-lookup @cache args)]
 (val e)
 (dosync
   (if-let [e (cache-lookup @cache args)]
 (val e)
 (let [result (apply f args)]
   (alter cache assoc args result)
   (alter cache cache-update args)
   result

Usage Examples:

(memoize f)
(memoize f (bound-cache-strategy 5))
(memoize f (lru-cache-strategy 5))
(memoize f (most-used-cache-strategy 5))

(Note: I have no clue about whether lru is well implemented or mu
makes sense. Just some examples...)

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: symbolmacro tests fail under 1.3.2-SNAPSHOT

2010-03-08 Thread Konrad Hinsen

On 9 Mar 2010, at 01:01, Mark Derricutt wrote:


I know we've changed around how clojure:test runs under 1.3.2-SNAPSHOT
and I half expected the build to fail, but this wasn't the failure I
expected to see.  Anyone have any idea whats going on here?  It has me
stumped at the moment...

Is this a problem with clojure-contrib, or how the plugin is
bootstrapping things?


I wrote that code, so perhaps I can contribute to solving that  
problem, but unfortunately I don't know anything at all about the  
Maven plugin and what it does to run tests.


From the failure message it seems that the symbol macro in the test  
is not expanded at all. It is unlikely that the failure is due to the  
macro with-symbol-macros, because that would probably cause other  
tests in the same test suite to fail as well. I suspect that the  
symbol macro is not recognized as such because of some problem with  
metadata handling. Here is how symbol macros are defined:


(defmacro defsymbolmacro
  "Define a symbol macro. Because symbol macros are not part of
   Clojure's built-in macro expansion system, they can be used only
   inside a with-symbol-macros form."
  [symbol expansion]
  (let [meta-map (if (meta symbol) (meta symbol) {})
meta-map (assoc meta-map :symbol-macro true)]
  `(def ~(with-meta symbol meta-map) (quote ~expansion

A symbol macro is a symbol whose value is its expansion and whose  
metadata map includes {:symbol-macro true}. The macro expansion code  
checks for that metadata tag. If it doesn't find it, it doesn't expand  
anything, which happens to be the observed behaviour in that test.


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