Re: Newbie question about vector performance

2010-05-28 Thread Michael Gardner
On May 28, 2010, at 8:47 AM, Rubén Béjar wrote:

> I would thank a lot any hint, suggestion, comment, or
> whatever... :-)

As a style issue I'd suggest using inc, dec, neg?, pos?, and zero? instead of 
the various (+ x 1), (< x 0), etc. in your code. This actually seems to improve 
performance a bit on my laptop, but it's nothing amazing. To get good 
performance you're likely going to need to do some type hinting.

http://clojure.org/java_interop#Java%20Interop-Type%20Hints

-- 
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: Elegant way of expressing this in Clojure?

2010-05-27 Thread Michael Gardner
On May 27, 2010, at 1:11 PM, CuppoJava wrote:

> Hi,
> I have a little snippet of Java code that I want to express in
> Clojure. But all my attempts thus far have been much more unreadable
> than the equivalent Java. Some help would be greatly appreciated.

It might help if we knew the purpose of your code. A higher-level approach 
might be appropriate, but it's hard to say without knowing what the code is for.

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


Re: promoting contrib.string to clojure, feedback requested

2010-05-27 Thread Michael Gardner
On May 27, 2010, at 2:45 AM, Stefan Kamphausen wrote:

> Hi,
> 
> On May 26, 11:00 pm, Stuart Halloway 
> wrote:
>> The people have spoken! The trims have it!
> 
> sorry, I'm a little late.  However, to me it is not clear what the
> trim functions shall do.  If they become a replacement for chomp they
> are clearly misnamed.  In many applications and languages (like Excel,
> several SQL variants, oh, and Java, ...) "trim" means stripping of
> whitespace characters, including but not limited to \n and \r.  In
> contrast to that chomp stands for the removal of the system-specific
> linebreak.

I disagree that "trim" is a misnomer for this function. It may be used as 
shorthand for "trim whitespace" in some programming languages, but it's not 
universal even within CS, and it's not what "trim" by itself means in English. 
And Clojure has already shown that it is willing to break with established 
terminology in order to Get Things Right.

> Usually trim-functions accept an optional list of chars to trim.  So
> if a trim function would be implemented as a char-remover, defaulting
> to whitespace, and chomp would then be equivalent to (trim "foo\n" "\r
> \n") -- the second arg should probably something seqable -- everything
> seems fine to me.

I agree with the proposal for a second, optional argument to trim; I'm 
ambivalent about whether chomp would still be necessary then. I'd prefer the 
optional trim argument to be an actual set of characters (the semantics are 
more appropriate, and it might allow a more performant implementation), but I 
wouldn't object too hard if it accepted any seq of chars instead.

-- 
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: Announcing Clojure/core

2010-05-25 Thread Michael Gardner
On May 25, 2010, at 7:30 AM, Rich Hickey wrote:

> Note: clojure.com will now resolve to the Clojure/core site. Come
> check it out!

Does this mean Clojure itself is to be directly associated with Relevance, 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


Re: Dividing list by predicate

2010-05-23 Thread Michael Gardner
Thanks for the tips, guys. I'll go with seq-utils/separate, as I don't really 
need the extra performance; it was more of an aesthetic thing. Still, it seems 
like separate should be written to evaluate its predicate only once for each 
item in 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


Dividing list by predicate

2010-05-23 Thread Michael Gardner
I need to use a predicate to divide one list into two, one containing all 
values for which the predicate is true, and the other with all remaining 
values. I can do this easily by filtering twice, once with the predicate and 
once with its complement, but is there some core or contrib function that will 
do this more directly and efficiently? I'm not sure what the best way to search 
for something like this would be.

-- 
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: reducing multiple sets

2010-05-22 Thread Michael Gardner
On May 22, 2010, at 10:00 AM, mikel wrote:

> Trying to get from here:
> #{#{[3 2] [5 4] [3 3] } #{[4 3] [5 4] [3 3] } #{[3 2] [2 2] [3 3] } }
> to here:
> #{[3 2] [5 4] [4 3] [2 2] [3 3] }
> that is, combining the set of sets into one set.


(apply clojure.set/union #{#{[3 2] [5 4] [3 3]} #{[4 3] [5 4] [3 3]} #{[3 2] [2 
2] [3 3]}})

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


Re: functional check slower than imperative?

