Maven vs Ivy (or Leiningen)

2011-01-21 Thread OGINO Masanori
Hello.

Clojure will use both Ant and Maven 2. (Maven 3 may be used in the future?)

However, if we see management software world widely, there is Ivy.
Could I ask you whether Ivy is well enough or not?

; Yes, there is another notable thing, Leiningen written in Clojure.
; It sounds interesting that "Clojure uses Clojure itself in build
process" but I can't consider if possible or not.

Here are some useful pages:
http://dev.clojure.org/display/design/Common+Contrib+Build
http://dev.clojure.org/pages/viewpage.action?pageId=950842
http://ant.apache.org/ivy/m2comparison.html

Thank you.

-- 
Name:  OGINO Masanori (荻野 雅紀)
E-mail: masanori.og...@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


Cute demonstration of agent update semantics

2011-01-21 Thread Ken Wesson
(defn evens []
  (iterate (partial + 2) 0))

(defn odds []
  (iterate (partial + 2) 1))

(def foo (agent []))

(defn push-foo [s]
  (doseq [x s] (Thread/sleep (rand-int 100)) (send foo conj x)))

(defn pusher-threads [ss]
  (map
(fn [s] (Thread. #(do (push-foo s) (println "done!"
ss))

(doseq [t (pusher-threads [(take 10 (evens)) (take 10 (odds))])]
  (.start t))

foo



Typical output:

user=> (doseq [t (pusher-threads [(take 10 (evens)) (take 10
(odds))])] (.start t))
nil
done!
done!

user=> foo
#

user=> (filter even? @foo)
(0 2 4 6 8 10 12 14 16 18)

user=> (filter odd? @foo)
(1 3 5 7 9 11 13 15 17 19)



The odds are interleaved more or less randomly with the evens, but the
odds are themselves in sequence, as are the evens, and nothing is
missing. This demonstrates the documented behavior of agents: no race
conditions and the sends from any single thread are processed in the
order they were sent.

This might be useful for someone just learning clojure. (Other things
demonstrated: creation of simple infinite integer sequences with
iterate; avoiding holding onto the heads of same by not def'ing them
directly but instead defining generator functions; doseq for running a
side effect for each element of a collection, x2; Java interop with
the Thread class: static method invocation, constructor call with
argument, and instance method invocation; rand-int; println for
monitoring execution of code; do for bundling multiple expressions in
an if clause or #(...); accumulating stuff in a vector with conj;
closures, both (fn ...) and #(...), and partial to generate functions
in-line; even? and odd?; def and defn; and map, take, and filter.)



A detailed breakdown of the code that may also be useful for beginners:

The evens function returns (iterate (partial + 2) 0), which returns a
sequence starting with 0 and continuing by adding 2 to each previous
item -- mathematically, a recurrence. Since it's just adding a fixed
amount each step, it's a special subtype called an arithmetic
progression. In this case it's just the nonnegative even numbers in
ascending order.

The odds function does almost the same thing, but the starting point
of its sequence is 1 and so every number is one higher than in evens.
This obviously yields up the nonnegative odd numbers.

Directly defining vars holding these sequences would hold onto their
heads; generally a bad idea with large or infinite sequences. Instead
it's better to define a no-arg function to produce fresh instances as
needed, so you can grab a copy, work with it, and lose its head. Hence
making evens and odds functions rather than vars directly holding the
sequences.

The foo agent initially holds an empty vector.

The push-foo function takes a sequence of objects and pushes them onto
the agent's vector using (send foo conj x). The vector thus
accumulates the values. It waits a random interval before each push,
however, up to 100ms, using the Thread/sleep Java method. This ensures
that the threads don't run so fast that one finishes in the time it
takes for the other to be launched (the start method seems to be quite
slow, and, the way this code is written, the .start calls use
reflection), or interleave too predictably (say, [0 1 2 3 ...]).

The pusher-threads function takes a sequence of sequences and for each
sequence s creates a Thread whose body is (do (push-foo s) (println
"done!")). # #(do (push-foo s) (println "done!")) encapsulates that as
a closure, (Thread. # #(do (push-foo s) (println "done!"))) creates a
Thread object with that closure as the code it will run when started,
and the (fn [s] ...) form wraps this up in a larger closure, which map
runs for each sequence in the input to produce a sequence of threads,
each of which will, when started, run push-foo on one of the input
sequences and then announce that it is done.

The doseq line that does the deed is trickier as there's lots going on
in it. (take 10 (evens)) calls (evens) to get an instance of the
even-numbers sequence and chops off the tail to make it finite. (take
10 (odds)) does likewise with the odd numbers. The two are enclosed in
square brackets [...] -- an expression that will evaluate to a
two-element vector, one with the first ten nonnegative even numbers
followed by the first ten nonnegative odd numbers. Calling
pusher-threads on this vector produces a sequence of two pusher
threads, one to push the evens and one to push the odds. The doseq
itself runs over these threads starting each one with (.start t),
which invokes the Java instance method start() on the Thread t,
launching it.

The doseq returns nil but the threads chug along autonomously and
concurrently, waiting random intervals and sending conjes to the agent
foo holding the initially empty vector. Each prints a "done!" line
when it is finished with its sequence of numbers to send to the agent
to conj onto the vector.

After roughly one second, both should be done on a dual-core-or-more
machine; it could take up to two se

Distinguishing a map from a record

2011-01-21 Thread Brian Marick
In the absence of #'record?, what's the safest way to tell whether a particular 
object is a true map or a record?

-
Brian Marick, Artisanal Labrador
Contract programming in Ruby and Clojure
Author of /Ring/ (forthcoming; sample: http://exampler.com/tmp/ring.pdf)
www.exampler.com, www.exampler.com/blog, www.twitter.com/marick

-- 
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: Distinguishing a map from a record

2011-01-21 Thread Christophe Grand
Hi!

On Fri, Jan 21, 2011 at 1:27 PM, Brian Marick  wrote:

> In the absence of #'record?, what's the safest way to tell whether a
> particular object is a true map or a record?
>

"true" maps extend clojure.lang.APersistentMap, records don't.

hth,

  Christophe

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

records, metas, and type checking

2011-01-21 Thread kony

If we try to write:

(defrecord Point [#^double x #^double y])

then create them as follows:

(Point. "sth")

we've got

java.lang.ClassCastException: java.lang.String cannot be cast to
java.lang.Number (NO_SOURCE_FILE:0)

and this is OK,

but If we put:

(defrecord vect2 [#^clojure.lang.IPersistentVector v])

instantiaion by a string seems to be OK

(vect2. "sth")

is it a bug or a feature?


-- 
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: records, metas, and type checking

2011-01-21 Thread Meikel Brandmeyer
Hi,

On 21 Jan., 14:00, kony  wrote:

> If we try to write:
>
> (defrecord Point [#^double x #^double y])
>
> then create them as follows:
>
> (Point. "sth")
>
> we've got
>
> java.lang.ClassCastException: java.lang.String cannot be cast to
> java.lang.Number (NO_SOURCE_FILE:0)
>
> and this is OK,
>
> but If we put:
>
> (defrecord vect2 [#^clojure.lang.IPersistentVector v])
>
> instantiaion by a string seems to be OK
>
> (vect2. "sth")
>
> is it a bug or a feature?

>From http://clojure.org/datatypes:

* fields can have type hints, and can be primitive
* note that currently a type hint of a non-primitive type will not
be used to constrain the field type nor the constructor arg, but will
be used to optimize its use in the class methods
* constraining the field type and constructor arg is planned

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: Why no def- ?

2011-01-21 Thread Aaron Bedra

On 01/21/2011 12:51 AM, Ken Wesson wrote:

On Fri, Jan 21, 2011 at 12:40 AM, Alex Baranosky
  wrote:

I've wanted to have private defs.  For defn, I just us defn-.  But there is
no def-

So I just use:

(defmacro def- [name&  decls]
 (list* `def (with-meta name (assoc (meta name) :private true)) decls))

There's a (defvar- ...) in clojure.contrib. Why there's nothing like
this in core remains a mystery to me. :)


You can also do

(def foo {:private true} (...))

which is just the basic functionality in core.

Cheers,

Aaron Bedra
--
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: Maven vs Ivy (or Leiningen)

2011-01-21 Thread faenvie
you may want to take a look at https://bitbucket.org/kotarak/clojuresque
which adapts the feature-rich gradle-buildsystem to implement build-
logic
for 'native' clojure-projects and also makes builds for mixed-
language-
projects and multi-projects quite easy.

by default the gradle-buildsystem uses ivy for dependency-management
but
is also able to process maven2-poms. IMO dependency-management via ivy
has some advantages compared to dependency-management via maven2.
ivy + ant is also a possible combination.

the released version of clojuresque is a little bit outdated ...
publication
of a snapshot or a release und update of the documentation would be
welcome ...

cheers



On Jan 21, 9:00 am, OGINO Masanori  wrote:
> Hello.
>
> Clojure will use both Ant and Maven 2. (Maven 3 may be used in the future?)
>
> However, if we see management software world widely, there is Ivy.
> Could I ask you whether Ivy is well enough or not?
>
> ; Yes, there is another notable thing, Leiningen written in Clojure.
> ; It sounds interesting that "Clojure uses Clojure itself in build
> process" but I can't consider if possible or not.
>
> Here are some useful 
> pages:http://dev.clojure.org/display/design/Common+Contrib+Buildhttp://dev.clojure.org/pages/viewpage.action?pageId=950842http://ant.apache.org/ivy/m2comparison.html
>
> Thank you.
>
> --
> Name:  OGINO Masanori (荻野 雅紀)
> E-mail: masanori.og...@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: Maven vs Ivy (or Leiningen)

2011-01-21 Thread faenvie
>>of a snapshot or a release und update of the documentation would be
>>welcome ...

after all ... its open-source-software and audience-participation
is probably welcome ;-)

-- 
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: Maven vs Ivy (or Leiningen)

2011-01-21 Thread Meikel Brandmeyer
Hi,

On 21 Jan., 15:02, faenvie  wrote:

> the released version of clojuresque is a little bit outdated ...
> publication
> of a snapshot or a release und update of the documentation would be
> welcome ...

Indeed. A new release will hopefully be available soon. A slightly
outdated SNAPSHOT is available from clojars as 1.4.0-SNAPSHOT. I will
update the SNAPSHOT over the weekend.

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: Maven vs Ivy (or Leiningen)

2011-01-21 Thread Stuart Sierra
Maven was the best choice for the kind of inherited configuration we need to 
make sure Clojure (and soon, contrib) releases get deployed from Hudson into 
the Sonatype open-source repository and then into the Maven Central 
repository.

You can certainly use Ivy, Leiningen, Gradel, or any other tool to build 
Clojure projects.

-Stuart Sierra
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: Why no def- ?

2011-01-21 Thread Stuart Sierra
And in Clojure 1.3:   (def ^:private foo ...)

-Stuart Sierra
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: Why no def- ?

2011-01-21 Thread Laurent PETIT
2011/1/21 Aaron Bedra 

> On 01/21/2011 12:51 AM, Ken Wesson wrote:
>
>> On Fri, Jan 21, 2011 at 12:40 AM, Alex Baranosky
>>   wrote:
>>
>>> I've wanted to have private defs.  For defn, I just us defn-.  But there
>>> is
>>> no def-
>>>
>>> So I just use:
>>>
>>> (defmacro def- [name&  decls]
>>> (list* `def (with-meta name (assoc (meta name) :private true))
>>> decls))
>>>
>> There's a (defvar- ...) in clojure.contrib. Why there's nothing like
>> this in core remains a mystery to me. :)
>>
>>  You can also do
>
> (def foo {:private true} (...))
>

With def, it will be :
user=> (def ^{:private true} foo "ba")
#'user/foo
user=> (meta (var foo))
{:ns #, :name foo, :file "NO_SOURCE_PATH", :line 4, :private
true}
user=>

It's with defn that you can use a map at a certain position:
user=> (defn foo {:private true} [] "bar")
#'user/foo
user=> (meta (var foo))
{:ns #, :name foo, :file "NO_SOURCE_PATH", :line 7,
:arglists ([]), :private true}
user=>


Cheers,

-- 
Laurent

-- 
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 no def- ?

2011-01-21 Thread Aaron Bedra

On 01/21/2011 09:22 AM, Laurent PETIT wrote:
2011/1/21 Aaron Bedra >


On 01/21/2011 12:51 AM, Ken Wesson wrote:

On Fri, Jan 21, 2011 at 12:40 AM, Alex Baranosky
mailto:alexander.barano...@gmail.com>>  wrote:

I've wanted to have private defs.  For defn, I just us
defn-.  But there is
no def-

So I just use:

(defmacro def- [name&  decls]
(list* `def (with-meta name (assoc (meta name)
:private true)) decls))

There's a (defvar- ...) in clojure.contrib. Why there's
nothing like
this in core remains a mystery to me. :)

You can also do

(def foo {:private true} (...))


With def, it will be :
user=> (def ^{:private true} foo "ba")
#'user/foo
user=> (meta (var foo))
{:ns #, :name foo, :file "NO_SOURCE_PATH", :line 4, 
:private true}

user=>

It's with defn that you can use a map at a certain position:
user=> (defn foo {:private true} [] "bar")
#'user/foo
user=> (meta (var foo))
{:ns #, :name foo, :file "NO_SOURCE_PATH", :line 7, 
:arglists ([]), :private true}

user=>


Doh!  I'm full of typos this week.  Do a combination of what I meant and 
what Laurent said and you'll be in a good place.


Cheers,

Aaron Bedra
--
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: just an observation about doseq...

2011-01-21 Thread Aaron Bedra

On 01/20/2011 07:53 AM, John Szakmeister wrote:

On Thu, Jan 20, 2011 at 5:38 AM, Baishampayan Ghose  wrote:

when we give an empty vector of seq-exprs to doseq it returns the value of
the last s-expression.. but returns nil when the
vector-of-seq-exprs is not empty.. may be this is the expected behaviour ..
but the documentation states otherwise ..

Yeah, that looks like a bug (either in the documentation or the code):
   (doseq [] 1) =>  1

doseq works just like do when the seq-exprs part is empty. This is
certainly not a bug in the code, but I believe the documentation can
be improved.

Relevant portion of the code:
https://github.com/clojure/clojure/blob/b578c69d7480f621841ebcafdfa98e33fcb765f6/src/clj/clojure/core.clj#L2395

Yep, I took a look at that before responding.  I'm just not sure what
the fix should be: make (doseq) return nil as advertised, or update
the documentation.  I tend towards the former, although the latter is
fine too. :-)  Either way, something should be fixed. :-)

I folks believe it's a documentation problem, I'm happy to whip up a
patch for that.

-John

Please create a ticket in JIRA for this.  If you have a patch and have 
signed the CA then all is good there.


Cheers,

Aaron Bedra
--
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: Maven vs Ivy (or Leiningen)

2011-01-21 Thread OGINO Masanori
Hello.

Thank you for responses, faenvie, Meikel and Stuart.
Yes, of course, we can use each favorite management software for own
Clojure project. (if teammates agree with :))
I asked if non-Maven stuffs can be used for Clojure itself officially or not.
And then, as Stuart says, Maven may be good with this situation and
which is not so surprising.
clojuresque sounds good, so I'll see it.
Although Ant

-- 
Name:  OGINO Masanori (荻野 雅紀)
E-mail: masanori.og...@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: Maven vs Ivy (or Leiningen)

2011-01-21 Thread OGINO Masanori
Hello.

Thank you for responses, faenvie, Meikel and Stuart.
Yes, of course, we can use each favorite management software for own
Clojure project. (if teammates agree with :))
I asked if non-Maven stuffs can be used for Clojure itself officially or not.
And then, as Stuart says, Maven may be good with this situation and
which is not so surprising.
clojuresque sounds good, so I'll see it.

Although they abuses XML, I often use Ant + Ivy and I have never hate Maven.
Also I like Leiningen but sometimes I'm confused with it because of my
ignorance.
On other languages, I use CMake, Rake, Autotools, MSBuild, etc.
I've never see the ultimate solution, but I write code and I want to
build it, so I (and you?) use them.

Anyway, I know that Clojure will use Maven surely.

Thank you.

-- 
Name:  OGINO Masanori (荻野 雅紀)
E-mail: masanori.og...@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: just an observation about doseq...

2011-01-21 Thread Baishampayan Ghose
> Please create a ticket in JIRA for this.  If you have a patch and have
> signed the CA then all is good there.

Done. http://dev.clojure.org/jira/browse/CLJ-722

My CA is on file.

Regards,
BG

-- 
Baishampayan Ghose
b.ghose at 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: just an observation about doseq...

2011-01-21 Thread Aaron Bedra

On 01/21/2011 10:31 AM, Baishampayan Ghose wrote:

Please create a ticket in JIRA for this.  If you have a patch and have
signed the CA then all is good there.

Done. http://dev.clojure.org/jira/browse/CLJ-722

My CA is on file.

Regards,
BG


Thanks!

--
Cheers,

Aaron Bedra
--
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: live-processing

2011-01-21 Thread Vilson Vieira
2011/1/21 MarkH :
> Cool stuff.  Works on Windows 7.

Thanks Mark. Thanks to test!

Cheers.

-- 
Vilson Vieira

vil...@void.cc

((( http://automata.cc )))

((( http://musa.cc )))

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


Re: ANN: Textmash - another IDE for Clojure

2011-01-21 Thread Laurent PETIT
2011/1/20 Jeff Rose 

> I'd also be interested in clojure paredit as a library.  We've been
> talking about having a stripped down editor for defining synthesizers
> and musical processes inside of Overtone, so some tools to get a
> useful little Clojure editing window would be great.
>


It seems that this is the week of lightweight Clojure text editors, if we
also add the "live-processing" initiative which uses one :-)

Seems indeed like the good "window" to start share more stuff.

I have a quick RC release of CCW to do, and then I'll place top on my list
working on externalizing paredit.clj .

The "problem" is that I'm still not happy with the way I've coded it, but
since it is "externally" quite stable and has a good test suite, it should
be consumable by other, I think.

Cheers,

-- 
Laurent

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

Help with java conversion

2011-01-21 Thread WoodHacker
I'm converting the java code examples in Killer Game Programming in
Java by Andrew Davison to Clojure and am having great fun doing it.
But I've hit wall where I can't seem to get the code to work.

The following code moves an animated gif strip to a java array:

  public BufferedImage[] loadStripImageArray(String fnm, int number)
  {
if (number <= 0) {
  System.out.println("number <= 0; returning null");
  return null;
}

BufferedImage stripIm;
if ((stripIm = loadImage(fnm)) == null) {
  System.out.println("Returning null");
  return null;
}

int imWidth = (int) stripIm.getWidth() / number;
int height = stripIm.getHeight();
int transparency = stripIm.getColorModel().getTransparency();

BufferedImage[] strip = new BufferedImage[number];
Graphics2D stripGC;

// each BufferedImage from the strip file is stored in strip[]
for (int i=0; i < number; i++) {
  strip[i] =  gc.createCompatibleImage(imWidth, height,
transparency);   <=

  // create a graphics context
  stripGC = strip[i].createGraphics();
<=
  // stripGC.setComposite(AlphaComposite.Src);

  // copy image
  stripGC.drawImage(stripIm,
  0,0, imWidth,height,
  i*imWidth,0, (i*imWidth)+imWidth,height,
  null);
  stripGC.dispose();
}
return strip;
  } // end of loadStripImageArray()

The problem I'm having is with the for loop.   If I create an abject
array of say 6 buffered images,  how do I reference the indexed image
array to match the lines pointed to?  In other words what is the
equivalent of strip[i] in Clojure?

I'm not exactly a beginner, but this has stumped me.

Bill


-- 
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: Help with java conversion

2011-01-21 Thread Justin Kramer
If you're setting values in an array, use aset:

(dotimes [i number]
  (aset strip i (.createCompatibleImage gc width height transparency))
  ...)

If you want to get a value, use aget:

(let [stripGC (.createGraphics (aget strip i))]
  ...)

Hope that helps,

Justin

On Jan 21, 11:07 am, WoodHacker  wrote:
> I'm converting the java code examples in Killer Game Programming in
> Java by Andrew Davison to Clojure and am having great fun doing it.
> But I've hit wall where I can't seem to get the code to work.
>
> The following code moves an animated gif strip to a java array:
>
>   public BufferedImage[] loadStripImageArray(String fnm, int number)
>   {
>     if (number <= 0) {
>       System.out.println("number <= 0; returning null");
>       return null;
>     }
>
>     BufferedImage stripIm;
>     if ((stripIm = loadImage(fnm)) == null) {
>       System.out.println("Returning null");
>       return null;
>     }
>
>     int imWidth = (int) stripIm.getWidth() / number;
>     int height = stripIm.getHeight();
>     int transparency = stripIm.getColorModel().getTransparency();
>
>     BufferedImage[] strip = new BufferedImage[number];
>     Graphics2D stripGC;
>
>     // each BufferedImage from the strip file is stored in strip[]
>     for (int i=0; i < number; i++) {
>       strip[i] =  gc.createCompatibleImage(imWidth, height,
> transparency);   <=
>
>       // create a graphics context
>       stripGC = strip[i].createGraphics();
> <=
>       // stripGC.setComposite(AlphaComposite.Src);
>
>       // copy image
>       stripGC.drawImage(stripIm,
>                   0,0, imWidth,height,
>                   i*imWidth,0, (i*imWidth)+imWidth,height,
>                   null);
>       stripGC.dispose();
>     }
>     return strip;
>   } // end of loadStripImageArray()
>
> The problem I'm having is with the for loop.   If I create an abject
> array of say 6 buffered images,  how do I reference the indexed image
> array to match the lines pointed to?  In other words what is the
> equivalent of strip[i] in Clojure?
>
> I'm not exactly a beginner, but this has stumped me.
>
> Bill

-- 
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: Overriding a java protected method and calling the super-class method from within

2011-01-21 Thread Stuart Sierra
Accessing protected methods is a pain. You could do it with reflection, as 
Bill said.

In gen-class, you need to add the `:exposes-methods` option to gen-class. 
This will make the protected method available as a public method, under an 
alternate name.

For example, if you're extending Java class A which has 
protectedMethod(parameter), you would have something like:

(ns com.example.subclass-of-A
  (:gen-class :extends com.example.A
 :exposes-methods {protectedMethod exposedSuperMethod}))

(defn -protectedMethod [this parameter]
  (.exposedSuperMethod this parameter))

-Stuart Sierra
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: Help with java conversion

2011-01-21 Thread Laurent PETIT
2011/1/21 WoodHacker 

> I'm converting the java code examples in Killer Game Programming in
> Java by Andrew Davison to Clojure and am having great fun doing it.
> But I've hit wall where I can't seem to get the code to work.
>
> The following code moves an animated gif strip to a java array:
>
>  public BufferedImage[] loadStripImageArray(String fnm, int number)
>  {
>if (number <= 0) {
>  System.out.println("number <= 0; returning null");
>  return null;
>}
>
>BufferedImage stripIm;
>if ((stripIm = loadImage(fnm)) == null) {
>  System.out.println("Returning null");
>  return null;
>}
>
>int imWidth = (int) stripIm.getWidth() / number;
>int height = stripIm.getHeight();
>int transparency = stripIm.getColorModel().getTransparency();
>
>BufferedImage[] strip = new BufferedImage[number];
>Graphics2D stripGC;
>
>// each BufferedImage from the strip file is stored in strip[]
>for (int i=0; i < number; i++) {
>  strip[i] =  gc.createCompatibleImage(imWidth, height,
> transparency);   <=
>
>  // create a graphics context
>  stripGC = strip[i].createGraphics();
> <=
>  // stripGC.setComposite(AlphaComposite.Src);
>
>  // copy image
>  stripGC.drawImage(stripIm,
>  0,0, imWidth,height,
>  i*imWidth,0, (i*imWidth)+imWidth,height,
>  null);
>  stripGC.dispose();
>}
>return strip;
>  } // end of loadStripImageArray()
>
> The problem I'm having is with the for loop.   If I create an abject
> array of say 6 buffered images,  how do I reference the indexed image
> array to match the lines pointed to?  In other words what is the
> equivalent of strip[i] in Clojure?
>

Hello,

As far as possible, the equivalent of a java anArray[anIndex] expression
will be of not using an index in the first place in Clojure (*).

In your example, I'm not sure there is a compelling reason for having strip
be a true java array. A clojure vector would probably be sufficient.
So AFAICT, I would write your "loop" as:

(defn with-gc* [gc f]
  (try (f gc)
(finally (when gc (.dispose gc)

(defmacro with-gc [[name gc-expr] & body] `(with-gc* ~gc-expr (fn [~name]
~@body)))

(let [get-strip (fn [offset] (let [strip (.createCompatibleImage gc imWidth
height transparency)] (with-gc [gc (.createGraphics strip )] (.drawImage
gc) strip)))]
  (into [] (map get-strip (map #(* imWidth) (range number)

Not tested, would probably place the fn on more than one line


(*): of course, for when you /really/ want to do that / can't possibly do
without that, you can , by using nth or get functions. Or by directly using
java arrays. Sometimes. When you really don't see how to avoid this.

-- 
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: Help with java conversion

2011-01-21 Thread Armando Blancas
I usually do something like this little sample. Calculations go in the
let bindings and new elements are conjoined into the vector.
(defn foo [n]
  (loop [v [] i 0]
(if (= i n)
  v
  (let [x (* i i)]
(recur (conj v x) (inc i))
user=> (foo 6)
[0 1 4 9 16 25]

On Jan 21, 8:07 am, WoodHacker  wrote:
> I'm converting the java code examples in Killer Game Programming in
> Java by Andrew Davison to Clojure and am having great fun doing it.
> But I've hit wall where I can't seem to get the code to work.
>
> The following code moves an animated gif strip to a java array:
>
>   public BufferedImage[] loadStripImageArray(String fnm, int number)
>   {
>     if (number <= 0) {
>       System.out.println("number <= 0; returning null");
>       return null;
>     }
>
>     BufferedImage stripIm;
>     if ((stripIm = loadImage(fnm)) == null) {
>       System.out.println("Returning null");
>       return null;
>     }
>
>     int imWidth = (int) stripIm.getWidth() / number;
>     int height = stripIm.getHeight();
>     int transparency = stripIm.getColorModel().getTransparency();
>
>     BufferedImage[] strip = new BufferedImage[number];
>     Graphics2D stripGC;
>
>     // each BufferedImage from the strip file is stored in strip[]
>     for (int i=0; i < number; i++) {
>       strip[i] =  gc.createCompatibleImage(imWidth, height,
> transparency);   <=
>
>       // create a graphics context
>       stripGC = strip[i].createGraphics();
> <=
>       // stripGC.setComposite(AlphaComposite.Src);
>
>       // copy image
>       stripGC.drawImage(stripIm,
>                   0,0, imWidth,height,
>                   i*imWidth,0, (i*imWidth)+imWidth,height,
>                   null);
>       stripGC.dispose();
>     }
>     return strip;
>   } // end of loadStripImageArray()
>
> The problem I'm having is with the for loop.   If I create an abject
> array of say 6 buffered images,  how do I reference the indexed image
> array to match the lines pointed to?  In other words what is the
> equivalent of strip[i] in Clojure?
>
> I'm not exactly a beginner, but this has stumped me.
>
> Bill

-- 
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: Help with java conversion

2011-01-21 Thread Laurent PETIT
2011/1/21 Armando Blancas 

> I usually do something like this little sample. Calculations go in the
> let bindings and new elements are conjoined into the vector.
> (defn foo [n]
>  (loop [v [] i 0]
>(if (= i n)
>  v
>  (let [x (* i i)]
>(recur (conj v x) (inc i))
> user=> (foo 6)
> [0 1 4 9 16 25]
>

Hello,

maybe the above example was intentionally simple, but should I suggest
capturing the is in a sequence (just using range) and then :

(defn foo [n] (into [ ] (map #(* % %) (range n))

HTH,

-- 
Laurent

-- 
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: Help with java conversion

2011-01-21 Thread Michael Gardner
On Jan 21, 2011, at 10:35 AM, Laurent PETIT wrote:

> As far as possible, the equivalent of a java anArray[anIndex] expression will 
> be of not using an index in the first place in Clojure (*).

Expanding on Laurent's answer: to transform one list into another, use 'map'. 
In this case you can think of transforming the integers [0 .. n), expressed in 
Clojure as (range n), into a list of image strips.

-- 
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: Help with java conversion

2011-01-21 Thread Laurent PETIT
2011/1/21 Michael Gardner 

> On Jan 21, 2011, at 10:35 AM, Laurent PETIT wrote:
>
> > As far as possible, the equivalent of a java anArray[anIndex] expression
> will be of not using an index in the first place in Clojure (*).
>
> Expanding on Laurent's answer: to transform one list into another, use
> 'map'. In this case you can think of transforming the integers [0 .. n),
> expressed in Clojure as (range n), into a list of image strips.
>

(inc)


>
> --
> 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 no def- ?

2011-01-21 Thread Ken Wesson
On Fri, Jan 21, 2011 at 9:49 AM, Aaron Bedra  wrote:
> On 01/21/2011 09:22 AM, Laurent PETIT wrote:
>
> 2011/1/21 Aaron Bedra 
>>
>> On 01/21/2011 12:51 AM, Ken Wesson wrote:
>>>
>>> On Fri, Jan 21, 2011 at 12:40 AM, Alex Baranosky
>>>   wrote:

 I've wanted to have private defs.  For defn, I just us defn-.  But there
 is
 no def-

 So I just use:

 (defmacro def- [name&  decls]
     (list* `def (with-meta name (assoc (meta name) :private true))
 decls))
>>>
>>> There's a (defvar- ...) in clojure.contrib. Why there's nothing like
>>> this in core remains a mystery to me. :)
>>>
>> You can also do
>>
>> (def foo {:private true} (...))
>
> With def, it will be :
> user=> (def ^{:private true} foo "ba")
> #'user/foo
> user=> (meta (var foo))
> {:ns #, :name foo, :file "NO_SOURCE_PATH", :line 4, :private
> true}
> user=>
>
> It's with defn that you can use a map at a certain position:
> user=> (defn foo {:private true} [] "bar")
> #'user/foo
> user=> (meta (var foo))
> {:ns #, :name foo, :file "NO_SOURCE_PATH", :line 7,
> :arglists ([]), :private true}
> user=>
>
>
> Doh!  I'm full of typos this week.  Do a combination of what I meant and
> what Laurent said and you'll be in a good place.

It's still odd that defvar- isn't in core. Explicit metadata is clunky
compared to defn-.

Another thing -- defn docstrings. It would be nice if:

1. You could optionally put a docstring after the value of a normal
   def -- (def foo 17 "seventeen").

2. A function body that starts with a string and isn't only the
   string is treated as if the string were a docstring, so

   (defn foo
 "Docstring -- works currently"
 ([x y]
   (+ (* x x) y)))

   and the imo tidier with only one arity

   (defn foo [x y]
 "Docstring -- works currently"
 (+ (* x x) y))

   would be equivalent.

   (defn foo
 "Docstring -- works currently"
 [x y]
 (+ (* x x) y))

   should probably also work (it currently doesn't -- the function
   works but the docstring disappears). Obviously

   (defn foo []
 "Hello, World")

   would still have to return "Hello, World", hence the requirement
   that the defn body discards the string rather than returns it.

3. Multiple string literals in a row could be used.

   (defn foo
 "Docstring line 1
  Docstring line 2
  Lines 3 and on ..."
 ([x y]
   (+ (* x x) y)))

   has lines 2 and up begin with a lot of spaces. If these are
   kept, it will print as

   Docstring line 1
 Docstring line 2
 Lines 3 and on ...

   If they are not, then a docstring containing


  ...
  Example usage:

  (mymacro foo [a 17 b 32]
(dosync ...
  (blah blah ...)))

   is going to lose the indents in the example lines. And of course

   (defn foo
 "Docstring line 1
   Docstring line 2
   Lines 3 and on ..."
 [x y]
 (+ (* x x) y))

   is butt-ugly.

   (defn foo
 "Docstring line 1"
 "Docstring line 2"
 "Lines 3 and on ..."
 [x y]
 (+ (* x x) y))

   if it worked would avoid that trilemma; docstring lines would
   start with only the leading spaces actually intended to be
   included in the text, so the entire tool chain could preserve
   them and get the desired results.

   If combined with 2 above, this would mean that any function whose
   s-expression was (defn symbol vector string-literal string-literal
   ... string-literal at-least-one-more-thing) would have the chain
   of string literals treated as docstring lines, with the
   at-least-one-more-thing as the body. Anything from the first
   non-string-literal on would be the body; if there was nothing in
   there but string literals after the argvec the final string
   literal would be taken as the return value rather than a doc line.

   Note: the docstring literals under this proposal will tend to not
   contain newlines. (interpose \newline string-literal-seq) should
   therefore be used to combine the literals into the docstring.

3 can be applied to the hypothetical def docstrings as well:

(def name val line1 line2 ... linen)

would be equivalent to

(def ^{:doc (interpose \newline [line1 line2 ... linen]) name val)

Another thing that could be useful: if the entire s-expression (def
...) or (defn ...) has metadata, merge it in, and if there's more than
one
meta, merge rather than replace. This would allow a style that didn't
cause longer documentation strings to separate the function name from
the body, and where indent is less of an issue:

^{:doc "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed
do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut
enim ad minim veniam, quis nostrud exercitation ullamco laboris
nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in
reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
culpa qui officia deserunt mollit anim id est laborum."}

^{:private t

Java/Clojure Developer at Wall Street Bank

2011-01-21 Thread Michael

I'm looking for a java/clojure developer for my small team at a Wall
Street bank.  If interested, please reply to sender and we can discuss
details.

Thanks,

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: Why no def- ?

2011-01-21 Thread Sean Corfield
On Fri, Jan 21, 2011 at 12:38 PM, Ken Wesson  wrote:
> Another thing that could be useful: if the entire s-expression (def
> ...) or (defn ...) has metadata, merge it in, and if there's more than
> one
> meta, merge rather than replace.

That's how Clojure 1.3 already works...

user=> (defn ^:private ^:stuff ^{:doc "Docs"} ^{:more "info"} foo [] "")
#'user/foo
user=> (meta #'user/foo)
{:arglists ([]), :ns #, :name foo, :private true,
:more "info", :doc "Docs", :stuff true, :line 7, :file
"NO_SOURCE_PATH"}
-- 
Sean A Corfield -- (904) 302-SEAN
Railo Technologies, Inc. -- http://getrailo.com/
An Architect's View -- http://corfield.org/

"If you're not annoying somebody, you're not really alive."
-- Margaret Atwood

-- 
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: Euler 14

2011-01-21 Thread Aaron Cohen
On Thu, Jan 20, 2011 at 11:57 PM, Mark Engelberg
 wrote:
> On Thu, Jan 20, 2011 at 6:51 AM, Andreas Liljeqvist  wrote:
>> I am sorry, I can't seem to reproduce the behavior at the moment :(
>> Mark, please tell me that I am not delusional...
>
> I definitely exhausted the heap running your program.  I was using
> Clojure 1.1, Java 1.6.0_21 with -server -Xmx1600M flags, running via
> Clojure Box on Windows XP.
>

"Fine grained locals clearing" was added in clojure 1.2, so it's
likely that your entire (range 1 100) list has to be processed
before it can be GC'ed.

max-key uses destructuring, which was one of the culprits for
unexpectedly holding onto the head of lists before locals clearing was
added.

See http://groups.google.com/group/clojure/msg/9b4e268b85c20cd6

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


Implementing destructuring without setting your hair on fire

2011-01-21 Thread Daniel Werner
Hi everyone,

let's play a round of golf. I am currently implementing associative
destructuring for ClojureJS while trying not to peek into clojure.core
too often -- which wouldn't make things much easier since the
'destructure fn is a huge beast.

After a few tries I've come up with the following algorithm to
transform :keys syntax into normal destructuring syntax, but am still
appalled by its complexity:

(let [vmap {'y :y, 'z :z :keys ['a 'b]}]
  (->> vmap
((juxt :keys :strs :syms))
(apply concat)
(mapcat #(vector % (keyword %)))
(apply hash-map)))

==> {a :a, b :b}

There has got to be a prettier/more efficient way! I'm open to
suggestions :-)

Thanks,

Daniel

-- 
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: Euler 14

2011-01-21 Thread Aaron Cohen
> max-key uses destructuring, which was one of the culprits for
> unexpectedly holding onto the head of lists before locals clearing was
> added.

This part of what I said is garbage, I'm sorry. I looked at max-key
too quickly, but there isn't any destructuring there.

That doesn't change that I think that it was probably the addition of
locals clearing in 1.2 that is causing what you guys are seeing (or
not seeing).

-- 
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: Implementing destructuring without setting your hair on fire

2011-01-21 Thread Mark Triggs
Daniel Werner  writes:

> After a few tries I've come up with the following algorithm to
> transform :keys syntax into normal destructuring syntax, but am still
> appalled by its complexity:
>
> (let [vmap {'y :y, 'z :z :keys ['a 'b]}]
>   (->> vmap
> ((juxt :keys :strs :syms))
> (apply concat)
> (mapcat #(vector % (keyword %)))
> (apply hash-map)))
>
> ==> {a :a, b :b}


Pretty's such a subjective thing, but my first impulse was:

  (let [vmap {'y :y, 'z :z :keys ['a 'b]}]
(reduce #(assoc %1 %2 (keyword %2))
{}
(mapcat vmap [:keys :strs :syms])))


-- 
Mark Triggs


-- 
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: Euler 14

2011-01-21 Thread Ken Wesson
On Fri, Jan 21, 2011 at 6:43 PM, Aaron Cohen  wrote:
>> max-key uses destructuring, which was one of the culprits for
>> unexpectedly holding onto the head of lists before locals clearing was
>> added.
>
> This part of what I said is garbage, I'm sorry. I looked at max-key
> too quickly, but there isn't any destructuring there.

I wrote my own implementation of max-key that doesn't use
destructuring before reading the later part of this thread. :)

It is here:

(defn my-max-key [f & xs]
  (reduce
(fn [v1 v2]
  (let [k1 (f v1)
k2 (f v2)]
   (if (> k1 k2)
 v1
 v2)))
xs))

If the original code had blown the heap on my machine I was going to
substitute my-max-key and see if it still did, and if it did tweak
my-max-key to see if there was a way to make the problem go away.

This my-max-key is less than perfectly efficient in that it keeps
recomputing f of the current max-key; it's a no-frills version
intended to be sure not to hold onto the head of anything, and to be
simple and easy to tweak and debug. I'd have added saving f of current
max-key later and seen if it still avoided blowing the heap on the
original problem, and, if so, submitted it as a possible replacement
for the core implementation of max-key. But that's moot now. It looks
like one of the improvements to locals clearing in 1.2 fixed the OP's
problem.

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


Unification

2011-01-21 Thread Alex Baranosky
Hi,

I've read a bit about clojure.core.unify (
http://blog.fogus.me/2010/12/14/unification-versus-pattern-matching-to-the-death/
)

I haven't gotten through PAIP yet, but I gather unification libraries enable
logic programming?  Is it true that unification is a superset of pattern
matching?

Mostly, I'd like to hear more about what something like clojure.core.unify
is good for.

Best,
Alex

-- 
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 private helper functions in a macro.

2011-01-21 Thread Ken Wesson
It *is* possible, and not just if the private helper function is run
at macroexpansion time to massage forms for the macro. The macro *can*
output code that invokes the helper at runtime, but it's awkward:

user=> (ns foo)
nil
foo=> (defn- priv [x] (+ 2 x))
#'foo/priv
foo=> (defmacro call-priv-1 [x] `(priv ~x))
#'foo/call-priv-1
foo=> (defmacro call-priv-2 [x] `((deref (var priv)) ~x))
#'foo/call-priv-2
foo=> (macroexpand-1 '(call-priv-1 3))
(foo/priv 3)
foo=> (call-priv-1 3)
5
foo=> (macroexpand-1 '(call-priv-2 3))
(@#'foo/priv 3)
foo=> (call-priv-2 3)
5
foo=> (in-ns 'user)
#
user=> (foo/call-priv-1 3)
#
user=> (foo/call-priv-2 3)
5
user=>

As expected, foo/call-priv-1 doesn't work from outside foo. But
foo/call-priv-2 does.

Obviously this can be made a bit nicer with another couple of macros:

(defmacro pvar [sym]
  `(deref (var ~sym)))

(defmacro pfn [sym & args]
  `((pvar ~sym) ~@args))

Then call-priv-2 becomes

(defmacro call-priv-2 [x]
  `(pfn priv ~x))

which is *almost* as nice as a plain old

`(priv ~x)

like in call-priv-1.

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