Re: Datatypes and Protocols update

2010-04-27 Thread Mark Engelberg
Watching Stuart's tutorial, it looks like the automatic factory
functions for deftypes have gone away (I'm still working with Clojure
1.1, so haven't had a chance to try the latest changes for myself).
I'm going to miss that feature, especially for defrecord, which is now
the "common case" construct.

I understand that you can always do "Foo." to construct a Foo record,
but these constructors don't act as full-fledged functions, right?

Honestly, for me the main issue is just that subjectively, it is less
satisfying to create a Clojure data structure and end up with
something that you construct with Java interop syntax.  I'd like
Clojure data structures to look and feel "Clojurish" not "Javaish"
(yes, I know that Clojure is built with interop in mind, so
technically, anything Javaish is also Clojurish, but I still feel a
difference).

-- 
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: tutorial on clojure protocols 1.2

2010-04-27 Thread Konrad Hinsen

On 26 Apr 2010, at 18:20, Stuart Halloway wrote:

I have created a short (30 min) tutorial on clojure protocols at http://vimeo.com/11236603 
. Hope some of you will find it useful.


Nice presentation!

I just noticed one inaccuracy: in the comparison between multimethods  
and protocol functions, you didn't mention that multimethods support  
hierarchies, while protocols don't.


Konrad.

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


Re: Datatypes and Protocols update

2010-04-27 Thread Konrad Hinsen

On 27 Apr 2010, at 09:20, Mark Engelberg wrote:


I understand that you can always do "Foo." to construct a Foo record,
but these constructors don't act as full-fledged functions, right?


No. They are not first-class objects (in fact, not objects at all in  
the JVM sense), so you can't pass them around.



Honestly, for me the main issue is just that subjectively, it is less
satisfying to create a Clojure data structure and end up with
something that you construct with Java interop syntax.  I'd like
Clojure data structures to look and feel "Clojurish" not "Javaish"


That was my initial reaction as well. However, I just write my own  
factory functions now, and this gives me the opportunity to add  
argument validation in any way I like.


BTW, another change is that the defined type is a Java class, whereas  
before it was a var pointing to the factory function. Not being a var  
means that the type doesn't really reside in the namespace. In  
particular, a :use of the namespace doesn't get you the type, you have  
to :import it.


Taken together, these changes make deftype and defrecord low-level  
features for defining data types that are best exposed to the outside  
world via a functional API. Your clients don't need to know that  
there's a Java type with a constructor hidden somewhere.


Konrad.

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


Re: ClassCastException using let

2010-04-27 Thread James Reeves
Could you provide the stack trace of the exception?

I don't see any obvious problems, but the error may be caused by data
you pass into the function. Alternatively perhaps the error is not
caused inside let, but whatever consumes the results.

Maybe also providing a sample of the raw data would be useful.

Also, just as a point of style, function names and keywords in Clojure
are usually all lowercase, separated by dashes. e.g.
diff-common-orders, :receiving-order-id.

- James

On 27 April 2010 05:14, borgees  wrote:
> I'm a clojure newbie, trying to automate a script at work and running
> into an issue.
>
> Given a function
>
> (defn diffCommonOrders
>  "For each order, checks the two execs maps
>  for the entries and reports the differences"
>  [orders1 orders2 execs1 execs2]
>  (for [order (vals orders1) :when (not-empty (get execs1
> (order :receiving_order_id )))]
>     (let [order2 (orders2 (order :cl_ord_id))
>           recOrdId1 (get order :receiving_order_id)
>           recOrdId2 (get order2 :receiving_order_id)]
>      (difference (set (get execs1 recFirmOrdId1)) (set (get execs2
> recFirmOrdId2))
>
> with orders1 and orders2 defined as a map of String to Map with keys
> {:receiving_order_id :cl_ord_id }
> and execs1 and execs2 defined as maps of String to Vector, when I try
> to execute this function in the REPL, I get:
>
> java.lang.ClassCastException: clojure.lang.PersistentHashSet cannot be
> cast to java.util.Map$Entry
>
> I believe the error is occurring on the last line.  Why is it
> expecting a Map Entry?  Why can I not return whatever I want from the
> let?
>
> --
> 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: Any window user familiar with clojure-contrib development ?

2010-04-27 Thread Chris Perkins
On Apr 26, 9:10 pm, gary ng  wrote:
> Hi,
>
> I am wondering if there is any windows user who is familiar with
> clojure-contrib project.
>
> The reason I asked is that I encountered quite some issues when trying to
> build my own copy of clojure-contrib(on windows). That includes:
>
> 1. I cannot clonehttp://github.com/richhickey/clojure-contrib.gitas it
> would die in the middle for some item(i.e. certain git hash object) not
> found. I have to use git:// instead
> 2. Even after successful cloning, certain files seems to be suffering from
> the CRLF/LF conversion issue, I have to turn off git's CRLF auto conversion
> or else the newly cloned tree would already been showing changes(while the
> diff shows lines deleted then added with no noticeable difference, which I
> traced to the CRLF).
> 3. Then while building clojure-contrib, certain tests failed which based on
> the test log is again about the odd case of file system naming("file:/foo <>
> 'file://C:/foo") and CRLF <> LF
>

I have the same problems, but I work around them.  Using the git:
protocol works, setting "git config core.autocrlf false" followed by
"git checkout -- ." seems to get around the line-endings problem, and
using "mvn -Dmaven.test.skip ..." gets around the failing tests.

One of us (meaning Windows users) really should step up and start
contributing patches for those tests...   um, anyone? anyone? I guess
I should get around to talking to my boss about signing that CA -
should only take a few months to get past the lawyers, I hope :)  Oh,
the joys of working for a big company.

- Chris Perkins

-- 
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: Datatypes and Protocols update

2010-04-27 Thread Rich Hickey


On Apr 27, 2010, at 3:20 AM, Mark Engelberg wrote:


Watching Stuart's tutorial, it looks like the automatic factory
functions for deftypes have gone away (I'm still working with Clojure
1.1, so haven't had a chance to try the latest changes for myself).
I'm going to miss that feature, especially for defrecord, which is now
the "common case" construct.

I understand that you can always do "Foo." to construct a Foo record,
but these constructors don't act as full-fledged functions, right?

Honestly, for me the main issue is just that subjectively, it is less
satisfying to create a Clojure data structure and end up with
something that you construct with Java interop syntax.  I'd like
Clojure data structures to look and feel "Clojurish" not "Javaish"
(yes, I know that Clojure is built with interop in mind, so
technically, anything Javaish is also Clojurish, but I still feel a
difference).




I agree. I asked for suggestions for the factory fn in #clojure, but  
got some pushback against introducing things in the namespace. I'm  
still open to suggestions, here are the issues:


The generated type name is now imported, so at the very least the  
factory fn can't have the same name as the class. Alternatives are  
create-Foo etc.


Some have asked for parameterized factories:

(record Foo ...) or (record ::Foo ...)

These cannot be made as fast as direct factories. Also, they may be  
used for key/value initialization:


(record ::Foo :field1 v1 :field2 v2 ...)

As soon as people want bodies for the factories, in order to do  
argument transformation/validation/defaulting, a generated factory is  
in the way.


We left it at: If you really want a factory you can always write one,  
let's see if people do.


Rich

--
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: tutorial on clojure protocols 1.2

2010-04-27 Thread Heinz N. Gies
On Apr 26, 2010, at 18:20 , Stuart Halloway wrote:

> I have created a short (30 min) tutorial on clojure protocols at 
> http://vimeo.com/11236603. Hope some of you will find it useful.

Stuart,
this was a awsome tutorial, I've never really gasped what protocols are and do 
from the talks on #clojure but now, 30 minutes later I think I got an idea! 
Great job keep it up mate!


Best regards,
Heinz

PS: when will a tutorial about cells come? I didn't got them either yet :P


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


Serializable Collections ?

2010-04-27 Thread Jules
Guys,

I've searched the group and cannot find an answer to this one...

Why are many of the more common collection types in Clojure not
Serializable :

Clojure 1.1.0-new-SNAPSHOT
user=> (def #^java.io.ObjectOutput os (java.io.ObjectOutputStream.
(java.io.FileOutputStream. "/dev/null")))
#'user/os
user=> (.writeObject os [1 2 3 4])
java.io.NotSerializableException: clojure.lang.PersistentVector$Node
(NO_SOURCE_FILE:0)
user=> (.writeObject os (list 1 2 3 4))
java.io.NotSerializableException: clojure.lang.PersistentList
(NO_SOURCE_FILE:0)
user=> (.writeObject os #{1 2 3 4})
java.io.NotSerializableException: clojure.lang.PersistentHashMap
$BitmapIndexedNode (NO_SOURCE_FILE:0)
user=> (.writeObject os {:1 2 :3 4})
nil
user=>

I am writing a distributed Java/Clojure app and am constantly bitten
by this. I am always having to copy collections into ArrayLists before
sending them, from Java code, over the wire...

ArrayLists, however do not implement Comparable and I now need a
Comparable AND Serializable collection. :-(

Is there a wishlist for Clojure 1.2 ? :-)

Is there a good reason why making '(), [], {} and #{} Serializable
should not be on it ?

thanks


Jules

P.S.

I'm using 1.1.0-new-SNAPSHOT - is there another version that
implements this, hence the lack of noise on the list ?

-- 
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: ClojureCLR multiple .dll files

2010-04-27 Thread rodgertq
I haven't actually tried this with ClojureCLR produced .dlls but you
could look at ILMerge as a post build step, see:

http://research.microsoft.com/en-us/people/mbarnett/ilmerge.aspx



On Apr 25, 12:10 pm, lucidquiet  wrote:
> Hello,
>
> I'm using ClojureCLR, at the moment I've created a couple of files
> that each have a (ns .. :gen-class .. ) in them.  I then compiled both
> clj and the result is a [name].clj.dll for each file.  The question I
> have: is there a way to create just a single dll as a result of
> compilation.  I'm aware that in the Java version of Clojure each
> function produces a single .class file, and in the end all of
> the .class files can be packaged as part of a .jar file.  In .NET
> there really isn't such a thing as a .class file (as far as I know).
>
> I have attempted to merge them using the ILMerge program found at
> MSDN.  It appears that __Init__, __InternalDynamicExpressionInit, and
> __REPL__ would wind up being duplicate types.  My guess is that
> individually it makes sense for these classes to exist per dll, but in
> a single dll perhaps only 1 of these type definition(s) should be
> generated.  I have used the /allowDup flag to turn off errors, but I
> am not certain of the consequences, and perhaps the better solution is
> to create a common type only once when outputting these dlls based on
> parameters to compile, or a *flag* of some kind.
>
> Any thoughts or suggestions welcome.
>
> Thanks,
> L-
>
> --
> 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 
> athttp://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: Datatypes and Protocols update

2010-04-27 Thread Konrad Hinsen
On 27.04.2010, at 14:45, Rich Hickey wrote:

> I agree. I asked for suggestions for the factory fn in #clojure, but got some 
> pushback against introducing things in the namespace. I'm still open to 
> suggestions, here are the issues:
> 
> The generated type name is now imported, so at the very least the factory fn 
> can't have the same name as the class. Alternatives are create-Foo etc.

How about providing the name of a factory function via an option, with no 
factory as the default? Something like

(defrecord Foo
  [bar baz]
  :factory create-Foo)

This would avoid the creation of vars that are not explicitly named in the 
code, and thus bad surprises.

Konrad.

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


Re: Serializable Collections ?

2010-04-27 Thread Brian Hurt
On Tue, Apr 27, 2010 at 9:10 AM, Jules  wrote:

> Guys,
>
> I've searched the group and cannot find an answer to this one...
>
> Why are many of the more common collection types in Clojure not
> Serializable :
>

I have an assemblia ticket open on this issue:
https://www.assembla.com/spaces/clojure/tickets/281-make-more-datastructures-serializable

I need to finish a new version of the patch that more carefully controls
what is serialized.

Brian

-- 
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: tutorial on clojure protocols 1.2

2010-04-27 Thread Christian Guimaraes
Stuart,

Great. Congratulations.

And I was thinking about one Clojure course using this format. Stay here the
sugestion ;-)

-- Christian.

On Tue, Apr 27, 2010 at 2:07 PM, Heinz N. Gies  wrote:

> On Apr 26, 2010, at 18:20 , Stuart Halloway wrote:
>
> > I have created a short (30 min) tutorial on clojure protocols at
> http://vimeo.com/11236603. Hope some of you will find it useful.
>
> Stuart,
> this was a awsome tutorial, I've never really gasped what protocols are and
> do from the talks on #clojure but now, 30 minutes later I think I got an
> idea! Great job keep it up mate!
>
>
> Best regards,
> Heinz
>
> PS: when will a tutorial about cells come? I didn't got them either yet :P
>
>
> --
> 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

Defining a namespace inside a let

2010-04-27 Thread David McNeil
I am experimenting with clojure.test and I encountered the following
situation which I cannot explain.

This code:

(println (do
   (ns ns01
 (:use clojure.test))
   (deftest test1 nil)
   (run-tests)))

Produces the expected result (note: it runs one test):

Testing ns01

Ran 1 tests containing 0 assertions.
0 failures, 0 errors.
{:type :summary, :test 1, :pass 0, :fail 0, :error 0}

However, if I do the exact same thing inside of a let:

(println (let []
   (do
 (ns ns02
   (:use clojure.test))
 (deftest test1 nil)
 (run-tests

Then I get the unexpected result that no tests are executed:

Testing ns02

Ran 0 tests containing 0 assertions.
0 failures, 0 errors.
{:type :summary, :test 0, :pass 0, :fail 0, :error 0}

Seems there is something going on with namespaces that I do not
understand and I hope that somewhere here can explain it.

Thank you.
-David McNeil

-- 
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: Conflicting definition for IPersistentVector

2010-04-27 Thread Dan
On Apr 26, 4:54 pm, Rich Hickey  wrote:
> On Apr 13, 2010, at 9:37 AM, Dan wrote:
>
> > I'm a Java developer; I work on a project that has adopted Clojure's
> > data structures as a convenient implementation of immutable sets,
> > maps, etc.  In attempting to add type parameters to the Clojure
> > interfaces, I noticed a conflict in the definition of
> > IPersistentVector.  I assume the problem exists when programming in
> > Clojure, too.
>
> You should note that by doing so you are superimposing homogeneity on  
> collections that are otherwise heterogeneous.

No, it does not limit expressibility at all to formally state the
constraints on input/output types that are otherwise informal.  You
can always use "Object" as the type argument to all these types to
express heterogeneity.

The conflict I'm describing is the same whether the constraints on
interface implementations are formally or informally stated.  The type
parameters simply help to clarify the intended contract by expressing
it formally.

> > Put another way, the problem is that if I have anAssociativewhich I
> > know maps integers to strings, and I invoke seq(), I don't know what
> > kind of values I'm going to get.
>
> Objects
...
> One way to look at the Seqable in high-level interfaces is just as  
> specifying a sequence of whatever the value type is for the ultimate  
> concrete implementation. Java supports return type covariance, so you  
> could put a more specific signature on seq for IPersistentMap.
>
> In any case,Associative shouldn't constrain the seq or value type of  
> the collection. All it specifies is that, given keys you can get items.

So, to parameterize Associative in a way that properly expresses the
contract you're describing, it looks like this:

public interface Associative  extends IPersistentCollection
{
  Associative assoc(K key, V val);
  ...
}

There's no connection (stated formally or informally) between the Seq
element type T and the key/value types K and V.  That's certainly
doable; it just strikes me as unnecessarily confusing.  The way I'd
design it is to remove the "extends IPersistentCollection" clause
entirely.

Anyway, thanks for helping to clarify the intended meaning.

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


finding sequential items from a sequence

2010-04-27 Thread Ralph
Crossposted to stackoverflow.

In a Clojure program, I have a sequence of numbers:

(2 3 4 6 8 1)

I want to find the longest sub-sequence where the items are
sequential:

(2 3 4)

I am assuming that it will involve (take-while ...) or (reduce ...).

Any ideas?

-- 
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: Questions about program design

2010-04-27 Thread Miki
Hello Andrew,

> So, for people like me, can any of you suggest some transcribed
> lectures, screencasts, books, blog posts, or other errata that discuss
> the difference between program design in a language like Clojure and
> an OO language like Ruby or Java?
Two books that helped me are 
http://mitpress.mit.edu/sicp/full-text/book/book.html
and http://www.paulgraham.com/onlisptext.html

HTH
--
Miki

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


Insert into an indexed seq

2010-04-27 Thread Sean Devlin
Is there a built in to insert a value into an "indexed" seq?

For example:

user=> (insert [:a :b :c :d] 2 :q)
(:a :b :q :c :d)

Not sure if I'm missing something simple...
Sean

-- 
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: Insert into an indexed seq

2010-04-27 Thread Sean Devlin
Also, why does this work:
user=> (assoc [:a :b :c :d] 2 :q)
[:a :b :q :d]

And this doesn't:
user=> (dissoc [:a :b :c :d] 2)
#

Annoying.


On Apr 27, 1:24 pm, Sean Devlin  wrote:
> Is there a built in to insert a value into an "indexed" seq?
>
> For example:
>
> user=> (insert [:a :b :c :d] 2 :q)
> (:a :b :q :c :d)
>
> Not sure if I'm missing something simple...
> Sean
>
> --
> 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 
> athttp://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: Insert into an indexed seq

2010-04-27 Thread Chouser
On Tue, Apr 27, 2010 at 1:31 PM, Sean Devlin  wrote:
> Is there a built in to insert a value into an "indexed" seq?
>
> For example:
>
> user=> (insert [:a :b :c :d] 2 :q)
> (:a :b :q :c :d)
>
> Not sure if I'm missing something simple...

That's a vector, which cannot efficiently splice internally, so
it's not supported directly.  However, you can build a new vector
with your value included:

  (apply conj [:a :b] :q [:c :d])
  ;=> [:a :b :q :c :d]

> Also, why does this work:
> user=> (assoc [:a :b :c :d] 2 :q)
> [:a :b :q :d]
>
> And this doesn't:
> user=> (dissoc [:a :b :c :d] 2)
> # clojure.lang.PersistentVector cannot be cast to
> clojure.lang.IPersistentMap (NO_SOURCE_FILE:286)>
>
> Annoying.

Again, vectors cannot efficiently insert or remove items except
from the right-hand end, though as you note items can replaced
internally.

  (let [v [:a :b :c :d]]
(into (subvec v 0 2) (subvec v 3)))
  ;=> [:a :b :d]

There are immutable collections that support both numeric-indexed
lookups and internal splicing, they're just not currently included
with Clojure.  See for example finger trees:


http://functionaljava.googlecode.com/svn/artifacts/2.21/javadoc/fj/data/Seq.html

--Chouser
http://joyofclojure.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: Insert into an indexed seq

2010-04-27 Thread Sean Devlin
You're right, inserting into a vector is fundamentally slow.
Inserting into a list (must traverse elements) or String (Char Array)
isn't any better.  I get why Clojure doesn't include certain
operations on certain data structures (e.g. assoc on a list), because
it's the wrong tool for the job. However, there are still problems
that require me to use an expensive operation.

Maybe I'm too focused on my current project, and wrong about how much
a typical person would use insert.  Still, its absence seems like an
oversight.

Sean

On Apr 27, 2:05 pm, Chouser  wrote:
> On Tue, Apr 27, 2010 at 1:31 PM, Sean Devlin  wrote:
> > Is there a built in to insert a value into an "indexed" seq?
>
> > For example:
>
> > user=> (insert [:a :b :c :d] 2 :q)
> > (:a :b :q :c :d)
>
> > Not sure if I'm missing something simple...
>
> That's a vector, which cannot efficiently splice internally, so
> it's not supported directly.  However, you can build a new vector
> with your value included:
>
>   (apply conj [:a :b] :q [:c :d])
>   ;=> [:a :b :q :c :d]
>
> > Also, why does this work:
> > user=> (assoc [:a :b :c :d] 2 :q)
> > [:a :b :q :d]
>
> > And this doesn't:
> > user=> (dissoc [:a :b :c :d] 2)
> > # > clojure.lang.PersistentVector cannot be cast to
> > clojure.lang.IPersistentMap (NO_SOURCE_FILE:286)>
>
> > Annoying.
>
> Again, vectors cannot efficiently insert or remove items except
> from the right-hand end, though as you note items can replaced
> internally.
>
>   (let [v [:a :b :c :d]]
>     (into (subvec v 0 2) (subvec v 3)))
>   ;=> [:a :b :d]
>
> There are immutable collections that support both numeric-indexed
> lookups and internal splicing, they're just not currently included
> with Clojure.  See for example finger trees:
>
>        
> http://functionaljava.googlecode.com/svn/artifacts/2.21/javadoc/fj/da...
>
> --Chouserhttp://joyofclojure.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 
> athttp://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


Example JAX-WS server code using annotation support

2010-04-27 Thread Richard Newman

I just spent a little time figuring this out, so I figured I'd share.

  

This example shows how to hang annotations onto Clojure code to  
replicate the equivalent Java web service code produced by NetBeans.  
It includes exported parameter names, types, and operationNames.


The correct classes seem to be generated, though I confess I haven't  
tried loading this into a container yet.


--
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: finding sequential items from a sequence

2010-04-27 Thread CuppoJava
Here's an attempt, but it's not my best work. =)

(def temp [2 3 4 6 8 1])

(let [l (filter identity
  (for [[a b c] (map vector temp (rest temp))]
(if (= (- b a) 1)
  a
  nil)))]
  (concat l [(+ (last l) 1)]))

  -Patrick

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


Re: finding sequential items from a sequence

2010-04-27 Thread Ralph
I must clarify my question: I need the longest INITIAL sequence where
the items are sequential.

One solution offered by "cgrand" at stackoverflow is

(defn longest-initial-sequence [[x :as s]]
  (take-while identity (map #(#{%1} %2) s (iterate inc x

On Apr 27, 2:59 pm, CuppoJava  wrote:
> Here's an attempt, but it's not my best work. =)
>
> (def temp [2 3 4 6 8 1])
>
> (let [l (filter identity
>           (for [[a b c] (map vector temp (rest temp))]
>             (if (= (- b a) 1)
>               a
>               nil)))]
>   (concat l [(+ (last l) 1)]))
>
>   -Patrick
>
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clojure@googlegroups.com
> Note that posts from new members are moderated - please be patient with your 
> first post.
> To unsubscribe from this group, send email to
> clojure+unsubscr...@googlegroups.com
> For more options, visit this group 
> athttp://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: Insert into an indexed seq

2010-04-27 Thread Mark J. Reed
I'm a bit surprised that it's not there already, at least in
clojure.contrib, but it's not hard to write, at least for vectors:

(defn insert [vec pos item]
(apply merge (subvec vec 0 pos) item (subvec vec pos)))


On Tue, Apr 27, 2010 at 2:45 PM, Sean Devlin wrote:

> You're right, inserting into a vector is fundamentally slow.
> Inserting into a list (must traverse elements) or String (Char Array)
> isn't any better.  I get why Clojure doesn't include certain
> operations on certain data structures (e.g. assoc on a list), because
> it's the wrong tool for the job. However, there are still problems
> that require me to use an expensive operation.
>
> Maybe I'm too focused on my current project, and wrong about how much
> a typical person would use insert.  Still, its absence seems like an
> oversight.
>
> Sean
>
> On Apr 27, 2:05 pm, Chouser  wrote:
> > On Tue, Apr 27, 2010 at 1:31 PM, Sean Devlin 
> wrote:
> > > Is there a built in to insert a value into an "indexed" seq?
> >
> > > For example:
> >
> > > user=> (insert [:a :b :c :d] 2 :q)
> > > (:a :b :q :c :d)
> >
> > > Not sure if I'm missing something simple...
> >
> > That's a vector, which cannot efficiently splice internally, so
> > it's not supported directly.  However, you can build a new vector
> > with your value included:
> >
> >   (apply conj [:a :b] :q [:c :d])
> >   ;=> [:a :b :q :c :d]
> >
> > > Also, why does this work:
> > > user=> (assoc [:a :b :c :d] 2 :q)
> > > [:a :b :q :d]
> >
> > > And this doesn't:
> > > user=> (dissoc [:a :b :c :d] 2)
> > > # > > clojure.lang.PersistentVector cannot be cast to
> > > clojure.lang.IPersistentMap (NO_SOURCE_FILE:286)>
> >
> > > Annoying.
> >
> > Again, vectors cannot efficiently insert or remove items except
> > from the right-hand end, though as you note items can replaced
> > internally.
> >
> >   (let [v [:a :b :c :d]]
> > (into (subvec v 0 2) (subvec v 3)))
> >   ;=> [:a :b :d]
> >
> > There are immutable collections that support both numeric-indexed
> > lookups and internal splicing, they're just not currently included
> > with Clojure.  See for example finger trees:
> >
> >
> http://functionaljava.googlecode.com/svn/artifacts/2.21/javadoc/fj/da...
> >
> > --Chouserhttp://joyofclojure.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 athttp://
> 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
>



-- 
Mark J. Reed 

-- 
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: Insert into an indexed seq

2010-04-27 Thread Mark J. Reed
On Tue, Apr 27, 2010 at 3:41 PM, Mark J. Reed  wrote:

> I'm a bit surprised that it's not there already, at least in
> clojure.contrib, but it's not hard to write, at least for vectors:
>
> (defn insert [vec pos item]
> (apply merge (subvec vec 0 pos) item (subvec vec pos)))
>

Er, that should be conj, not merge.  Though, perhaps surprisingly, it works
as written.

-- 
Mark J. Reed 

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

try-let

2010-04-27 Thread ataggart
Inspired from an earlier discussion, I've written a macro that allows
let-style binding values to be try'd, and still available to catch/
finally clauses.  E.g.:

(try-let [from (API/open from-addr) to (API/open to-addr)]
  (do-stuff from to)
  (finally
(if to (doto to .flush .close))
(if from (.close from

(Setting aside the fact that the above example would probably be
better implemented using with-open)

Source:  http://gist.github.com/377278

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


deftype and final classes

2010-04-27 Thread Richard Newman

Further to IRC conversations:

I'm attempting to generate a JAX-WS service using Clojure. The main  
stumbling block was annotations; that's been removed, so I gave it a  
shot using deftype.


My first strike works code-wise, so I sent it to the list earlier today.

When it comes to actually integrating the output into the Java world,  
though, things get hairy. JAX-WS has a bunch of demands on classes in  
order for its tools (such as wsgen) to accept them:




---
JAX-WS endpoints must follow these requirements:

* The implementing class must be annotated with either the  
javax.jws.WebService or javax.jws.WebServiceProvider annotation.
* The implementing class may explicitly reference an SEI through the  
endpointInterface element of the @WebService annotation, but is not  
required to do so. If no endpointInterface is not specified in  
@WebService, an SEI is implicitly defined for the implementing class.
* The business methods of the implementing class must be public, and  
must not be declared static or final.
* Business methods that are exposed to web service clients must be  
annotated with javax.jws.WebMethod.
* Business methods that are exposed to web service clients must have  
JAX-B-compatible parameters and return types. See Default Data Type  
Bindings.
* The implementing class must not be declared final and must not be  
abstract.

* The implementing class must have a default public constructor.
* The implementing class must not define the finalize method.
* The implementing class may use the javax.annotation.PostConstruct or  
javax.annotation.PreDestroy annotations on its methods for lifecycle  
event callbacks.


The @PostConstruct method is called by the container before the  
implementing class begins responding to web service clients.


The @PreDestroy method is called by the container before the endpoint  
is removed from operation.

---

The classes generated by deftype are final; the javap output is

---
public final class com.example.FooBarService extends java.lang.Object  
implements com.example.FooBarInterface{

  public static {};
  public com.example.FooBarService();
  public java.lang.String createCustomer(java.lang.String,  
java.lang.String, java.lang.String);

}
---

That doesn't make JAX-WS's tools happy:

---
com.sun.tools.ws.processor.modeler.ModelerException: modeler error:  
Classes annotated with @javax.jws.WebService must not be final.

---

Digging into the compiler, it looks like individual fields can be made  
non-final by flagging them as mutable. The class emitter in `compile`,  
however, always passes ACC_PUBLIC + ACC_SUPER + ACC_FINAL to the  
ClassVisitor.visit method... so classes produced in this way are  
always final.


From a purity perspective, this is nice: Clojure prohibits  
implementation inheritance because it's a Bad Idea®. However, this  
hampers interop (I don't *want* to do implementation inheritance, but  
JAX-WS demands non-final classes!), and the mutable/volatile options  
for fields seem to suggest that getting hands dirty is acceptable at  
the level of deftype… and thus having some optional way to produce non- 
final classes might be a reasonable feature to add.


Chouser suggested gen-class, which generates non-final classes; that's  
my next fallback, once annotation support for gen-class gets  
integrated. However, if this isn't an intentional and inflexible  
limitation on deftype, I thought I'd investigate removing it. So I  
spent a little time with the compiler, producing three patches:


   * Pass options through to deftype emission (they never made it  
past deftype in the current code).
   * Propagate class accessibility flags throughout build/compile  
(parameterizing the ACC_FINAL/ACC_PUBLIC/ACC_SUPER flags).

   * Allow (and check for) :non-final as a boolean option for deftype.

The test suite runs without problems, and I've verified that adding  
`:non-final true` generates a non-final class (and the reverse). With  
this change I'm able to run wsgen against a jar built by Leiningen,  
containing no Java code, and get WSDL as output. Pretty neat, no?


I've pushed these to my GitHub fork (rather than sending patches to  
the list):




Opinions, thoughts, critiques, "you're insane"s, etc. welcome.

Thanks,

-Richard


--
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: Defining a namespace inside a let

2010-04-27 Thread Adrian Cuthbertson
Not sure about your specific case, but when you don't get back
expected results it's usually due to the functions called being lazy.
Try doall or dorun to force the iterations.

-Rgds, Adrian.

On Tue, Apr 27, 2010 at 12:25 AM, David McNeil  wrote:
> I am experimenting with clojure.test and I encountered the following
> situation which I cannot explain.
>
> This code:
>
> (println (do
>           (ns ns01
>             (:use clojure.test))
>           (deftest test1 nil)
>           (run-tests)))
>
> Produces the expected result (note: it runs one test):
>
>    Testing ns01
>
>    Ran 1 tests containing 0 assertions.
>    0 failures, 0 errors.
>    {:type :summary, :test 1, :pass 0, :fail 0, :error 0}
>
> However, if I do the exact same thing inside of a let:
>
> (println (let []
>           (do
>             (ns ns02
>               (:use clojure.test))
>             (deftest test1 nil)
>             (run-tests
>
> Then I get the unexpected result that no tests are executed:
>
>    Testing ns02
>
>    Ran 0 tests containing 0 assertions.
>    0 failures, 0 errors.
>    {:type :summary, :test 0, :pass 0, :fail 0, :error 0}
>
> Seems there is something going on with namespaces that I do not
> understand and I hope that somewhere here can explain it.
>
> Thank you.
> -David McNeil
>
> --
> 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


gen-class, polymorphism, etc.

2010-04-27 Thread Lucas Caballero
Hello,

I'm trying to write some Java code that uses reflection to pull some data
from clojure generated classes.  Do clojure .class, or generated Java class
files actually extend the type specified in the extends and implements
clauses?  That is will they also work polymorphically in normal Java code?
 In my case I am attempting to understand what it is the .NET DLR output.

Any information on this topic is most appreciated.

Cheers,
L-

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

Beginner's question regarding implementing a constructor using gen-class

2010-04-27 Thread Gregg Williams
Hi--another example, another failed attempt to do something that looks
quite basic (grumble, grumble) ...

I'm continuing on my path to learning how to use Clojure with the
graphics library Piccolo2D (http://www.piccolo2d.org) by re-
implementing some of Piccolo2D's sample programs. Now I'm working on
the "Graph Editor" program described at 
http://www.piccolo2d.org/learn/grapheditor.html
.

In this sample program (which you don't really need to look at) I need
to subclass a Piccolo2D class to create a class named GraphEditor ,
but unlike the last time (see "Java interop question: proxy or gen-
class?" in this group), I can't subclass using proxy because *this*
subclass, GraphEditor, adds a new method. This means I have to use gen-
class.

Not unexpectedly, GraphEditor wants to execute a number of Java
statements when a new instance is instantiated. Pretty simple, eh?
Well, nothing I tried worked. So my next step was to try to debug the
simplest possible gen-class example I could come up with, which
follows:

-
(ns genclassInteger
 (:gen-class
   :extends [java.lang.Integer])
 ;(:import (java.lang   Integer))   ; < unnecessary, right?
 )

(defn -genclassInteger [this number]
  (println "It worked!")
  number)

(defn -main []
  (println (new genclassInteger 666)))
-

This returns the error:

 Exception in thread "main" java.lang.ClassNotFoundException:
[java/lang/Integer] (genclassInteger.clj:1)

Now, turn your attention to the line marked with "<". After I got
the above error, I added this line (minus the leading semincolon), and
I got the same error.

My goal with GraphEditor is to add some behavior to its constructor
(when compared with its superclass). Apart from whatever caused the
error in the toy program above, is the creation of a function whose
name is "-" followed by the name of the subclass (e.g., -GraphEditor, -
genclassInteger) the right way to do so?

As always, I appreciate any suggestions/pointers/etc.

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