2010-05-19 Thread Michael Gardner
On May 18, 2010, at 10:45 AM, Christophe Grand wrote:

> (every? f coll1 coll2) is not supported.

Is there a reason for this? It seems like an obvious improvement to make 
every?, some, etc. take multiple args just like map.

-- 
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: Style preference: (:key map) or (map :key)?

2010-05-17 Thread Michael Gardner
On May 17, 2010, at 10:10 AM, Sean Devlin wrote:

> From the in progress Clojure library standards[1] page:
> 
> * Use keyword-first syntax to access properties on objects:
> 
> (:property object-like-map)
> 
> * Use collection-first syntax to extract values from a collection (or
> use get if the collection might be nil).
> 
> (collection-like-map key) (get collection-like-map key)

Makes sense. Thanks.

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


Style preference: (:key map) or (map :key)?

2010-05-17 Thread Michael Gardner
It appears the (:key map) style is more common than (map :key) among 
Clojurians. Is this true?

So far I'm doing (map :key) because it's more familiar, and because it doesn't 
make me change styles when using something besides keywords as keys (admittedly 
rare so far). But I'd like to hear other views before I get too set in my ways. 
Which style do you use, and why?

-- 
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: setting vars with macro

2010-05-17 Thread Michael Gardner
On May 15, 2010, at 4:56 PM, islon wrote:

