side effects in fn passed to filter

2010-02-18 Thread Rowdy Rednose
The filter documentation reads: Returns a lazy sequence of the items in coll for which (pred item) returns true. pred must be free of side-effects. So that means I should not write a function like this: (defn unique [sc] Returns a lazy sequence with all consecutive duplicates removed (let

Re: side effects in fn passed to filter

2010-02-18 Thread Rowdy Rednose
, Rowdy Rednose rowdy.redn...@gmx.net wrote: The filter documentation reads: Returns a lazy sequence of the items in coll for which (pred item) returns true. pred must be free of side-effects. So that means I should not write a function like this: (defn unique [sc] Returns a lazy sequence

Re: side effects in fn passed to filter

2010-02-18 Thread Rowdy Rednose
is a better demo of how do I maintain state across a lazy sequence without requiring any mutation? Stu Hi, On Feb 18, 3:04 pm, Rowdy Rednose rowdy.redn...@gmx.net wrote: Returns a lazy sequence of the items in coll for which (pred item) returns true. pred must be free of side-effects

Re: side effects in fn passed to filter

2010-02-18 Thread Rowdy Rednose
am, Rowdy Rednose rowdy.redn...@gmx.net wrote: Various interesting approaches to this problem... thanks guys! Stu, you're right, distinct builds up a set of all items encountered, which I don't need, and it's costly. But that function it is indeed a good model. So here's a side effects

Re: headMap / tailMap / subMap

2010-01-31 Thread Rowdy Rednose
: On Sat, Jan 30, 2010 at 7:31 PM, Rowdy Rednose rowdy.redn...@gmx.net wrote: I want to have it in O(1). That's why I use a tree map in the first place. On Jan 31, 9:15 am, Sean Devlin francoisdev...@gmail.com wrote: If you can live with an O(n) operation, take/drop-with will do the job

headMap / tailMap / subMap

2010-01-30 Thread Rowdy Rednose
How would I do something like these 3 TreeMap operations with clojure's sorted-map? The goal is to narrow down a map to include only keys with a given prefix. -- You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to

Re: headMap / tailMap / subMap

2010-01-30 Thread Rowdy Rednose
I want to have it in O(1). That's why I use a tree map in the first place. On Jan 31, 9:15 am, Sean Devlin francoisdev...@gmail.com wrote: If you can live with an O(n) operation, take/drop-with will do the job. Sean On Jan 30, 6:59 pm, Rowdy Rednose rowdy.redn...@gmx.net wrote: How would

a regular expression for matching and for generating?

2009-07-29 Thread Rowdy Rednose
Does anybody know an elegant way to have a regex pattern with capturing groups like in this simple example: user= (def pat #^foo=([^;]*);bar=([^;]*);$) #'user/pat user= (re-seq pat foo=F0o;bar=bAr;) ([foo=F0o;bar=bAr; F0o bAr]) And reuse that pattern to generate a text that replaces the groups

Re: let a variable number of bindings

2009-07-18 Thread Rowdy Rednose
Thanks Meikel, your negative answer actually helped me analyze my problem better and get the distinction run time / expansion time straight. I figured that the names of the bindings are already there at expansion time and it's only the values that I need to retrieve at run time. So this version

Re: let a variable number of bindings

2009-07-18 Thread Rowdy Rednose
As I said, the example was stripped down for simplicity, and that simple version doesn't make much sense other than for communicating my problem. My real val-fn is not a map, it's indeed a couple of functions, applied to some data that's passed in. But the result of that acts like a map: it

On Lisp = Clojure

2009-07-18 Thread Rowdy Rednose
Hi all, in my quest to learn clojure (and lisp in general), I'm currently trying to translate some the On Lisp code to clojure. The first couple of functions and macros in Chapter 19 A Query Compiler read: (defun make-db (optional (size 100)) (make-hash-table :size size)) (defvar

Re: On Lisp = Clojure

2009-07-18 Thread Rowdy Rednose
On Jul 19, 12:53 pm, Richard Newman holyg...@gmail.com wrote: * Can the body of the db-push function be simplified? I think so. Untested: (defn db-push   ([key val] (db-push key val *default-db*))   ([key val db]      (swap! db assoc key (cons val (db key)) If I add an @ it runs:

let a variable number of bindings

2009-07-17 Thread Rowdy Rednose
How can I lexically bind names like let does in a macro, when names and values for those bindings are passed in? This here works fine when I pass a literal collection: (defmacro let-coll [coll body] `(let ~(vec coll) ~...@body)) user= (let-coll [a 11 b 22] (list b a)) (22 11) Doing the

The snake again - Programming Clojure book

2009-07-10 Thread Rowdy Rednose
Why does the Snake example in the book use refs when all mutation is done from the EDT? To verify that, I put a call to assert-edt in front of every dosync in snake.clj. assert-edt is defined like this: (defn assert-edt [] (assert (javax.swing.SwingUtilities/ isEventDispatchThread))) And it

Re: The snake again - Programming Clojure book

2009-07-10 Thread Rowdy Rednose
On Jul 10, 10:28 pm, Stuart Halloway stuart.hallo...@gmail.com wrote: binding to a thread. The snake game could be extended to be a   simulation that runs on multiple threads (or perhaps even multiple   Makes sense. For the example in the book it just seemed completely redundant. And when

Re: The snake again - Programming Clojure book

2009-07-10 Thread Rowdy Rednose
This did the trick: (ns clojure.core) (defmacro dosync [ body] `(do (assert (not (javax.swing.SwingUtilities/isEventDispatchThread))) (#'dosync ~...@body))) This is great for testing! Thanks for your help, Stu. And btw the book is really great so far (and I'm almost through)! It

Re: The snake again - Programming Clojure book

2009-07-10 Thread Rowdy Rednose
refer to the original dosync from my new dosync macro? On Jul 10, 11:52 pm, Rowdy Rednose rowdy.redn...@gmx.net wrote: This did the trick: (ns clojure.core) (defmacro dosync [ body]   `(do      (assert (not (javax.swing.SwingUtilities/isEventDispatchThread)))      (#'dosync ~...@body

Re: The snake again - Programming Clojure book

2009-07-10 Thread Rowdy Rednose
The idea is to have all existing code (that gets recompiled after my redefinition) benefit from my changes automatically, although I fear it's not considered good style to do this. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the

Re: adding a member to a set in a map in a map in a map in a ref

2009-07-05 Thread Rowdy Rednose
schrieb Rowdy Rednose: user= (dosync (alter gnu-rms update-in [:key1 :key2 :key3] #(conj % foo))) {:key1 {:key2 {:key3 #{foo You actually don't need the anonymous function... (dosync    (alter gnu-rms update-in [:key1 :key2 :key3] conj foo)) Sincerely Meikel  smime.p7s

adding a member to a set in a map in a map in a map in a ref

2009-07-04 Thread Rowdy Rednose
Say I have a data structure like this (def ref-map-map-map-set (ref {:key1 {:key2 {:key3 #{)) If I want to add a new member to the set which is the value of :key3, I currently do this: (dosync (let [key1 (:key1 @ref-map-map-map-set) key2 (:key2 key1) key3 (:key3 key2)]

Re: adding a member to a set in a map in a map in a map in a ref

2009-07-04 Thread Rowdy Rednose
:07 AM, Rowdy Rednose rowdy.redn...@gmx.net wrote: Say I have a data structure like this (def ref-map-map-map-set (ref {:key1 {:key2 {:key3 #{)) If I want to add a new member to the set which is the value of :key3, I currently do this: (dosync  (let [key1 (:key1 @ref-map-map-map

Re: adding a member to a set in a map in a map in a map in a ref

2009-07-04 Thread Rowdy Rednose
On Jul 5, 1:23 pm, Richard Newman holyg...@gmail.com wrote: However, I'd point out that: * This might be a sign that you're doing things 'wrong'. Could be. I'm still figuring out how to do things the functional way. The reason I have these nested maps is to give my data structure. I don't

Re: adding a member to a set in a map in a map in a map in a ref

2009-07-04 Thread Rowdy Rednose
On Jul 5, 1:20 pm, Adrian Cuthbertson adrian.cuthbert...@gmail.com wrote: (dosync (alter rms assoc-in [:key1 :key2 :key3] foo)) {:key1 {:key2 {:key3 foo}}} I just found update-in, which is even better, as I want to update a set: user= (def gnu-rms (ref {:key1 {:key2 {:key3 #{))

Re: Clojure only (in-memory) database / locking granularity

2009-07-01 Thread Rowdy Rednose
On Jul 1, 12:02 am, Chouser chou...@gmail.com wrote: You could wrap a Ref around every value, if you chose, to allow independent changes to different rows at the same time -- though this would not help when inserting rows. I guess having millions of Refs would not perform too well. Plus you

Re: Clojure only (in-memory) database / locking granularity

2009-07-01 Thread Rowdy Rednose
that... :) On Jul 1, 9:18 pm, Matt Culbreth mattculbr...@gmail.com wrote: On Jul 1, 8:02 am, Rowdy Rednose rowdy.redn...@gmx.net wrote: But it looks like I have to implement that myself - which is not a complaint, but I'm trying to estimate the amount of work necessary. But I guess I could just

Clojure only (in-memory) database / locking granularity

2009-06-30 Thread Rowdy Rednose
Would it be easy to implement an in-memory database in clojure that allows concurrent access? It sounds pretty easy, as most of the features are already provided by Clojure. I'm not sure though about the locking granularity. Of course you don't pessimistically lock in Clojure, but if you have a

Re: Question regarding example in Stuart Halloway's book (page 120)

2009-06-26 Thread Rowdy Rednose
I find the pdf actually pretty useful as a quick reference until I'm familiar with all the function names. There's however a small mistake on page 28. The 'd' doesn't belong there in the 2nd line of this example: (let [{a :a, b :b, c :c, :as m :or {a 2 b 3}} {:a 5 :c 6}] [a b c d m]) - [5 3 6

why is io! not enforced?

2009-06-20 Thread Rowdy Rednose
Was it a deliberate decision to make io! optional or is this by accident? I know it came in later I guess it's hard to do considering all access to Java would have to be categorized, which is impossible, I guess. It just seems like Clojure loses a lot by not guaranteeing side-effect- freeness,

Re: Apply a function to some values in a map

2009-06-18 Thread Rowdy Rednose
18, 3:21 pm, Rowdy Rednose rowdy.redn...@gmx.net wrote: Say I've got this map of keys to integer values: (def m {:one 0 :two 0 :three 0}) I want to write a function inc-values-in-map that takes such a map, a collection of keys and will increment those keys' values in the map. In other

Re: Apply a function to some values in a map

2009-06-18 Thread Rowdy Rednose
] (assoc m k (f (m k default map keys))) On Jun 18, 5:06 pm, Rowdy Rednose rowdy.redn...@gmx.net wrote: Thanks for the ideas guys I especially like the reduce/assoc approach. It's the one most clear to me so far and also the most efficient one, I guess, as it does only one assoc per changed key

Re: Apply a function to some values in a map

2009-06-18 Thread Rowdy Rednose
Interesting. It's a bit different from what I currently need, but I'm currently looking at the sources. I wondered why you define this: (defn list-to-map [ params] (apply hash-map (reduce concat (partition 2 params Couldn't you just use the plain hash-map function? user= (=

Apply a function to some values in a map

2009-06-17 Thread Rowdy Rednose
Say I've got this map of keys to integer values: (def m {:one 0 :two 0 :three 0}) I want to write a function inc-values-in-map that takes such a map, a collection of keys and will increment those keys' values in the map. In other words, calling (inc-values-in-map m [:one :three]) should

Re: parallel iteration

2009-03-24 Thread Rowdy Rednose
Thanks guys, these solutions look much better already. But do I always have to have these 2 steps * merge collection * split collection It feels a bit inefficient, I don't know if it actually is, though. On Mar 24, 8:05 pm, Phlex ph...@telenet.be wrote: On 24/03/2009 1:06, pmf wrote: On

Re: parallel iteration

2009-03-24 Thread Rowdy Rednose
Thanks all, microbenchmarking shows that a simple (time (doseq [[a b] (map vector list-a list-b)])) is ~50% faster on my system than (def par-doseq-fn (comp dorun map)) (defmacro par-doseq [bindings body] (let [bindings (partition 2 bindings)] `(par-doseq-fn (fn ~(vec (map first

parallel iteration

2009-03-23 Thread Rowdy Rednose
Hi group, say I have 2 sequences (def seq-a '(a1 a2 a3)) (def seq-b '(b1 b2 b3)) and want to iterate over them in parallel, like this (par-doseq [a seq-a b seq-b] (prn a b)) which should print a1 b1 a2 b2 a3 b3 The way I do it currently is using map (and last to fight lazyness): (last

Anyone tried to create a dynamic TableModel (or ListModel)?

2009-02-20 Thread Rowdy Rednose
All the clojure swing examples I've seen so far use JTables in a static way, i.e. the data is not programmatically modified once the table got created. Has anyone actually tried to implement a TableModel that is backed by a clojure Vector/Map and fires events to TableModelListeners when the

Re: Anyone tried to create a dynamic TableModel (or ListModel)?

2009-02-20 Thread Rowdy Rednose
.  You could then do something like  (map = old_vec new_vec) And then look for false results in the array and send a notification to Swing. On Fri, Feb 20, 2009 at 5:40 AM, Rowdy Rednose rowdy.redn...@gmx.netwrote: All the clojure swing examples I've seen so far use JTables in a static way

Have get calculate not-found value only if key does not exist

2009-02-20 Thread Rowdy Rednose
What is the easiest way to make clojure's get function evaluate the value for 'not-found' only if the key is not found? Do I have to write a macro like (defmacro my-get [map key not-found] `(if (contains? ~map ~key) (get ~map ~key) ~not-found))

Re: Stumped - Java hangs when using Swing in Slime

2008-12-30 Thread Rowdy Rednose
as long as it is not realized yet... Still, this definitely is a doable workaround. Thanks! -Luke On Dec 29, 9:17 pm, Rowdy Rednose rowdy.redn...@gmx.net wrote: What if you run the Swing code in the Event Dispatch Thread? In other words, does this: (. javax.swing.SwingUtilities

Re: Stumped - Java hangs when using Swing in Slime

2008-12-30 Thread Rowdy Rednose
wrote: Wow... that actually fixes it. Still a minor problem, since according to the Sun website, it is legal to create a Swing object in another thread as long as long as it is not realized yet... Still, this definitely is a doable workaround. Thanks! -Luke On Dec 29, 9:17 pm, Rowdy Rednose

Re: Stumped - Java hangs when using Swing in Slime

2008-12-29 Thread Rowdy Rednose
What if you run the Swing code in the Event Dispatch Thread? In other words, does this: (. javax.swing.SwingUtilities (invokeAndWait #(. javax.swing.JOptionPane (showMessageDialog nil Hello World or (. javax.swing.SwingUtilities (invokeLater #(. javax.swing.JOptionPane (showMessageDialog

Re: Listen on changes to refs

2008-12-19 Thread Rowdy Rednose
land in an easy manner? On 16 Dez., 20:58, Rowdy Rednose rowdy.redn...@gmx.net wrote: Can I listen on changes done to refs? Let's say in a scenario like that onhttp://en.wikibooks.org/wiki/Clojure_Programming#Mutation_Facilities could I add a facility that allows the registration of listeners

Re: Listen on changes to refs

2008-12-19 Thread Rowdy Rednose
it will probably not be in 1.0 On 20 Dez., 11:30, Chouser chou...@gmail.com wrote: On Fri, Dec 19, 2008 at 8:35 PM, Rowdy Rednose rowdy.redn...@gmx.net wrote: Apart from being blasphemous - would it be a good idea to override clojure.lang.Ref in Java land? I want to create (children of?) refs

Re: Listen on changes to refs

2008-12-17 Thread Rowdy Rednose
The structure of the wikibook page has changed and that link doesn't work any more. This will: http://en.wikibooks.org/wiki/Clojure_Programming/Concepts#Mutation_Facilities On 16 Dez., 20:58, Rowdy Rednose rowdy.redn...@gmx.net wrote: Can I listen on changes done to refs? Let's say

Re: Listen on changes to refs

2008-12-17 Thread Rowdy Rednose
If Rich adds watchers for refs, would watchers be notified inside the transaction? I'm actually trying to get a notification after a transaction successfully finished. Although there might be use cases for firing events inside the transaction. On 17 Dez., 00:37, Stuart Sierra

Re: Listen on changes to refs

2008-12-16 Thread Rowdy Rednose
There must be a smarter way to achieve this than polling the collection for changes, I guess. On 16 Dez., 21:43, Dave Griffith dave.l.griff...@gmail.com wrote: Right now, you add listeners to agents, but not refs. IIRC, there was talk of adding listeners to refs to enable just the sort of