Sorting gotcha

2011-08-23 Thread Mark Engelberg
I had always assumed that vectors were sorted lexicographically.  In
other words, you sort on the first element, and then refine by the
second element, and so on.  I was surprised tonight to discover that
is not the case.

 (compare abc b); Strings are compared lexicographically
-1
 (compare (vec abc) (vec b)); Vectors are not
1

It turns out that shorter vectors are always considered to be less
than longer vectors.  Lexicographic behavior only kicks in among
vectors of the same length.  Not at all what I expected, so I wanted
to make sure everyone knew about this behavior.

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


Re: a question about using the delay macro

2011-08-23 Thread faenvie
perfect answer.

thank you !

btw: my snippet is taken out of the docs for java.jdbc.

On Aug 22, 12:04 pm, Meikel Brandmeyer (kotarak) m...@kotka.de
wrote:
 Hope, I'm not too far off.

 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: Sorting gotcha

2011-08-23 Thread Ben Smith-Mannschott
On Tue, Aug 23, 2011 at 09:44, Mark Engelberg mark.engelb...@gmail.com wrote:
 I had always assumed that vectors were sorted lexicographically.  In
 other words, you sort on the first element, and then refine by the
 second element, and so on.  I was surprised tonight to discover that
 is not the case.

 (compare abc b)        ; Strings are compared lexicographically
 -1
 (compare (vec abc) (vec b))    ; Vectors are not
 1

 It turns out that shorter vectors are always considered to be less
 than longer vectors.  Lexicographic behavior only kicks in among
 vectors of the same length.  Not at all what I expected, so I wanted
 to make sure everyone knew about this behavior.

Yea, confirmed.

$ git describe
clojure-1.3.0-beta1-6-g82c7254

$ java -jar target/clojure-1.3.0-master-SNAPSHOT.jar
Clojure 1.3.0-master-SNAPSHOT

user= (defn sort* [  more ] (sort more))

;; strings: always sort lexicographically

user= (sort* abc b)
(abc b)

;; vector: short sorts before long

user= (sort* (vec abc) (vec b))
([\b] [\a \b \c])

;; vector: same length sorts lexicographically

user= (sort* (vec abc) (vec abb))
([\a \b \b] [\a \b \c])

user= (sort* (vec abc) (vec abd))
([\a \b \c] [\a \b \d])

;; seq: does not implement comparable

user= (sort* (seq abc) (seq b))
ClassCastException clojure.lang.StringSeq cannot be cast to
java.lang.Comparable  clojure.lang.Util.compare (Util.java:92)

user= (sort* (seq (vec abc)) (seq (vec b)))
ClassCastException clojure.lang.PersistentVector$ChunkedSeq cannot be
cast to java.lang.Comparable  clojure.lang.Util.compare (Util.java:92)

;; list: does not implement comparable

user= (sort* (apply list abc) (apply list b))
ClassCastException clojure.lang.PersistentList cannot be cast to
java.lang.Comparable  clojure.lang.Util.compare (Util.java:92)


It seems a shame that Clojure doesn't provide universal lexicographical
comparability of sequential things, much as it provides equality for
sequential things:

user= (= (list \a \b \c) (vector \a \b \c))
true

Perhaps there's a good reason for this (law of unintended consequences,
anyone?) that I'm not seeing.

// Ben

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


How to import libs in Clojure?

2011-08-23 Thread Wanderfels
Hello,

win XP, clojure 1.2.1 and clojure.contrib-1.2.0.jar here. i want to
learn clojure (background python and javascript and a bit Haskell) and
are currently reading the pdf 'Programming Clojure' from 2009.
In Chapter 1.3: 'Exploring Clojure Libraries' it says:


Clojure code is packaged in libraries. Each Clojure library belongs to
a namespace, which is analogous to a Java package. You can load a
Clojure library with require:

  (require quoted-namespace-symbol)