> I'm working in a simple single-thread console-based rpg game in
> clojure (a port from my own scala version)
> and didn't want to use any concurrency structure because the game is
> single threaded.
> I was thinking about a macro like
> 
> (defmacro set!! [s val]
>  `(def ~s ~val))
> 
> so I can set my game state without using transactions, agents, etc.
> Any comments about it?

I wrote a Pong clone in Clojure just to stretch my mind a bit. Here's what I do:

The entire game world is a single struct-map (may become a record at some 
point), stored in a global ref. When my world-update timer fires, I deref the 
world and then pass it to my world-updater functions. These updaters return the 
new world state, which I then set via ref-set.

This is almost as simple as the usual global vars approach, and allowed me to 
trivially split the world-drawing code into another thread that runs on a 
different schedule. It also permits stuff like printing out or saving the 
entire world state very easily.

Actually, were it not for Swing, I wouldn't even be using mutable structures at 
all-- I'd be passing the world state from update to update using tail-recursion.

You might also be interested in these links:

http://nakkaya.com/2009/12/19/cloning-pong-part-1/ (I originally based my Pong 
game on this code)
http://jng.imagine27.com/articles/2009-09-12-122605_pong_in_clojure.html 
(cloning Pong is apparently pretty popular!)
http://briancarper.net/blog/making-an-rpg-in-clojure-part-one-of-many (site 
down at the moment)
http://prog21.dadgum.com/23.html (not Clojure, but interesting for functional 
games in general)

-- 
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: dump html with clojure

2010-05-13 Thread Michael Gardner
On May 13, 2010, at 7:52 AM, Nurullah Akkaya wrote:

> Yes but AFAIK you only get availableProcessors + 2 threads with pmap
> which is fine when the task is CPU bound but for downloading web pages
> most of the time will be lost at waiting for I/O so having more
> threads would speed things up.

Seems like pmap could use some control over the number of threads.

-- 
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: Calling apply on java methods

2010-05-11 Thread Michael Gardner
On May 11, 2010, at 12:39 PM, Alexandre Patry wrote:

> I am trying to call a java method using apply, like :
> 
> (apply .println [System/out "hello" "world"])
> 
> But I get an error: "Unable to resolve symbol: .println in this context"
> 
> Am I missing something?

Unfortunately, Java methods are not currently first-class functions in Clojure. 
But you can use memfn to create one (see "Java Interop" section at clojure.org):

(apply (memfn println s) [System/out "hello world"])

(Note that your original version won't work because println only takes one 
String argument.) You can also do (apply #(.println %1 %2) [System/out "hello 
world"]), of course.

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


Re: clojure 1.2 seq fn enhancement FAQ

2010-04-30 Thread Michael Gardner
On Apr 30, 2010, at 6:33 AM, Rich Hickey wrote:

> Would contains-val? be fast for sets?  As a user of sets, I consider them 
> collections of values, and I absolutely would reach for contains-val? in any 
> library that had it, for use with sets. If so, and I used contains-val?, and 
> I moved code from using sets to maps (happens frequently), or vectors (also 
> happens) my perf would suddenly stink. If not fast on sets, why not? The 
> reason isn't supported by the name.

You should understand the performance characteristics of the data structures 
you employ, under whatever operations you subject them to. Being surprised when 
your code that looks stuff up by value slows down when you switch from sets to 
something else doesn't strike me as reasonable.

> 'contains?' and 'get' abstract over fast lookup. They are polymorphic on the 
> collection type and on the nature of the looked-up thing. For maps the 
> looked-up thing is a key, for sets: a value, for vectors, strings and arrays: 
> an index.  Calling it contains-key? doesn't make them the same, nor add any 
> value.

The objects in a set are both keys and values, and an index *is* a key. The 
documentation for get and contains? implies this, as others have noted.

> Renaming contains? is not on the table.

I hope you will reconsider this, if not now then at some point in the future.

> For people that understand its relationship with get, it makes perfect sense 
> (and I don't think I'm the only one :).

As would contains-key?, I think.

> And there is a lot of client code.

No need to rename immediately; alias and deprecate.

> And no one has come up with a better name that doesn't include caveats.

I don't see any caveats for contains-key? other than the one addressed above.

> I do understand that this use of the word differs from that used in e.g., 
> Java. But I'll make the same argument to Java devs that I do to the Lispers, 
> who have seen many more of their prized words repurposed in Clojure (assoc, 
> loop, do et al):
> 
>The words can't mean the same thing forever without trapping us in the 
> same semantics forever, and there are only so many good words.
> 
> Everyone has to realize that this level of polymorphism in Clojure is 
> unusual. I haven't seen a library with equivalents to get and contains?. 
> Heck, in Java, Maps aren't even collections! So, should we adopt their 
> nomenclature because it is familiar?

contains? isn't a bad name because it's used differently in Java. It's bad 
because it's ambiguous, and because there's a much clearer name available.

> I agree that contains?'s behavior on vectors is confusing for newcomers. 
> That's not a reason for it to be different.

I disagree.

> The only options for right now are:
> 
> A) I remove seq-contains?
> B) I rename seq-contains?

Something involving the word 'scan', perhaps?

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


Re: clojure 1.2 seq fn enhancement FAQ

2010-04-29 Thread Michael Gardner
On Apr 29, 2010, at 3:21 AM, ataggart wrote:

> I know it won't matter, but for posterity if nothing else...
> 
> Functions named contains-key? and contains-val? would make a lot more
> sense to me than the current contains? and new seq-contains?.  Anyone
> looking at contains-val? should expect it to be O(n).  The only
> effective difference would be that the test value for contains-val? is
> consistently a single value rather than a [key value] tuple for maps.

+1. I can't imagine any use case for looking up a whole [key, value] pair in a 
hash-map.

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


Re: clojure 1.2 seq fn enhancement FAQ

2010-04-28 Thread Michael Gardner
On Apr 28, 2010, at 6:38 PM, Mark Engelberg wrote:

> 2.  Rename contains? to contains-key? and make a function with the new
> semantics called contains?  Disadvantage - breaks existing code.

You could also alias contains? to contains-key?, use seq-contains? (or some 
other new name) for this new function, and deprecate contains? with an eye 
towards removal down the road. Deprecation has been used successfully even in 
mature languages, hasn't it?

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


Re: Beginners question about Don't know how to create ISeq from: Symbol

2010-04-19 Thread Michael Gardner
On Apr 19, 2010, at 4:52 PM, uap12 wrote:

> (defn -main
>(make-lotto))
> ([] (-main )))

You're missing the empty arglist in your definition of -main. It should be:

(defn -main []
  (make-lotto))

-- 
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: mutating multiple java swing components

2010-04-09 Thread Michael Gardner
On Apr 9, 2010, at 10:39 AM, Josh Stratton wrote:

> Here's an example of trying to use "map" to bring the two tables
> together together in the same scope.  I'm doing this because the nth
> element in one sequence correlates to its nth counterpart in the
> second sequence.  mainTables is a sequence of JTables and fixedTables
> is an equally sized sequence of JTables.  For some reason though it
> appears that setupTable is never being called.  Why is that?  Is there
> a better way to do this?

Map is lazy. Don't use it when you just want side-effects; use doseq instead, 
as John Williams already suggested.

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

To unsubscribe, reply using "remove me" as the subject.


Re: A syntax question: positional & keyword

2010-04-06 Thread Michael Gardner
On Apr 6, 2010, at 6:08 PM, Sophie wrote:

> Don't you think
>  - fixed-order named parameters
> could (should?) be a separate issue from
>  - optional, any-order, named parameters
> ?

I don't see the advantage of fixed-order named parameters over keyword 
parameters. Note that you can require certain keyword parameters simply by 
throwing an exception when they're omitted.

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

To unsubscribe, reply using "remove me" as the subject.


Re: Mutually-referencing structures

2010-04-06 Thread Michael Gardner
On Apr 6, 2010, at 9:01 AM, Laurent PETIT wrote:

>* BUT : isn't the real problem that one will not content
> [him/her]/self with playing with in-memory data ? One will want to
> make the data persistent (outside-of-process, aka
> storage-persistance). And with this kind of problem, one will have a
> graph of identities containing references to other identities (the
> needed level(s) of indirection). How does one painlessly store/unstore
> those graphs to databases ?

XML with ID/IDREFs seems the obvious choice.

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

To unsubscribe, reply using "remove me" as the subject.


Re: Mutually-referencing structures

2010-04-05 Thread Michael Gardner
On Apr 5, 2010, at 4:34 PM, Sophie wrote:

> But single-assignment is a quite valid (and more flexible?)  form of
> immutability. I'm not convinced cycles are intrinsically tied to it in
> any way.

If you can assign to it, it's mutable. What you're talking about is creating a 
mutable object, then making it immutable at some point (say, after it's first 
assigned to). Immutable object cycles are indeed possible if one permits this; 
Clojure doesn't, at least not for simple objects.

-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

To unsubscribe, reply using "remove me" as the subject.


Re: Mutually-referencing structures

2010-04-05 Thread Michael Gardner
On Apr 5, 2010, at 7:49 AM, Sophie wrote:

> Is this a Clojure restriction, or is it intrinsic to functional
> programming?

It's a consequence of immutable data structures, which are an aspect of 
functional programming. An immutable object can never be changed, and you can't 
create multiple objects simultaneously, so you can't ever get a cycle of direct 
references between immutable objects.

> If my app is essentially about a user creating and editing a graph
> structure (sometimes via crud-level interactions, other times by
> somewhat larger refactorings), is either Clojure or functional not a
> good match?

You can still do things functionally, with a level of indirection. I don't see 
any problem with that approach, but whether it's a good match for your project 
is up to you.

Otherwise, you can use mutable structures. Clojure certainly supports 
mutability, but as a primarily-functional language it might not be the right 
tool for the job if you're going to use mutability extensively.

-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

To unsubscribe, reply using "remove me" as the subject.


Re: Mutually-referencing structures

2010-04-05 Thread Michael Gardner
On Apr 5, 2010, at 2:09 AM, Sophie wrote:

> (deftype Account [owner balance])
> (deftype Person [accounts])
> 
> joe has 1 account.
> 
> How to I create / initialize joe & the account with mutual references?
> I'd rather not use refs.

You can't do it directly without one of the two being mutable. But you could 
simulate it by assigning each object a unique ID and having them refer to each 
other by that ID.

-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: map-filter, is this in the API?

2010-03-19 Thread Michael Gardner
On Mar 19, 2010, at 12:54 PM, Greg Fodor wrote:

> Very simple function:
> 
> (defn map-filter [f coll]
>  (map f (filter f (coll)))

You have an extra parenthesis before coll.

> Is there an API function for this that I am missing? For example, it
> is useful for pulling out all values in a list of maps of a certain
> key that is optional:
> 
> Clojure=> (map-filter :k [{:a :b :c :d :k :found} {:a :b :c :d}
> {:a :b :c :d :k :other}])
> (:found :other)

I don't think so. But note that you can do the filter after the map, to avoid 
applying f twice per element:

(defn map-filter-2 [f coll]
(filter identity (map f coll)))

-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

To unsubscribe from this group, send email to 
clojure+unsubscribegooglegroups.com or reply to this email with the words 
"REMOVE ME" as the subject.


Re: getRuntime exec call?

2010-03-19 Thread Michael Gardner
On Mar 19, 2010, at 6:07 AM, TimDaly wrote:

> (defn cmdresult [cmdstr]
>  (let [args (into [] (seq (.split cmdstr " ")))]
>  (BufferedReader.
>(InputStreamReader.
>  (. (. (. Runtime (getRuntime)) (exec args))
> (getInputStream))

Why do (into [])? .exec expects a String[], exactly what .split gives you.

I wrote something similar recently (included below, in case you're interested). 
You could easily wrap it with a .split to get similar functionality, though 
personally I prefer passing the args as a list because that way you can deal 
with paths with spaces in them.

-Michael

(defn cmd [command & args]
"Runs command with args and returns its stdout, stderr, and exit status."
(let [process (.start (ProcessBuilder. (into-array (cons command args]
(.waitFor process)
(hash-map
:output
(line-seq
(java.io.BufferedReader.
(java.io.InputStreamReader.
(.getInputStream process
:error
(line-seq
(java.io.BufferedReader.
(java.io.InputStreamReader.
(.getErrorStream process
:status
(.exitValue process

(defn do-cmd [command & args]
"Runs command with args and returns its stdout as a seq of lines. Throws 
exception on failure."
(let [command (apply cmd command args)]
(if (zero? (:status command))
(:output command)
(throw
(Exception.
(str "Command failed: "
(apply str
(interpose "\n"
(:error command)

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

To unsubscribe from this group, send email to 
clojure+unsubscribegooglegroups.com or reply to this email with the words 
"REMOVE ME" as the subject.


Re: Translation from Common Lisp 1

2010-03-18 Thread Michael Gardner
On Mar 18, 2010, at 4:17 PM, David Nolen wrote:

> On Thu, Mar 18, 2010 at 4:25 PM, alux  wrote:
> Hello!
> 
> I much enjoyed reading the tutorial http://www.lisperati.com/casting.html
> , mentioned by eyeris today. The most mind-extending thing (to me,
> having Java background) is the, admittedly non-idiomatic, use of
> symbols as data.
> 
> But I have two translation problems, I want to pose before going to
> sleep (its pitch dark in Europe :). First the easy one:
> 
> Common Lisp
> (defun describe-path (path)
>  `(there is a ,(second path) going ,(first path) from here.))
> 
> You probably want
> 
> (defn describe-path [path]
>`(~'there ~'is ~'a ~(second path) ~'going ~(first path) ~'from ~'here)) 

Or just:

(defn describe-path [path]
['there 'is 'a (second path) 'path 'going (first path) 'from 'here])

But using symbols for something like this is a bit contrived anyway.

-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

To unsubscribe from this group, send email to 
clojure+unsubscribegooglegroups.com or reply to this email with the words 
"REMOVE ME" as the subject.


Re: Java method call irritation

2010-03-18 Thread Michael Gardner
On Mar 18, 2010, at 10:55 AM, Per Vognsen wrote:

> Is there any reason why a .method occurrence in non-operator position
> doesn't just do the closure wrapping automagically?

I'd like to know this as well. Smooth Java interop is one of Clojure's selling 
points, but having to wrap Java methods in lambdas to use them as first-class 
functions feels awkward. Especially if the method in question has multiple 
arguments!

-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

To unsubscribe from this group, send email to 
clojure+unsubscribegooglegroups.com or reply to this email with the words 
"REMOVE ME" as the subject.


Spurious STDERR output ("Picked up JAVA_TOOL_OPTIONS")

2010-03-12 Thread Michael Gardner
I noticed that when I set JAVA_TOOL_OPTIONS, clojure outputs the following to 
STDERR before it runs my code:

Picked up JAVA_TOOL_OPTIONS: ...

This interferes with e.g. running clojure scripts as cron jobs, since it's 
common to rely on the presence of output on STDERR to signal errors.

Obviously one can work around this with IO redirection plus grep or tail, but 
that's ugly and a pain. Is there a way to turn off this output entirely, or at 
least to get clojure to put it on STDOUT instead?

-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: collections within collections

2010-03-10 Thread Michael Gardner
On Mar 10, 2010, at 12:20 PM, Glen Rubin wrote:

> However, the output of my trips function yields multiple collections
> of vectors inside of a larger vector.  I am completely befuddled as to
> how to process this behemoth.

You can merge the structure into a single list of triples by applying concat:

user=> (def squares (apply concat (trips (range 1 7
#'user/squares
user=> squares
([1 2 2.23606797749979] [1 3 3.1622776601683795] [1 4 4.123105625617661] [1 5 
5.0990195135927845] [1 6 6.082762530298219] [2 3 3.605551275463989] [2 4 
4.47213595499958] [2 5 5.385164807134504] [2 6 6.324555320336759] [3 4 5] [3 5 
5.830951894845301] [3 6 6.708203932499369] [4 5 6.4031242374328485] [4 6 
7.211102550927978] [5 6 7.810249675906654])

Then test for perfect squares with this (assuming a and b are always integers):

user=> (defn perfect-square? [[a b c]] (integer? c))
#'user/perfect-square?
user=> (filter perfect-square? squares)
([3 4 5])

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


Re: apply-ing Java methods

2010-03-09 Thread Michael Gardner
On Mar 8, 2010, at 11:20 PM, Michał Marczyk wrote:

> It's simple to write this way... And if you provide type hints, I'd
> expect the resulting function to be quite performant. If you don't
> care about that, here's a flexible alternative using eval:
> 
> user> (defmacro methodfn [name]
>`(fn [& args#]
>   (apply
>(eval (concat (list '~`memfn '~name)
>  (map (fn [~'_] (gensym)) (rest args#
>args#)))
> #'user/methodfn
> user> ((methodfn substring) "asdf" 1)
> "sdf"
> user> ((methodfn substring) "asdf" 1 3)
> "sd"

Thanks for the suggestion! I'm a little surprised that Clojure would adopt an 
awkward API purely for performance reasons, but I guess the beauty of Lisp is 
that I can fix it myself.

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


Re: apply-ing Java methods

2010-03-09 Thread Michael Gardner
On Mar 8, 2010, at 1:06 PM, Nurullah Akkaya wrote:

> Using this,
> 
> http://paste.lisp.org/display/67182
> 
> would allow you to do,
> 
> (let [config {:str "fred" :beg 2 :end 3}]
>  (apply (jfn 'substring) (map config [:str :beg :end])))

That's quite nice. Thanks!

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


Re: apply-ing Java methods

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

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

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

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


Re: apply-ing Java methods

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

> See memfn in Clojure API docs.

Thanks for the tip. After some experimentation:

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

but why does memfn require these "dummy" arguments?

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


apply-ing Java methods

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

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

rather than:

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

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

-Michael

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


Re: "Unable to resolve symbol" in third-party jarfile

2010-03-03 Thread Michael Gardner
On Mar 3, 2010, at 8:42 AM, Michael Wood wrote:

> On 2 March 2010 17:40, Michael Gardner  wrote:
>> As part of a project to help me learn Clojure, I'm trying to send an
>> email using code like 
>> http://nakkaya.com/2009/11/10/using-java-mail-api-from-clojure/.
>> For the JavaMail API I'm using GNU JavaMail, which in turn requires
>> GNU JAF (activation.jar). When I try to run my program, I get:
>> 
>>> Exception in thread "main" java.lang.Exception: Unable to resolve symbol: � 
>>> in this context (activation.jar:0)
>> 
>> (The symbol in question shows up for me as a diamond with a question
>> mark in it.) Any clues on what this could be? Since it's dying at line
>> 0, maybe it's choking on a UTF BOM or something?
> 
> How are you trying to run it?  It looks like it's trying to treat
> activation.jar as a Clojure source file.

Ah, that was it. I was using clojure's -cp option with a path with a wildcard 
at the end, but forgot to enclose the path in quotes. Clojure was interpreting 
the second jarfile in that dir as the script I was asking it to run.

It would be nice if clojure would die with a message about "too many arguments" 
in such a case, but I'm just happy to have figured it out. 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


"Unable to resolve symbol" in third-party jarfile

2010-03-03 Thread Michael Gardner
As part of a project to help me learn Clojure, I'm trying to send an
email using code like 
http://nakkaya.com/2009/11/10/using-java-mail-api-from-clojure/.
For the JavaMail API I'm using GNU JavaMail, which in turn requires
GNU JAF (activation.jar). When I try to run my program, I get:

> Exception in thread "main" java.lang.Exception: Unable to resolve symbol: � 
> in this context (activation.jar:0)

(The symbol in question shows up for me as a diamond with a question
mark in it.) Any clues on what this could be? Since it's dying at line
0, maybe it's choking on a UTF BOM or something?

-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


<    1   2   3