Re: compojure 0.6.0: problem getting post arguments with google app engine

2011-02-17 Thread Zmitro Lapcjonak
On Feb 17, 5:13 am, Zhenchao Li  wrote:

>    (POST "/vote" {params :params} (post-vote params))

> The site here is used to capture :params, which is new in compojure
> 0.6.0. However I'm getting a empty map in post-vote. I wonder what's
> wrong with the above code? Any suggestions? Hints?

Have you verified, that your html form really sends the params?
Also you can wrap your handler "index" with logging middleware,
to see, what is your request.

--
Zmi La

-- 
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: Get digits of a number

2011-02-17 Thread Ken Wesson
On Thu, Feb 17, 2011 at 1:39 AM, Shantanu Kumar
 wrote:
> On Feb 17, 11:09 am, Ken Wesson  wrote:
>> On Thu, Feb 17, 2011 at 12:29 AM, Andreas Kostler
>>
>>  wrote:
>> > Is there an easy and idiomatic way of getting the digits of a number in 
>> > clojure?
>>
>> > (defn explode-to-digits [number]
>> >        (map #(- (int %) (int \0)) (str number)))
>> > (explode-to-digits 123456)
>> > => (1 2 3 4 5 6)
>>
>> > Seems a bit clunky...
>>
>> How about
>>
>> (defn explode-to-digits [n]
>>   (if (= n 0)
>>     []
>>     (let [d (rem n 10)
>>           r (quot n 10)]
>>       (conj (explode-to-digits r) d
>>
>> or if you want to avoid consuming stack
>>
>> (defn explode-to-digits* [n out]
>>   (if (= n 0)
>>     out
>>     (let [d (rem n 10)
>>           r (quot n 10)]
>>       (recur r (cons d out)
>>
>> (defn explode-to-digits [n]
>>   (explode-to-digits* n nil))
>>
>> It should also be possible to make a lazy sequence version, but
>> returning the digits in the reverse order.
>
> A bit shorter:
>
> (defn digits [n]
>  (let [r (rem n 10)
>        q (int (/ n 10))
>        _ (println (format "q=%d, r=%d" q r))]
>    (if (zero? q) [r]
>      (into [r] (digits q)
>
> Not efficient though.

If you really want efficient:

(defn digits [n]
  (loop [n (int n)
 out nil]
(if (zero? n)
  out
  (recur
(unchecked-divide n 10)
(cons
  (unchecked-remainder n 10)
  out)

If you really /really/ want efficient:

(defn digits [n dig-max]
  (let [a (int-array dig-max)]
(loop [n (int n) i (int (dec dig-max))]
  (when-not (zero? n)
(aset a i (unchecked-remainder n 10))
(recur
  (unchecked-divide n 10)
  (unchecked-dec i
(seq a)))

Icky but should be fastest possible in Clojure. This one will throw an
exception if dig-max is too small and produce leading zeros if it's
bigger than the number of digits of n, and it will truncate inputs
that don't fit in a 32-bit signed int. You can wrap in (drop-while
zero? ...) to lose the latter behavior but it will slow things down,
or use Math/log10 and slow things down even more.

All of the variations I've posted will turn negative numbers into seqs
of digits that are all negated, e.g. (-1 0 -2 -4 -5 -6) and are liable
to hang, blow the heap (pretty slowly), or blow the stack (variously)
on fractions of any kind (Ratios, doubles, etc.).

In practice, if I needed something like this in production code, I'd
probably use one of the nicely idiomatic versions from earlier --
after all, the complexity is logarithmic in the magnitudes of the
numbers you're using anyway -- and handle negative and fractional
inputs in some more elegant manner. I'd probably also generalize it to
accept an arbitrary radix as an optional second parameter. This would
simply replace all the literal 10s in the code. It too would need to
be sanity checked, for being an integer and >= 2.

-- 
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: compojure 0.6.0: problem getting post arguments with google app engine

2011-02-17 Thread James Reeves
On 17 February 2011 03:13, Zhenchao Li  wrote:
> This is how I define my app:
>
> (defroutes index
>   (GET "/" [] (main-page))
>   (GET "/form" [] (render-page "Vote" (render-form)))
>   (POST "/vote" {params :params} (post-vote params))
>   (route/not-found "Page not found"))
>
> (def app (site index))
>
> (defservice app)
>
> The site here is used to capture :params, which is new in compojure
> 0.6.0. However I'm getting a empty map in post-vote. I wonder what's
> wrong with the above code? Any suggestions? Hints?

There's nothing wrong with the above code in principle. The problem
likely lies either in your render-form function, or your post-vote
function.

Try running the following routes:

(defroutes main-routes
  (GET "/" [] "Main Page")
  (GET "/form" []
(str ""
 ""
 ""
 ""))
  (POST "/vote" {params :params}
(pr-str params))
  (route/not-found "Page not found"))

If you go to "/form" and submit the form there, you should get a page
that looks something like:

  {:test "foo"}

- 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: better error messages > smaller stack traces

2011-02-17 Thread Timo Mihaljov
On Tue, Feb 08, 2011 at 09:01:38AM -0500, Stuart Halloway wrote:
> Please let us know when you get a misleading error message from a
> macroexpansion, so we can make it better. Or contribute a patch along the 
> lines
> of [2].

Here's another error message that really threw me off for a while.

I have some code that looks like this:

(let [[x y] (nth @my-atom z)]
  ...)

Which occasionally failed with:

java.lang.UnsupportedOperationException: nth not supported on this type: 
Float
at clojure.lang.RT.nthFrom(RT.java:815) ~[clojure-1.2.0.jar:na]
at clojure.lang.RT.nth(RT.java:765) ~[clojure-1.2.0.jar:na]

I tried to figure out how a single float could have ended up in that
atom and why I couldn't catch it by checking for `(coll? @my-atom)`.
After a while I found out that `@my-atom` indeed contained a
collection -- a collection of floats -- and that the exception was
thrown by the destructuring.

An error message something like this would have been much more helpful:

Can't destructure `(nth @my-atom z)` to `[x y]`: expected a
collection with 2 or more items but got `Float`.

--
Timo

-- 
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: 1st script, last hurtle...

2011-02-17 Thread mss
Armando, James, & Mike.

Thank you very much for the help, I'll certainly put your ideas to
good use.

Appreciate it!

-- 
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: Java Agent Based Modeling Systems and Clojure

2011-02-17 Thread ky...@brandeis.edu
Hi Fred,

I've just finished prototyping clj-breve (http://spiderland.org/)
everything works but the integration isn't as elegant as I would like,
hence why I haven't released it yet. The current paradigm for using
clj-breve is to develop models in Clojure then export to breve. More
details when I post this later in the Spring.

Best,
Kyle

On Feb 17, 12:46 am, Fred Concklin  wrote:
> Hi,
>
> I've been surveying some agent based modeling systems for use in a
> project I am doing in clojure. I've narrowed down the list to those
> below. I am wondering if anybody here has experience with these or has
> any thoughts about choosing one over the other for use with clojure. I
> will probably write a clojure wrapper for the abms I end up using.
>
> list (bare)
>
> * mason
> * jaslibrary
> * netlogo
> * repast
> * swarm
>
> list (w/ tags, links, annotations)
>
> *** agent based libraries java
>  mason                                                  :java:fast:small:
> * models independent from visualization
> * models self contained / run inside other frameworks + apps
>  jaslibrary [[http://jaslibrary.sourceforge.net/]]                  :java:
> * seems old (last update 2006)
>  netlogo [[http://ccl.northwestern.edu/netlogo/resources.shtml]]  
> :java:scala:good:
> * good community
>  repast [[http://repast.sourceforge.net/docs.html]]                    
> :java:
> * clusters
> * java api 
> [[http://repast.sourceforge.net/docs/api/repastjava/index.html]]
>  swarm                                                             :java:
> * obj.c > java                                               :support:
> * good community
>
> list of libs
> [[http://www.swarm.org/index.php/Tools_for_Agent-Based_Modelling]]
>
> cheers,
>
> fpc

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


Specialize collection

2011-02-17 Thread pba
Is there a better way to specialize an built-in Clojure collection
except wrapping - like for example to create a fixed size queue that
drops new elements while full ?

-- 
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: compojure 0.6.0: problem getting post arguments with google app engine

2011-02-17 Thread Zhenchao Li
Ah!
Turns out when I started the server using appengine.server/start-
server I accidently passed in the original index handler! No wonder it
did not work. It works just fine now. Thanks for your reply!

On Feb 17, 4:07 pm, Zmitro Lapcjonak  wrote:
> On Feb 17, 5:13 am, Zhenchao Li  wrote:
>
> >    (POST "/vote" {params :params} (post-vote params))
> > The site here is used to capture :params, which is new in compojure
> > 0.6.0. However I'm getting a empty map in post-vote. I wonder what's
> > wrong with the above code? Any suggestions? Hints?
>
> Have you verified, that your html form really sends the params?
> Also you can wrap your handler "index" with logging middleware,
> to see, what is your request.
>
> --
> Zmi La

-- 
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: Boston Clojure Meetup Thursday Jan 13

2011-02-17 Thread Shree Mulay
When is the next one?

On Feb 16, 9:24 am, rob levy  wrote:
> By the way, as those who attend the first meeting know, it was actually
> quite huge in terms of the number of people who attended.  There are less
> than 40 people who list themselves on the meetup page as members, but closer
> to 50 people actually attended if I estimate correctly, in part due to a
> large contingent from Sonian's Clojure team who came from around the world,
> as well as a big group of Schemers and Haskellers from Northeastern that
> happened to show up.
>
>
>
>
>
>
>
> On Wed, Feb 16, 2011 at 9:16 AM, rob levy  wrote:
> > No one has planned anything yet, but my sense is that anyone who wants to
> > take initiative to host or help plan a second meeting should get in touch
> > with Eric (who created the meetup.com page and organized the first
> > meeting), so as to coordinate with him and post the details on meetup.
> > Also, there is a "next topic" thread at
> >http://www.meetup.com/Boston-Clojure-Group/messages/boards/thread/102...if 
> >you haven't already joined the meetup that would also be a good way
> > to stay up to date with any news.
>
> > On Sat, Feb 12, 2011 at 5:04 PM, David Jacobs <
> > develo...@allthingsprogress.com> wrote:
>
> >> I don't know how I missed this originally. Is there another one planned?
>
> >> --
> >> You received this message because you are subscribed to the Google
> >> Groups "Clojure" group.>> To post to this group, send email 
> >> tocloj...@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: (identical? "foo" "foo") evaluates to true

2011-02-17 Thread Matthew Boston
I think I recall something in a CS class about how String is (or
possibly is) implemented.  Consider the following:

1) Each character of the alphabet, number, symbol etc. is assigned a
memory location.  0x00, 0x01, 0x02, etc. etc.
2) A String has an internal representation as a char[].

So, String s = "test" is really a char[] = ['t', 'e', 's', 't']

To represent this in memory, the char[] is really [0x20, 0x05, 0x19,
0x20] to represent each letter respectively.  Now conside a new String
s2 = "test".

Again, this will have the same "path" of internal memory addresses.
Therefore (identical? "test" "test") is the same path of memory
addresses and is identical?.

On Feb 16, 3:33 am, "C. Arel"  wrote:
> Thank you all,
> It has to be the same object otherwise it makes no sense. Anyways that
> is good news since this means that Clojure has a little support built
> in so you don't create unneccessary objects.
>
> /Can Arel
>
> On 16 Feb, 00:27, Stuart Sierra  wrote:
>
>
>
>
>
>
>
> > Since about 1.1, I think, the Clojure compiler calls String.intern() on
> > String literals.  So yes, they are the same object.
>
> > -Stuart Sierra

-- 
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: compojure 0.6.0: problem getting post arguments with google app engine

2011-02-17 Thread Zhenchao Li
It turns out I used this code to start my local dev server using
appengine.server/start-server:
(start-server index :directory "./war" :join? false :port 8080)
forgot to replace index with the new "app". It works fine now, post
arguments are correctly captured.

By the way, when deploying the project it seems compojure somehow uses
java.rmi.server.UID if you use compojure.handler, and on app engine
java.rmi.server.UID is not usable. I'm using ring.middleware to
construct a proper usable handler for appengine, like this: (Almost
the same as (site index) except that I don't use wrap-multipart, which
probably uses that package)

(def app (-> index
 wrap-keyword-params
 wrap-nested-params
 wrap-params
 wrap-cookies
 wrap-session))

Seems to work without problems on appengine so far, hope it might be
useful for people googling about similar errors.

On Feb 17, 7:02 pm, James Reeves  wrote:
> On 17 February 2011 03:13, Zhenchao Li  wrote:
>
> > This is how I define my app:
>
> > (defroutes index
> >   (GET "/" [] (main-page))
> >   (GET "/form" [] (render-page "Vote" (render-form)))
> >   (POST "/vote" {params :params} (post-vote params))
> >   (route/not-found "Page not found"))
>
> > (def app (site index))
>
> > (defservice app)
>
> > The site here is used to capture :params, which is new in compojure
> > 0.6.0. However I'm getting a empty map in post-vote. I wonder what's
> > wrong with the above code? Any suggestions? Hints?
>
> There's nothing wrong with the above code in principle. The problem
> likely lies either in your render-form function, or your post-vote
> function.
>
> Try running the following routes:
>
> (defroutes main-routes
>   (GET "/" [] "Main Page")
>   (GET "/form" []
>     (str ""
>          ""
>          ""
>          ""))
>   (POST "/vote" {params :params}
>     (pr-str params))
>   (route/not-found "Page not found"))
>
> If you go to "/form" and submit the form there, you should get a page
> that looks something like:
>
>   {:test "foo"}
>
> - 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: (identical? "foo" "foo") evaluates to true

2011-02-17 Thread Meikel Brandmeyer
Hi,

On 17 Feb., 06:52, Matthew Boston  wrote:

> Again, this will have the same "path" of internal memory addresses.
> Therefore (identical? "test" "test") is the same path of memory
> addresses and is identical?.

This is the = way. identical? means you really have the same array.
Not only the same contents.

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: Specialize collection

2011-02-17 Thread Michael Fogus
> Is there a better way to specialize an built-in Clojure collection
> except wrapping - like for example to create a fixed size queue that
> drops new elements while full ?

You can use protocols as in the example at https://gist.github.com/831830

Is this what you were looking for?

-- 
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: Specialize collection

2011-02-17 Thread pba
Not quite, the queue size needs to be passed in at instantiation time
for example you may want to have a 10 or 100 element queue depending
on the element type. What I'm looking for is to somehow decorate the
queue (or some other object) with behavior constraints (a la C++
traits) when the object is created.

Thanks!

On Feb 17, 9:36 am, Michael Fogus  wrote:
> > Is there a better way to specialize an built-in Clojure collection
> > except wrapping - like for example to create a fixed size queue that
> > drops new elements while full ?
>
> You can use protocols as in the example athttps://gist.github.com/831830
>
> Is this what you were looking for?

-- 
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: Specialize collection

2011-02-17 Thread Alexandre Patry

On 11-02-17 10:33 AM, pba wrote:

Not quite, the queue size needs to be passed in at instantiation time
for example you may want to have a 10 or 100 element queue depending
on the element type. What I'm looking for is to somehow decorate the
queue (or some other object) with behavior constraints (a la C++
traits) when the object is created.

Thanks!

You can define a record like :

(defrecord BoundedQueue [queue size])

where queue is the decorated queue and then consider the size of the 
queue in your protocole extension.


--
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 prime? function

2011-02-17 Thread HB
This code looks it working:

(defn prime-2? [num]
  (loop [i 2]
(if (<= (* i i) num)
  (if (zero? (mod num i))
false
(recur (inc i)))
  true)))

Any potential logic error?

On Feb 17, 2:01 am, Marek Stępniowski  wrote:
> On Thu, Feb 17, 2011 at 12:34 AM, HB  wrote:
> > I'm trying to write a function that determines if a number is a prime
> > or not.
> > Here is my first shot:
>
> > (defn prime? [num]
> >  (loop [i 2]
> >    (if (<= (* i i) num)
> >      false)
> >    (recur (inc i)))
> >  true)
>
> > It is not working to be sure :)
>
> > Please blow my mind with various implementations.
>
> It seems that you're mixing loop expression in Clojure with a Java
> loop. The loop in Clojure is just a binding and a point to which each
> recur will jump, instead of the start of function. You're lacking a
> stop condition in the loop.
>
> Here is the solution similiar to yours, but with a proper accumulator
> and stop condition:
>
> (defn prime? [n]
>  (let [m (Math/sqrt n))]
>    (loop [k 2]
>      (cond
>       (> k m) true
>       (zero? (mod n k)) false
>       true (recur (inc k))
>
> IMHO more idiomatic Clojure solution, using seq operations:
>
> (defn prime? [n]
>  (if (< n 2)
>    false
>    (not-any? #(zero? (rem n %))
>              (range 2 (min (inc (Math/sqrt n)) n)
>
> Cheers,
> --
> Marek Stępniowskihttp://stepniowski.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: compojure 0.6.0: problem getting post arguments with google app engine

2011-02-17 Thread James Reeves
On 17 February 2011 13:54, Zhenchao Li  wrote:
> By the way, when deploying the project it seems compojure somehow uses
> java.rmi.server.UID if you use compojure.handler, and on app engine
> java.rmi.server.UID is not usable. I'm using ring.middleware to
> construct a proper usable handler for appengine, like this: (Almost
> the same as (site index) except that I don't use wrap-multipart, which
> probably uses that package)

I'm working on a rewrite of wrap-multipart-params that should work on
locked down environments like Google App Engine, as well as
maintaining backward compatibility. You'll also be able to specify
custom storage backends, e.g. uploading multipart files directly to an
S3 bucket.

When I have something usable, I'll create a thread on the Ring group
to discuss it.

- 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: Get digits of a number

2011-02-17 Thread JMatt


On Feb 16, 10:29 pm, Andreas Kostler
 wrote:
> Is there an easy and idiomatic way of getting the digits of a number in 
> clojure?

Here is my attempt at this from a few months ago:

(defn to-digit
  "Create a seq of digits from a number."
  ^{:user/comment "For Euler Problems (Specifically 16)"}
  [i & {:keys [type] :or {type int}}]
   (let [ss (str i)] (map type (map (fn [s] (- (int s) 48)) ss

I took a more pragmatic approach to this problem and just assumed the
representation for digits was ASCII. It's possible to simplify this
some but, as is, it'll take seqs of characters or strings. At the time
I remember thinking this function should really return bytes or shorts
or something smaller than an int. Thus the :type option.


JMatt

-- 
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: Get digits of a number

2011-02-17 Thread Mike Meyer
On Thu, 17 Feb 2011 10:26:05 -0800 (PST)
JMatt  wrote:
> On Feb 16, 10:29 pm, Andreas Kostler
>  wrote:
> > Is there an easy and idiomatic way of getting the digits of a number in 
> > clojure?
> Here is my attempt at this from a few months ago:

My turn...

(defn to-digits
  "Create a seq of digits from a number."
  [i]
  ^{:user/comment "For Euler Problems (Specifically 16)"}
  (map {\0 0 \1 1 \2 2 \3 3 \4 4 \5 5 \6 6 \7 7 \8 8 \9 9}
   (str  i)))

No assumption about representation here. But my Python background is
showing - Pythons dictionaries are used *everywhere* in the language,
and hence tuned as tightly as possible and thus blasted fast.

 http://www.mired.org/consulting.html
Independent Software developer/SCM consultant, email for more information.

O< ascii ribbon campaign - stop html mail - www.asciiribbon.org

-- 
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: Specialize collection

2011-02-17 Thread pba
Combining the two approaches:
(defn bounded-queue [size]
   (with-meta (clojure.lang.PersistentQueue/EMPTY) {:bounded-size
size}))

then look for (get (meta q) :bounded-size) in the protocol. Nice ...

Thanks!

On Feb 17, 11:04 am, Alexandre Patry 
wrote:
> On 11-02-17 10:33 AM, pba wrote:> Not quite, the queue size needs to be 
> passed in at instantiation time
> > for example you may want to have a 10 or 100 element queue depending
> > on the element type. What I'm looking for is to somehow decorate the
> > queue (or some other object) with behavior constraints (a la C++
> > traits) when the object is created.
>
> > Thanks!
>
> You can define a record like :
>
> (defrecord BoundedQueue [queue size])
>
> where queue is the decorated queue and then consider the size of the
> queue in your protocole extension.

-- 
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: Specialize collection

2011-02-17 Thread pba
The code from my previous post is not correct, cons for example
affects metadata:
(def q (bounded-queue 3))
(meta q) -> { :bounded-size 3}
(meta (cons 1 qm)) -> nil


On Feb 17, 3:19 pm, pba  wrote:
> Combining the two approaches:
> (defn bounded-queue [size]
>    (with-meta (clojure.lang.PersistentQueue/EMPTY) {:bounded-size
> size}))
>
> then look for (get (meta q) :bounded-size) in the protocol. Nice ...
>
> Thanks!
>
> On Feb 17, 11:04 am, Alexandre Patry 
> wrote:
>
>
>
>
>
>
>
> > On 11-02-17 10:33 AM, pba wrote:> Not quite, the queue size needs to be 
> > passed in at instantiation time
> > > for example you may want to have a 10 or 100 element queue depending
> > > on the element type. What I'm looking for is to somehow decorate the
> > > queue (or some other object) with behavior constraints (a la C++
> > > traits) when the object is created.
>
> > > Thanks!
>
> > You can define a record like :
>
> > (defrecord BoundedQueue [queue size])
>
> > where queue is the decorated queue and then consider the size of the
> > queue in your protocole extension.

-- 
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: Get digits of a number

2011-02-17 Thread Michael Gardner
On Feb 17, 2011, at 1:36 PM, Mike Meyer wrote:

> My turn...
> 
> (defn to-digits
>  "Create a seq of digits from a number."
>  [i]
>  ^{:user/comment "For Euler Problems (Specifically 16)"}
>  (map {\0 0 \1 1 \2 2 \3 3 \4 4 \5 5 \6 6 \7 7 \8 8 \9 9}
>   (str  i)))
> 
> No assumption about representation here. But my Python background is
> showing - Pythons dictionaries are used *everywhere* in the language,
> and hence tuned as tightly as possible and thus blasted fast.

Why not use Character/digit, as Saul suggested?

-- 
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: Functional program design concepts

2011-02-17 Thread MS


On Feb 15, 3:10 pm, Sean Corfield  wrote:
> On Tue, Feb 15, 2011 at 2:53 PM, MS <5lvqbw...@sneakemail.com> wrote:
> > Because I'm not sure how else to use (for example) a graph library and
> > still have it look like a circuit, rather than a graph.
>
> Almost any such graph library is going to be a bunch of functions that
> operate on a data structure. Your circuit can be that data structure
> so you can simply use the functions on it. I suspect you're assuming a
> level of type checking, from statically typed OO languages, that just
> doesn't need to be there?
>
> > The specialization of changing the abstract thing
> > into the concrete thing is basically to reduce some of the graph
> > functionality (for example in a circuit a node can't have more than
> > one unique edge on it) and renaming things so they match the domain
> > (eg "vertex" become "node", "edge" become "net"), and add extra stuff
> > such as PCB layer, simulation model, etc.
>
> Sounds like you just need a set of functions that match your domain,
> which wrap the graph library (which in turn operates on the underlying
> graph data structure that represents your circuit).
>
> > Yes, I finally figured out how to have a cyclic structure using refs
> > and dosyncs.
>
> I was thinking of something much simpler - a pure data structure.

At first it was simple, but I want a net to point to its nodes, and
the nodes to point to its parent net.
Actually what originally started this was I wanted a way to pre-sort
the circuit database (for example resistors, capacitors, and their
associated parameters) in a way that allowed for constant time
access... so (this was in python...) I figured out how to create a map
where the parameters were the keys, so if you wanted to search for a
given parameter you'd have to pick the right map.  Then I realized
this is probably not necessary or practical even for extremely large
circuits.  This turned into figuring out how to wire up the
components, and here I am :)

>
> > In clojure there is a defstruct...
>
> This is why I think you're trying to apply a strict type system which
> will get in the way of a simple solution. You want named types with
> specific behaviors because that's more like the OO you're used to?

Yes that's what I'm used to, and what's funny is that this is almost
exactly what the samples in SICP were moving towards, with tag-based
dispatch and functions associated with lists of stuff.

In a way just having a "to the metal" type of data structure feels a
little loosy goosy I think I want to be able to ask about what
type of structure this is... but I suppose I could tag it with a
{:type :ckt} or something.  Kind of feels like I should have more
infrastructure... so maybe this is just the mental shift I need to
move away from OO :)

-- 
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: Functional program design concepts

2011-02-17 Thread MS


On Feb 15, 4:12 pm, James Reeves  wrote:
> On 15 February 2011 22:53, MS <5lvqbw...@sneakemail.com> wrote:
>
> >> So an electrical circuit is a data structure containing vertices and
> >> edges and describing how they are connected. Then you'll have some
> >> functions that operate on that data structure.
>
> > So... how do I use someone else's implementation of some type of graph
> > algorithm as applied to my circuit structure, unless I use their
> > structure?
>
> The short answer is protocols:http://clojure.org/protocols.
>

Cool thanks I'll take a look.

-- 
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: Functional program design concepts

2011-02-17 Thread MS

> If your domain model can be represented by a simple vector / map /
> set, then you have a very rich set of tools (in Clojure) to operate on
> your domain model. If your domain model is represented by fixed types,
> you have to write all sorts of wrapper functions to be able to apply
> those operations. One of the nicest things about Clojure in this area
> is that once you have a basic set of operations defined on your simple
> data structures, it's easy to incrementally wrap it up in as much of a
> typed API as you want via records, protocols and macros etc to create
> an aesthetically pleasing façade around the core functionality.
>
> Hope that makes sense?

In principle it makes sense!  In practice I need some practice :)
Actually I need to finish my home remodel so I can get to writing code
in the evenings!

-- 
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: Get digits of a number

2011-02-17 Thread Mike Meyer
On Thu, 17 Feb 2011 15:27:47 -0600
Michael Gardner  wrote:

> On Feb 17, 2011, at 1:36 PM, Mike Meyer wrote:
> 
> > My turn...
> > 
> > (defn to-digits
> >  "Create a seq of digits from a number."
> >  [i]
> >  ^{:user/comment "For Euler Problems (Specifically 16)"}
> >  (map {\0 0 \1 1 \2 2 \3 3 \4 4 \5 5 \6 6 \7 7 \8 8 \9 9}
> >   (str  i)))
> Why not use Character/digit, as Saul suggested?

Because I'm not a java programmer, so my natural inclination is to use
Clojure tools (like the hashmap) rather than Java tools. Since I
hadn't seen a solutions using the hashamp - but had seen some more
complex variants - I thought this one might be of interest.

http://www.mired.org/consulting.html
Independent Software developer/SCM consultant, email for more information.

O< ascii ribbon campaign - stop html mail - www.asciiribbon.org

-- 
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: Specialize collection

2011-02-17 Thread Ken Wesson
You can also create new types that act mostly like Clojure's existing
types using deftype and implementing things like IPersistentStack.

I posted code for (among other things) a bounded deque (using a
ringbuffer internally) here fairly recently.

The one limitation with these things is that they don't print and then
read back as the same type via the Clojure reader -- but then you get
the same problem with the built-in sorted-set (comes back a normal
set) and sorted-map (comes back a normal map) too.

The decorator concept using metadata is interesting, though, because
it has the potential to overcome that limitation. The protocol
functions would have to look for the metadata and then dispatch on
something in it; and you'd have to use print-meta to make the metadata
survive a round trip through prn and the reader; but it could in
principle be done. Something like

(defprotocol MyStack
  (my-pop* [type-marker stack])
  (my-peek* [type-marker stack])
  (my-push* [type-marker stack obj]))

(defn my-pop [stack]
  (.my-pop* (:type-marker (meta stack)) stack))

(defn my-peek [stack]
  (.my-peek* (:type-marker (meta stack)) stack))

(defn my-push [stack obj]
  (.my-push* (:type-marker (meta stack)) stack obj))

(deftype NormalStackMarker []
  MyStack
(my-pop* [_ stack] (pop stack))
(my-peek* [_ stack] (peek stack))
(my-push* [_ stack obj] (conj stack obj)))

(def normal-stack-marker {:type-marker (NormalStackMarker.)})

(defn normal-stack []
  (with-meta [] normal-stack-marker))

(deftype BoundedStackMarker []
  MyStack
(my-pop* [_ stack] (pop stack))
(my-peek* [_ stack] (peek stack))
(my-push* [_ stack obj]
  (let [limit (:limit (meta stack))]
...)))

(def bounded-stack-marker (BoundedStackMarker.))

(defn bounded-stack [capacity]
  (with-meta [] {:type-marker bounded-stack-marker :limit capacity}))

... or something like this. The prn/reader trip just has to handle a
normal vector with metadata, so *print-meta* is the only maybe-unusual
step needed to make these round-trip intact.

-- 
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: Trouble with type hints

2011-02-17 Thread Nick
Is there something besides type-hinting that I'm missing?

-- 
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: Get digits of a number

2011-02-17 Thread Matthew Boston
How about using the Java api Character/getNumbericValue, like so:

(defn explode-to-digits [number]
  (map #(Character/getNumericValue %) (str number)))

user => (explode-to-digits 12345)
(1 2 3 4 5)

On Feb 17, 4:45 pm, Mike Meyer  wrote:
> On Thu, 17 Feb 2011 15:27:47 -0600
>
> Michael Gardner  wrote:
> > On Feb 17, 2011, at 1:36 PM, Mike Meyer wrote:
>
> > > My turn...
>
> > > (defn to-digits
> > >  "Create a seq of digits from a number."
> > >  [i]
> > >  ^{:user/comment "For Euler Problems (Specifically 16)"}
> > >  (map {\0 0 \1 1 \2 2 \3 3 \4 4 \5 5 \6 6 \7 7 \8 8 \9 9}
> > >       (str  i)))
> > Why not use Character/digit, as Saul suggested?
>
> Because I'm not a java programmer, so my natural inclination is to use
> Clojure tools (like the hashmap) rather than Java tools. Since I
> hadn't seen a solutions using the hashamp - but had seen some more
> complex variants - I thought this one might be of interest.
>
>     --
> Mike Meyer           http://www.mired.org/consulting.html
> Independent Software developer/SCM consultant, email for more information.
>
> O< ascii ribbon campaign - stop html mail -www.asciiribbon.org

-- 
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: compojure 0.6.0: problem getting post arguments with google app engine

2011-02-17 Thread Zhenchao Li
Great, that would be nice!

On Feb 18, 12:44 am, James Reeves  wrote:
> On 17 February 2011 13:54, Zhenchao Li  wrote:
>
> > By the way, when deploying the project it seems compojure somehow uses
> > java.rmi.server.UID if you use compojure.handler, and on app engine
> > java.rmi.server.UID is not usable. I'm using ring.middleware to
> > construct a proper usable handler for appengine, like this: (Almost
> > the same as (site index) except that I don't use wrap-multipart, which
> > probably uses that package)
>
> I'm working on a rewrite of wrap-multipart-params that should work on
> locked down environments like Google App Engine, as well as
> maintaining backward compatibility. You'll also be able to specify
> custom storage backends, e.g. uploading multipart files directly to an
> S3 bucket.
>
> When I have something usable, I'll create a thread on the Ring group
> to discuss it.
>
> - 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: Get digits of a number

2011-02-17 Thread Robert McIntyre
I normally use

(defn digits [n]
  (map #(Integer/parseInt (str %))  (seq (str n

You can adapt it to read in different bases easily.

sincerely,
--Robert McIntyre


On Thu, Feb 17, 2011 at 9:45 PM, Matthew Boston
 wrote:
> How about using the Java api Character/getNumbericValue, like so:
>
> (defn explode-to-digits [number]
>  (map #(Character/getNumericValue %) (str number)))
>
> user => (explode-to-digits 12345)
> (1 2 3 4 5)
>
> On Feb 17, 4:45 pm, Mike Meyer  wrote:
>> On Thu, 17 Feb 2011 15:27:47 -0600
>>
>> Michael Gardner  wrote:
>> > On Feb 17, 2011, at 1:36 PM, Mike Meyer wrote:
>>
>> > > My turn...
>>
>> > > (defn to-digits
>> > >  "Create a seq of digits from a number."
>> > >  [i]
>> > >  ^{:user/comment "For Euler Problems (Specifically 16)"}
>> > >  (map {\0 0 \1 1 \2 2 \3 3 \4 4 \5 5 \6 6 \7 7 \8 8 \9 9}
>> > >       (str  i)))
>> > Why not use Character/digit, as Saul suggested?
>>
>> Because I'm not a java programmer, so my natural inclination is to use
>> Clojure tools (like the hashmap) rather than Java tools. Since I
>> hadn't seen a solutions using the hashamp - but had seen some more
>> complex variants - I thought this one might be of interest.
>>
>>    > --
>> Mike Meyer           http://www.mired.org/consulting.html
>> Independent Software developer/SCM consultant, email for more information.
>>
>> O< ascii ribbon campaign - stop html mail -www.asciiribbon.org
>
> --
> 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