When you require a library named clojure.contrib.str-utils, Clojure
looks for
a file named clojure/contrib/str-utils.clj on the CLASSPATH. Try it:

  user= (require 'clojure.contrib.str-utils)
  nil


i did. i got:

  user= (require 'clojure.contrib.str-utils)
  java.io.FileNotFoundException: Could not locate clojure/contrib/
str_utils__init.class or clojure/contrib/str_utils.clj on classpath:
(NO_SOURCE_FILE:0)

i googled a bit. i now know that:

* if you type str-utils it searches for str_utils.clj, not str-
utils.clj btw.
  my pdf is wrong (or old) here.
* setting CLASSPATH doesnt help (JAVA_HOME is set btw).
* setting classpath directly with ...
java -classpath C:\CLOJURE\lib\clojure-contrib-1.2.0.jar -cp
clojure.jar clojure.main
  ... doesnt help.
* doing add-classpath seemed to work for a guy @
http://stackoverflow.com/questions/1805081/clojure-cant-find-clj-in-local-directory-and-classes-on-classpath
but a) not for me ...

  user= (add-classpath file:///C:/CLOJURE/lib/clojure-
contrib-1.2.0.jar)
  WARNING: add-classpath is deprecated
  nil
  user= (println (seq (.getURLs (java.lang.ClassLoader/
getSystemClassLoader
  (#URL file:/C:/CLOJURE/bin/clojure.jar)
  nil

... and b) it is depreaced.

both str_utils__init.class and clojure/contrib/str_utils.clj are
contained in this contrib jar at the right position. I tried removing
the numbers from the jar. i tried extracting the contents of the jar.
i tried moving the contrib folder to C:\CLOJURE\bin\src\clj\clojure.
li tried moving the contrib jar to C:\CLOJURE\bin.

Now i need a hint. How to import libs in Clojure?

greets, Wanderfels

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


Re: How to import libs in Clojure?

2011-08-23 Thread Michael Wood
Hi

On 23 August 2011 12:10, Wanderfels wanderf...@web.de wrote:
 Hello,

 win XP, clojure 1.2.1 and clojure.contrib-1.2.0.jar here. i want to
 learn clojure (background python and javascript and a bit Haskell) and
 are currently reading the pdf 'Programming Clojure' from 2009.
 In Chapter 1.3: 'Exploring Clojure Libraries' it says:

 
 Clojure code is packaged in libraries. Each Clojure library belongs to
 a namespace, which is analogous to a Java package. You can load a
 Clojure library with require:

  (require quoted-namespace-symbol)

 When you require a library named clojure.contrib.str-utils, Clojure
 looks for
 a file named clojure/contrib/str-utils.clj on the CLASSPATH. Try it:

  user= (require 'clojure.contrib.str-utils)
  nil
 

 i did. i got:

  user= (require 'clojure.contrib.str-utils)
  java.io.FileNotFoundException: Could not locate clojure/contrib/
 str_utils__init.class or clojure/contrib/str_utils.clj on classpath:
 (NO_SOURCE_FILE:0)

 i googled a bit. i now know that:

 * if you type str-utils it searches for str_utils.clj, not str-
 utils.clj btw.
  my pdf is wrong (or old) here.
 * setting CLASSPATH doesnt help (JAVA_HOME is set btw).

Depending on how you started the repl this may be ignored.

 * setting classpath directly with ...
    java -classpath C:\CLOJURE\lib\clojure-contrib-1.2.0.jar -cp
 clojure.jar clojure.main
  ... doesnt help.

-cp is the short form of -classpath.  If you want more than one thing
on the classpath, you should use:

java -classpath C:\CLOJURE\lib\clojure-contrib-1.2.0.jar;clojure.jar
clojure.main

(assuming you have clojure.jar in the current directory)

or:

java -cp C:\CLOJURE\lib\clojure-contrib-1.2.0.jar;clojure.jar clojure.main

 * doing add-classpath seemed to work for a guy @
 http://stackoverflow.com/questions/1805081/clojure-cant-find-clj-in-local-directory-and-classes-on-classpath
 but a) not for me ...

  user= (add-classpath file:///C:/CLOJURE/lib/clojure-
 contrib-1.2.0.jar)
  WARNING: add-classpath is deprecated
  nil
  user= (println (seq (.getURLs (java.lang.ClassLoader/
 getSystemClassLoader
  (#URL file:/C:/CLOJURE/bin/clojure.jar)
  nil

 ... and b) it is depreaced.

It is indeed deprecated and may not work.  Something to do with class
loaders.  Rather just specify the jar in the classpath manually on the
command line, or better, use Leiningen as I'm sure others will tell
you.

 both str_utils__init.class and clojure/contrib/str_utils.clj are
 contained in this contrib jar at the right position. I tried removing
 the numbers from the jar. i tried extracting the contents of the jar.
 i tried moving the contrib folder to C:\CLOJURE\bin\src\clj\clojure.
 li tried moving the contrib jar to C:\CLOJURE\bin.

 Now i need a hint. How to import libs in Clojure?

 greets, Wanderfels

Basically you were on the right track, but the way you were specifying
the classpath was wrong.

Something like Leiningen or cake can help you with this sort of thing.
 They require you to create a project, which might seem strange
coming from Python, but since they help with things like the classpath
and downloading dependencies etc. that's generally worth the effort.

e.g.:

lein new myproject
cd myproject
edit project.clj
(add the clojure.contrib.str-utils dependency)
lein deps
lein repl

https://github.com/technomancy/leiningen#readme

-- 
Michael Wood esiot...@gmail.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: Video Slides on Pattern Matching and Predicate Dispatch in Clojure

2011-08-23 Thread Brent Millare
In the ClojureScript case, you can do lazy compile time compilation
instead, where the predicate call is really a macro that always
expands into a predicate call but during compile time can check if the
tree needs to be updated. This isn't as lazy as the runtime version
but at least groups of extend-pred won't compile unnecessarily.

The only other option I can see is to make the compile step like a
hook that is integrated to the build process and runs at the very end.

-Brent

On Aug 22, 3:12 pm, David Nolen dnolen.li...@gmail.com wrote:
 On Mon, Aug 22, 2011 at 3:07 PM, Brent Millare brent.mill...@gmail.comwrote:









   For pattern matching code size is a one time cost. For predicate
  dispatch,
   that's a lot of code to generated, since every new predicate case will
   produce an entirely new tree. But perhaps people won't care that much.
  Only
   time and experience reports will tell.

  If you want, you can be lazy about compilation and only compile right
  before the call to the predicate only if a new predicate has been
  added since the last compilation. Also, we can be smart about how we
  do the cached tree check. After extend-pred is called, it wraps the
  current DAG tree with a compilation step.

  During the compilation step (made right before the call), the new DAG
  tree is made and replaces the old one. Note that there is no longer a
  check for new predicates.

  -Brent

 Interesting idea. One possible down side of lazy runtime compilation is that
 it makes it more difficult to target ClojureScript.

 David

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


Re: a question about using the delay macro

2011-08-23 Thread Sean Corfield
I was trying to construct a simple example of what I actually have in
my apps that use pooling on top of java.jdbc. My actual code *does*
work to create a singleton but you're right, I've contracted my code
too far in trying to create a simple example of it... I'll have
another attempt!

Sean

On Aug 22, 3:04 am, Meikel Brandmeyer (kotarak) m...@kotka.de
wrote:
 Am Montag, 22. August 2011 11:53:32 UTC+2 schrieb faenvie:
  yesterday i came across the following use of
  the delay-macro:

  (defn pooled-data-source
    [db-connection-settings]
    ; this Fn creates and returns object of type PooledDataSource
  )

  (defn pooled-data-source-as-singleton
    [db-connection-settings]
    (let [datasource (delay (pooled-data-source db-connection-
  settings))]
      @datasource))

  i am not sure, what the exact semantic of this is and if
  it's safe in a multithreaded context.

  What is the difference to using def/defonce ?

  Someone who can explain ?

 This snippet does not work as the original author intended. You can remove
 the whole delay incantations and everything will work as before. That's
 because each call of pooled-data-source-as-singleton creates a new delay,
 forces it and throws it away immediately. What probably was intended, is the
 use of memoize:

 (def ^{:arglists ([db-connection-settings])} pooled-data-source-as-singleton
   (memoize pooled-data-source))

 The intention is to delay the creation of the datapool instance until
 runtime and not do it on load time. This is also nice if you have your db
 connection configurable in some non-code form. If you have only one db
 connection, which is hardwired in some Var at load time, you can go with the
 following:

 (let [db-connection-pool (delay (pooled-data-source
 db-connection-settings))]
   (defn pooled-data-source-as-singleton
     []
     @db-connection-pool))

 This is maybe, what the original author had in mind.

 Hope, I'm not too far off.

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


Cron algorithm challenge

2011-08-23 Thread Michael Jaaka
Hi!

I have some challenge for you, sine it is easy express it in
imperative language I would like to ask you if is it possible to
create DSL to express such algorithm in clojure or if is it too
complicated just write it in functional manner. The challenge is to
write cron algorithm. I can express it in such way: http://pastebin.com/1ssvbJ8z

Anyone?
Thanks in advance!

-- 
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: Cron algorithm challenge

2011-08-23 Thread Michael Jaaka
After the post I suddenly saw the code in my mind: http://pastebin.com/kYYYirdb
The expression abilities are truly near the mind.
Clojure rox :-)

On Aug 23, 6:01 pm, Michael Jaaka michael.ja...@googlemail.com
wrote:
 Hi!

 I have some challenge for you, sine it is easy express it in
 imperative language I would like to ask you if is it possible to
 create DSL to express such algorithm in clojure or if is it too
 complicated just write it in functional manner. The challenge is to
 write cron algorithm. I can express it in such 
 way:http://pastebin.com/1ssvbJ8z

 Anyone?
 Thanks in advance!

-- 
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: Cron algorithm challenge

2011-08-23 Thread Meikel Brandmeyer
Hi,

Am 23.08.2011 um 18:01 schrieb Michael Jaaka:

 I have some challenge for you, sine it is easy express it in
 imperative language I would like to ask you if is it possible to
 create DSL to express such algorithm in clojure or if is it too
 complicated just write it in functional manner. The challenge is to
 write cron algorithm. I can express it in such way: 
 http://pastebin.com/1ssvbJ8z

Here my try. Basic idea: Provide a sequence of times where things have to run 
based on a pattern. A pattern is a vector of months, weekdays, days, hours and 
minutes. Each may be a _ (read: don't care), a number or a set of numbers to 
specify several allowed times. (One might also want to allow maps for ranges?)

The main function then waits till the next scheduled point in time. Executes 
the function. Wait till the next scheduled point in time. …

Much is left to be desired: What if the next moment has already expired when f 
completes? What does wait-till look like? Maybe cron-seq could be more clever 
than brute force?

But I think you get the idea.

Sincerely
Meikel

(import 'org.joda.time.DateTime)

(defn cron-seq
  [pattern]
  (let [fix-up-pattern (fn [x]
 (cond
   (= x '_) nil
   (set? x) x
   :else#{x}))
[months weekdays days hours minutes]
(map fix-up-pattern pattern)]
(for [point-in-time (iterate #(.plusMinutes % 1) (DateTime/now))
  :when (or (not minutes)  (minutes  (.getMinuteOfHour point-in-time)))
  :when (or (not hours)(hours(.getHourOfDay point-in-time)))
  :when (or (not days) (days (.getDayOfMonth point-in-time)))
  :when (or (not weekdays) (weekdays (.getDayOfWeek point-in-time)))
  :when (or (not months)   (months   (.getMonthOfYear point-in-time)))]
  point-in-time)))

; Left as excercise.
(defn wait-till
  [future-point-in-time]
  (magic happens here))

(defn cron*
  [pattern f  args]
  (loop [schedule-time (seq (cron-seq pattern))]
(when schedule-time
  (wait-till (first schedule-time))
  (apply f args)
  (recur (next schedule-time)

(defmacro cron
  [pattern  command]
  `(cron* ~(vec (map #(if (= % '_) `(quote ~%) %) pattern)) ~@command))

; Usage:
(cron [#{1 4 7 10} _ _ 6 30] do-something with 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


Odp: Re: Cron algorithm challenge

2011-08-23 Thread Michael Jaaka
Wow nice! :-)

Of course wait-till can be done with Timers build-in JDK.

And here is a proposition for cron parsing function:

(defn parse-cron-expr[ pattern ]
(let[ cal (Calendar/getInstance) ]
(let [[min-pat hour-pat day-pat month-pat week-pat] (letfn[ (parse-cron[ val 
pos cal ]
(letfn[ (range-values[ val ]
(if-let[ [ gr min max ] (first (re-seq #([\d]+)-([\d]+) val)) ]
(range (Integer. min) (inc (Integer. max)))
(if (= val *) 
(condp = pos Calendar/DAY_OF_WEEK nil Calendar/DAY_OF_MONTH nil
(map (if (= pos Calendar/MONTH) inc identity)
(range (.getMinimum cal pos) (inc (.getMaximum cal pos)
 [(Integer. val)])   )) ]
(let[ [ tx re div ] (first (re-seq #([^\/]+)/?([\d]+)? val)) 
div-n (or (nil? div) (Integer. div)) ]
(filter #(or (nil? div) (= (mod %1  div-n) 0)) 
(sort (distinct (mapcat range-values (re-seq #[^\,]+ re
 (into-set[ cal val ]
(into #{} val))
 (into-weeks[ cal val ]
(let [ z (zipmap (range 1 8) (iterate #(inc (mod %1 7)) (.getFirstDayOfWeek 
cal))) ]
(into-set cal (map #(get z %1) val
 (into-months[ cal val ]
(into-set cal (map dec val))) ]

(let[ cal (repeat cal) ]
(map #(%1 %2 %3) [into-set into-set into-set into-months into-weeks] 
cal (map parse-cron  (re-seq #[\S]+ pattern) 
[ Calendar/MINUTE Calendar/HOUR_OF_DAY Calendar/DAY_OF_MONTH
Calendar/MONTH  Calendar/DAY_OF_WEEK ] cal) ))) ]

(if (and (empty? day-pat) (empty? week-pat))
[min-pat hour-pat day-pat month-pat 
(into #{} (range (.getMinimum cal Calendar/DAY_OF_WEEK) (inc (.getMaximum 
cal Calendar/DAY_OF_WEEK ]
[min-pat hour-pat day-pat month-pat week-pat]



and use is

(parse-cron-expr 0 10 10-13 * 1-5)

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

Re: How to import libs in Clojure?

2011-08-23 Thread Paul Lam
Did you use the -cp option to include the contrib when running
java.exe on clojure.jar? The error shows that it's looking in a
subdirectory with a .class rather than the packaged jar file.

I'd suggest using leiningen to avoid build problems like this.



On Aug 23, 6:10 am, Wanderfels wanderf...@web.de wrote:
 Hello,

 win XP, clojure 1.2.1 and clojure.contrib-1.2.0.jar here. i want to
 learn clojure (background python and javascript and a bit Haskell) and
 are currently reading the pdf 'Programming Clojure' from 2009.
 In Chapter 1.3: 'Exploring Clojure Libraries' it says:

 
 Clojure code is packaged in libraries. Each Clojure library belongs to
 a namespace, which is analogous to a Java package. You can load a
 Clojure library with require:

   (require quoted-namespace-symbol)

 When you require a library named clojure.contrib.str-utils, Clojure
 looks for
 a file named clojure/contrib/str-utils.clj on the CLASSPATH. Try it:

   user= (require 'clojure.contrib.str-utils)
   nil
 

 i did. i got:

   user= (require 'clojure.contrib.str-utils)
   java.io.FileNotFoundException: Could not locate clojure/contrib/
 str_utils__init.class or clojure/contrib/str_utils.clj on classpath:
 (NO_SOURCE_FILE:0)

 i googled a bit. i now know that:

 * if you type str-utils it searches for str_utils.clj, not str-
 utils.clj btw.
   my pdf is wrong (or old) here.
 * setting CLASSPATH doesnt help (JAVA_HOME is set btw).
 * setting classpath directly with ...
     java -classpath C:\CLOJURE\lib\clojure-contrib-1.2.0.jar -cp
 clojure.jar clojure.main
   ... doesnt help.
 * doing add-classpath seemed to work for a guy 
 @http://stackoverflow.com/questions/1805081/clojure-cant-find-clj-in-l...
 but a) not for me ...

   user= (add-classpath file:///C:/CLOJURE/lib/clojure-
 contrib-1.2.0.jar)
   WARNING: add-classpath is deprecated
   nil
   user= (println (seq (.getURLs (java.lang.ClassLoader/
 getSystemClassLoader
   (#URL file:/C:/CLOJURE/bin/clojure.jar)
   nil

 ... and b) it is depreaced.

 both str_utils__init.class and clojure/contrib/str_utils.clj are
 contained in this contrib jar at the right position. I tried removing
 the numbers from the jar. i tried extracting the contents of the jar.
 i tried moving the contrib folder to C:\CLOJURE\bin\src\clj\clojure.
 li tried moving the contrib jar to C:\CLOJURE\bin.

 Now i need a hint. How to import libs in Clojure?

 greets, Wanderfels

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


why is it necessary to use identity to check for nils in an if statement

2011-08-23 Thread Andrew Xue
this doesn't work:

user= (defn if-a [a b] (if (a) (str a) (str b)))
#'user/if-a
user= (if-a nil b)
java.lang.NullPointerException (NO_SOURCE_FILE:0)
user= (if-a a nil)
user= java.lang.ClassCastException: java.lang.String cannot be cast
to clojure.lang.IFn (NO_SOURCE_FILE:0)

this does work:

user= (defn if-a [a b] (if (identity a) (str a) (str b)))
#'user/if-a
user= (if-a nil b)
b
user= (if-a a nil)
a


why is the identity function is needed? thanks

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


Re: why is it necessary to use identity to check for nils in an if statement

2011-08-23 Thread Dave Ray
You have an extra set of parens around a, treating it as a function call. Try:

  (defn if-a [a b] (if a (str a) (str b)))

Hope that helps,

Dave

On Tue, Aug 23, 2011 at 4:37 PM, Andrew Xue and...@lumoslabs.com wrote:
 this doesn't work:

 user= (defn if-a [a b] (if (a) (str a) (str b)))
 #'user/if-a
 user= (if-a nil b)
 java.lang.NullPointerException (NO_SOURCE_FILE:0)
 user= (if-a a nil)
 user= java.lang.ClassCastException: java.lang.String cannot be cast
 to clojure.lang.IFn (NO_SOURCE_FILE:0)

 this does work:

 user= (defn if-a [a b] (if (identity a) (str a) (str b)))
 #'user/if-a
 user= (if-a nil b)
 b
 user= (if-a a nil)
 a


 why is the identity function is needed? thanks

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

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


Re: why is it necessary to use identity to check for nils in an if statement

2011-08-23 Thread Matthew Gilliard
 user= (defn if-a [a b] (if (a) (str a) (str b)))

The problem is  (a)  - it tries to call a as a function, which
throws NullPointer if a is nil.  You meant:

 user= (defn if-a [a b] (if a (str a) (str b)))



mg



On Tue, Aug 23, 2011 at 9:37 PM, Andrew Xue and...@lumoslabs.com wrote:
 this doesn't work:

 user= (defn if-a [a b] (if (a) (str a) (str b)))
 #'user/if-a
 user= (if-a nil b)
 java.lang.NullPointerException (NO_SOURCE_FILE:0)
 user= (if-a a nil)
 user= java.lang.ClassCastException: java.lang.String cannot be cast
 to clojure.lang.IFn (NO_SOURCE_FILE:0)

 this does work:

 user= (defn if-a [a b] (if (identity a) (str a) (str b)))
 #'user/if-a
 user= (if-a nil b)
 b
 user= (if-a a nil)
 a


 why is the identity function is needed? thanks

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

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


Creating a Clojure Record from a string

2011-08-23 Thread Timothy Baldridge
Given:

test=(defrecord Foo [A B])
test=(class (Foo. 1 2))
test.Foo

How do I:

test=(new test.Foo 1 2)
#:test.Foo{:A 1, :B 2}

Currently I get  Unable to resolve classname: test/Foo.

Thanks,

Timothy

-- 
“One of the main causes of the fall of the Roman Empire was
that–lacking zero–they had no way to indicate successful termination
of their C programs.”
(Robert Firth)

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


Re: Creating a Clojure Record from a string

2011-08-23 Thread Stuart Halloway
 Given:
 
 test=(defrecord Foo [A B])
 test=(class (Foo. 1 2))
 test.Foo
 
 How do I:
 
 test=(new test.Foo 1 2)
 #:test.Foo{:A 1, :B 2}
 
 Currently I get  Unable to resolve classname: test/Foo.
 
 Thanks,
 
 Timothy

(Class/forName java.lang.String)

 

Be mindful of the performance...

Stu


Stuart Halloway
Clojure/core
http://clojure.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: Creating a Clojure Record from a string

2011-08-23 Thread Craig Andera
 Given:

 test=(defrecord Foo [A B])
 test=(class (Foo. 1 2))
 test.Foo

 How do I:

 test=(new test.Foo 1 2)
 #:test.Foo{:A 1, :B 2}

 Currently I get  Unable to resolve classname: test/Foo.

Check out 
http://stackoverflow.com/questions/3748559/clojure-creating-new-instance-from-string-class-name.
Be sure to scroll down to see all the replies, since the macro one is
(IMO) the easiest to understand, although it has limitations.

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


Re: Creating a Clojure Record from a string

2011-08-23 Thread Craig Andera
 (Class/forName java.lang.String)

Oh, does that work in 1.3? Because (new (Class/forName user.Foo))
was the first thing I tried (under 1.2) and it doesn't work. Perhaps
unsurprisingly given that new is a special form.

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


Re: Creating a Clojure Record from a string

2011-08-23 Thread Alan Malloy
On Aug 23, 3:39 pm, Craig Andera cand...@wangdera.com wrote:
  (Class/forName java.lang.String)

 Oh, does that work in 1.3? Because (new (Class/forName user.Foo))
 was the first thing I tried (under 1.2) and it doesn't work. Perhaps
 unsurprisingly given that new is a special form.

No. But you can use reflection to futz around with the class object,
find its constructor, and then invoke that.

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


clojure.java.jdbc: mapping BigDecimal to double

2011-08-23 Thread HiHeelHottie

Hi,

It looks like Oracle NUMBER types get mapped to BigDecimal in a result
seq from clojure.java.jdbc. Is there an easy way to configure
clojure.java.jdbc/ResultSet to map Oracle NUMBERS to doubles?

The resultset-seq from 
https://github.com/clojure/java.jdbc/blob/master/src/main/clojure/clojure/java/jdbc/internal.clj
is below.

(defn resultset-seq*
  Creates and returns a lazy sequence of structmaps corresponding to
   the rows in the java.sql.ResultSet rs. Based on clojure.core/
resultset-seq
   but it respects the current naming strategy.
  [^ResultSet rs]
(let [rsmeta (.getMetaData rs)
  idxs (range 1 (inc (.getColumnCount rsmeta)))
  keys (map (comp keyword *as-key*)
(map (fn [^Integer i] (.getColumnLabel rsmeta i))
idxs))
  check-keys
(or (apply distinct? keys)
(throw (Exception. ResultSet must have unique
column labels)))
  row-struct (apply create-struct keys)
  row-values (fn [] (map (fn [^Integer i] (.getObject rs i))
idxs))
  rows (fn thisfn []
 (when (.next rs)
   (cons (apply struct row-struct (row-values)) (lazy-
seq (thisfn)]
  (rows)))

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


Re: Creating a Clojure Record from a string

2011-08-23 Thread rfhayashi
test= (def foo-class-symbol (load-string test.Foo))
test= (def foo (eval (list 'new foo-class-symbol 1 2)))
test= foo
#:test.Foo{:A 1, :B 2}

Is that what you want?

On Aug 23, 6:24 pm, Timothy Baldridge tbaldri...@gmail.com wrote:
 Given:

 test=(defrecord Foo [A B])
 test=(class (Foo. 1 2))
 test.Foo

 How do I:

 test=(new test.Foo 1 2)
 #:test.Foo{:A 1, :B 2}

 Currently I get  Unable to resolve classname: test/Foo.

 Thanks,

 Timothy

 --
 “One of the main causes of the fall of the Roman Empire was
 that–lacking zero–they had no way to indicate successful termination
 of their C programs.”
 (Robert Firth)

-- 
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.java.jdbc: mapping BigDecimal to double

2011-08-23 Thread Sean Corfield
No, you'd have to do it yourself. Since not all BigDecimal values
would fit correctly in double, it would be dangerous for resultset-seq
to do it.

I expect there are all sorts of JDBC data types that don't quite match
Clojure types but I don't think automatically mapping them would be a
good idea...

Sean

On Tue, Aug 23, 2011 at 6:16 PM, HiHeelHottie hiheelhot...@gmail.com wrote:
 It looks like Oracle NUMBER types get mapped to BigDecimal in a result
 seq from clojure.java.jdbc. Is there an easy way to configure
 clojure.java.jdbc/ResultSet to map Oracle NUMBERS to doubles?

-- 
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.java.jdbc: mapping BigDecimal to double

2011-08-23 Thread HiHeelHottie

Hey Sean,

I really appreciate the quick response and your work with java.jdbc.
Completely agree with you that it shouldn't automatically map out of
the box. As a newbie to clojure and jdbc, do you have any advice on
how I can get into resultset-seq* to do the mapping? I think it would
be better not to have to map a BigDecimal to double after resultset-
seq* returns a row.

Are there any future plans to add a mapping api to resultset-seq or is
the pattern just to chain any custom mappings after resultset-seq?

On Aug 23, 9:41 pm, Sean Corfield seancorfi...@gmail.com wrote:
 No, you'd have to do it yourself. Since not all BigDecimal values
 would fit correctly in double, it would be dangerous for resultset-seq
 to do it.

 I expect there are all sorts of JDBC data types that don't quite match
 Clojure types but I don't think automatically mapping them would be a
 good idea...

 Sean







 On Tue, Aug 23, 2011 at 6:16 PM, HiHeelHottie hiheelhot...@gmail.com wrote:
  It looks like Oracle NUMBER types get mapped to BigDecimal in a result
  seq from clojure.java.jdbc. Is there an easy way to configure
  clojure.java.jdbc/ResultSet to map Oracle NUMBERS to doubles?

-- 
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.java.jdbc: mapping BigDecimal to double

2011-08-23 Thread Ken Wesson
On Tue, Aug 23, 2011 at 9:54 PM, HiHeelHottie hiheelhot...@gmail.com wrote:
 Are there any future plans to add a mapping api to resultset-seq or is
 the pattern just to chain any custom mappings after resultset-seq?

Is wrapping in (map double ...) too much typing? :)

-- 
Protege: What is this seething mass of parentheses?!
Master: Your father's Lisp REPL. This is the language of a true
hacker. Not as clumsy or random as C++; a language for a more
civilized age.

-- 
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.java.jdbc: mapping BigDecimal to double

2011-08-23 Thread Sean Corfield
On Tue, Aug 23, 2011 at 6:54 PM, HiHeelHottie hiheelhot...@gmail.com wrote:
 Completely agree with you that it shouldn't automatically map out of
 the box. As a newbie to clojure and jdbc, do you have any advice on
 how I can get into resultset-seq* to do the mapping? I think it would
 be better not to have to map a BigDecimal to double after resultset-
 seq* returns a row.

Apply something like this to the result?

(map (fn [row] (into {} (map (fn [[k v]] [k (some-mapping v)]) row))) results)

 Are there any future plans to add a mapping api to resultset-seq or is
 the pattern just to chain any custom mappings after resultset-seq?

Currently no plans. I don't want to add the overhead of map-over-map
for all users when I suspect only a few would want / need such a
mapping.
-- 
Sean A Corfield -- (904) 302-SEAN
An Architect's View -- http://corfield.org/
World Singles, LLC. -- http://worldsingles.com/
Railo Technologies, Inc. -- http://www.getrailo.com/

Perfection is the enemy of the good.
-- Gustave Flaubert, French realist novelist (1821-1880)

-- 
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.java.jdbc: mapping BigDecimal to double

2011-08-23 Thread gaz jones
the oracle jdbc adapter returns a whole host of strange datatypes. for
instance, it returns bigdecimals for numbers you have mapped to be
numbers (with a precision, without a scale) in the table. it also
returns its own custom time classes. these generally have a toJdbc()
method to convert them to the 'expected' jdbc type.

a quick look under the covers of any other language's db libraries
(such as activerecord or sequel) shows a whole host of these annoying
conversions back and forth. to be honest, the lack of the conversion
in java.jdbc is great as i can decide what i want to convert / not
convert.

tldr oracle jdbc is shit :P

On Tue, Aug 23, 2011 at 8:54 PM, HiHeelHottie hiheelhot...@gmail.com wrote:

 Hey Sean,

 I really appreciate the quick response and your work with java.jdbc.
 Completely agree with you that it shouldn't automatically map out of
 the box. As a newbie to clojure and jdbc, do you have any advice on
 how I can get into resultset-seq* to do the mapping? I think it would
 be better not to have to map a BigDecimal to double after resultset-
 seq* returns a row.

 Are there any future plans to add a mapping api to resultset-seq or is
 the pattern just to chain any custom mappings after resultset-seq?

 On Aug 23, 9:41 pm, Sean Corfield seancorfi...@gmail.com wrote:
 No, you'd have to do it yourself. Since not all BigDecimal values
 would fit correctly in double, it would be dangerous for resultset-seq
 to do it.

 I expect there are all sorts of JDBC data types that don't quite match
 Clojure types but I don't think automatically mapping them would be a
 good idea...

 Sean







 On Tue, Aug 23, 2011 at 6:16 PM, HiHeelHottie hiheelhot...@gmail.com wrote:
  It looks like Oracle NUMBER types get mapped to BigDecimal in a result
  seq from clojure.java.jdbc. Is there an easy way to configure
  clojure.java.jdbc/ResultSet to map Oracle NUMBERS to doubles?

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