Re: side effects in fn passed to filter

2010-02-18 Thread Rowdy Rednose
gt; better idea of what is going on. > > Sean > > On Feb 18, 10:34 am, Rowdy Rednose 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&#

Re: side effects in fn passed to filter

2010-02-18 Thread Rowdy Rednose
ers--he > only wanted to remove *adjacent* duplicates. > > That said, core/distinct 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 wrote

Re: side effects in fn passed to filter

2010-02-18 Thread Rowdy Rednose
, 11:04 pm, Rowdy Rednose 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 uniqu

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" (le

Re: headMap / tailMap / subMap

2010-01-31 Thread Rowdy Rednose
te: > On Sat, Jan 30, 2010 at 7:31 PM, Rowdy Rednose 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 wrote: > >> If you can live with an O(n) operation, take/drop-with will do

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 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 wrote: > > > How would I d

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 clojur

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

Re: On Lisp => Clojure

2009-07-18 Thread Rowdy Rednose
On Jul 19, 12:53 pm, Richard Newman 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: (defn db-pu

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

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 retur

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

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

Re: The snake again - Programming Clojure book

2009-07-10 Thread Rowdy Rednose
Awesome! Works great! (After fixing the typo in "SwingUtilites". that is :) --~--~-~--~~~---~--~~ 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

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 Go

Re: The snake again - Programming Clojure book

2009-07-10 Thread Rowdy Rednose
But can't I somehow refer to the original dosync from my new dosync macro? On Jul 10, 11:52 pm, Rowdy Rednose wrote: > This did the trick: > > (ns clojure.core) > (defmacro dosync [& body] >   `(do >      (assert (not (javax.swing.SwingUtilities/isEventDispatchThrea

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 pr

Re: The snake again - Programming Clojure book

2009-07-10 Thread Rowdy Rednose
On Jul 10, 10:28 pm, Stuart Halloway 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 writing Swing code, at least

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 n

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
07.2009 um 07:27 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... > > (dosy

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 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 #{)) #'user/gnu-rms user=> (dosync

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 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 want to have a loos

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
Much better! Thanks. On Jul 5, 1:20 pm, Adrian Cuthbertson wrote: > You could use assoc-in... > > (def rms (ref {:key1 {:key2 {:key3 #{)) > > (dosync (alter rms assoc-in [:key1 :key2 :key3] "foo")) > {:key1 {:key2 {:key3 "foo"}}} > > Rgds, Adria

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

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

2009-07-01 Thread Rowdy Rednose
ctional Relational Programming", I'll probably immediately dump my code and just use that... :) On Jul 1, 9:18 pm, Matt Culbreth wrote: > On Jul 1, 8:02 am, Rowdy Rednose wrote: > > > > > But it looks like I have to implement that myself - which is not a > > compl

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

2009-07-01 Thread Rowdy Rednose
On Jul 1, 12:02 am, Chouser 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 don't need tha

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

Re: Apply a function to some values in a map

2009-06-18 Thread Rowdy Rednose
You are absolutely right about the names. Unfortunately all the good ones are already taken by Rich. ;) --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@goog

Re: Apply a function to some values in a map

2009-06-18 Thread Rowdy Rednose
p is > to decode the specific key it is assigned to. > > (def decoder-map {0 "Awesome" 1 "Not Awesome"}) > > ;This will decode the key :a > user=> (deftrans decoder :a decoder-map) > #'user/decoder > > user=> (decoder test-map) > {:a &q

Re: Apply a function to some values in a map

2009-06-18 Thread Rowdy Rednose
e (fn [m k] (assoc m k (f (m k default map keys))) On Jun 18, 5:06 pm, Rowdy Rednose 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

Re: Apply a function to some values in a map

2009-06-18 Thread Rowdy Rednose
inc 0) map keys)) > > But its longer than yours :) > > On Jun 18, 3:21 pm, Rowdy Rednose 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"

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 ret

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 b

Re: parallel iteration

2009-03-24 Thread Rowdy Rednose
On Mar 24, 12:01 am, Rowdy Rednose  wrote: > > >> Hi group, > > >> say I have 2 sequences > > >> (def seq-a '("a1" "a2" "a3")) > >> (def seq-b '("b1" "b2" "b3")) > > >&g

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

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: Anyone tried to create a dynamic TableModel (or ListModel)?

2009-02-20 Thread Rowdy Rednose
s.  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 wrote: > > > > > All the clojure swing examples I've seen s

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 under

Re: Stumped - Java hangs when using Swing in Slime

2008-12-30 Thread Rowdy Rednose
, levand 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!

Re: Stumped - Java hangs when using Swing in Slime

2008-12-30 Thread Rowdy Rednose
it is not "realized" yet... > > Still, this definitely is a doable workaround. Thanks! > > -Luke > > On Dec 29, 9:17 pm, Rowdy Rednose wrote: > > > What if you run the Swing code in the Event Dispatch Thread? > > > In other words, does t

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
ry&cells=tiles So it will probably not be in 1.0 On 20 Dez., 11:30, Chouser wrote: > On Fri, Dec 19, 2008 at 8:35 PM, Rowdy Rednose wrote: > > > Apart from being blasphemous - would it be a good idea to override > > clojure.lang.Ref in Java land? > > > I want to c

Re: Listen on changes to refs

2008-12-19 Thread Rowdy Rednose
ojure) the relevant functions like ref-set etc. Or is there anything else I missed? On 20 Dez., 10:35, Rowdy Rednose 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 in clojure that sen

Re: Listen on changes to refs

2008-12-19 Thread Rowdy Rednose
elop this in Clojure land in an easy manner? On 16 Dez., 20:58, Rowdy Rednose 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 reg

Re: Listen on changes to refs

2008-12-17 Thread Rowdy Rednose
That's great news. Thanks Dave! Re-reading http://clojure.org/agents I found it mentioned somewhere at the end, almost like a small, unimportant footnote, easy to overlook... Now before I start working on a small and handy framework to handle stuff similar to what's called biz objects / biz mode

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 wrote: > On Dec 16

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 wrote: > Can I listen on changes done to refs? > > Let's say in a

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 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 > reactive programming you

Listen on changes to refs

2008-12-16 Thread Rowdy Rednose
Can I listen on changes done to refs? Let's say in a scenario like that on http://en.wikibooks.org/wiki/Clojure_Programming#Mutation_Facilities could I add a facility that allows the registration of listeners that get called on certain changes to the refs? For example I'd like to be notified on