Re: Exercise: words frequency ranking

2008-12-25 Thread wwmorgan
A better implementation would split the different steps of the program into separate functions. This increases readability and testability of the source code, and encourages the reuse of code in new programs. I haven't tested this program, but hopefully you'll understand the general approach. Als

Re: How to reduce a list of booleans?

2009-01-15 Thread wwmorgan
Look at every?. The predicate true? returns true if its argument is the boolean literal true, and false otherwise. If, instead, you want to check for logical truth (false and nil are false, everything else is true), then use identity. As you can see, every? falls out at the first false result. us

Re: Is 'every?' broken?

2009-01-21 Thread wwmorgan
map is lazy, but to-array is not. The output you are seeing is the result of to-array needing to realize every element of its collection. every? only applies its predicate to as many elements of its collection as are necessary. See the following: user=> (every? print-arg [true false true]) [true]

Re: contains

2009-01-23 Thread wwmorgan
#{"aeiou"} is the set containing the String "aeiou". You want #{\a \e \i \o \u} On Jan 23, 4:04 pm, Mark Volkmann wrote: > On Fri, Jan 23, 2009 at 2:51 PM, Chouser wrote: > > > On Fri, Jan 23, 2009 at 3:47 PM, Mark Volkmann > > wrote: > > >> (contains? "aeiou" letter) > > >> but that doesn't w

Re: how to reason about two dimentional array(vector) like...

2009-02-04 Thread wwmorgan
Here's what I came up with: is it any clearer? (def board (vec (for [i (range dim)] (vec (for [j (range dim)] (ref (struct cell white))) On Feb 4, 11:30 pm, wubbie wrote: > Hi, > > Have some difficulties in reasoning about it. > From ants.clj, > (def boar

Re: A useful function?

2010-08-16 Thread wwmorgan
Note that if f is not referentially transparent, you can get different results depending on the order in which the collection is traversed, for unordered collections. See the API function group-by. It makes this behavior explicit by mapping each input argument to a vector of output arguments, in th

Re: A useful function?

2010-08-16 Thread wwmorgan
p class x)) {(:a) clojure.lang.PersistentVector} - Will Morgan On Aug 16, 5:52 pm, wwmorgan wrote: > Note that if f is not referentially transparent, you can get different > results depending on the order in which the collection is traversed, > for unordered collections. See the API function group-by. It makes &g

Re: What is the reason Lisp code is not written with closing parenthesis on new lines?

2010-08-18 Thread wwmorgan
The Incanter example is confusing for the same reason that the Leiningen example from the blog post is confusing, and I don't think paren style matters at all. The functions have grown over time, they're now too big, and they need to be refactored into several smaller functions. The paren style is

Re: multiplying lists

2010-08-19 Thread wwmorgan
user=> (def a [1 2 3]) #'user/a user=> (def b [[4 5 6] [7 8 9]]) #'user/b user=> (map #(map * a %) b) ((4 10 18) (7 16 27)) - Will Morgan On Aug 19, 7:56 pm, Glen Rubin wrote: > I want to multiply a list of n items by h lists of n items, so that > for example if i have list 'a' and 'b' > > (def

Re: WHILE macro

2008-09-10 Thread wwmorgan
The best solution using the API that I could come up with would be something like (doseq r (take-while identity (repeatedly #(side-effects with many args))) (pr r)) It's useful in the situation where 1. You have a function which can return different values when invoked with the same arguments,

Re: recur question

2008-10-17 Thread wwmorgan
I think Stewart's code was more for exemplary purposes, to demonstrate the use of recur in tail position. A more idiomatic approach might look like this: (defn factorial [n] (apply * (range 1 (inc n On Oct 17, 12:43 pm, Joel L <[EMAIL PROTECTED]> wrote: > This is interesting as I hit a sim

Re: Suggestions on handling state for a game...

2008-10-19 Thread wwmorgan
In the game I'm writing, state is held in a single var *state*. Actions are reified and represented by maps. So I have a single function (defmulti execute :Action) that operates on *state* and returns a new state object (state is immutable). So to execute a sequence of actions would look like this

Re: Newbie question on the use of functional hash maps

2008-10-19 Thread wwmorgan
If you're interested only in counting the number of unique words, then you don't even need a map. You can get by with a set, like this: (defn unique-words-in-file [file] (count (set (split-on-whitespace (slurp file) slurp reads file into a String object in memory. The hypothetical spli

Re: Currying for Clojure

2008-10-22 Thread wwmorgan
You can get most of the functionality you're looking for with partial (map (partial * 3) (range 10)) => (0 3 6 9 12 15 18 21 24 27) (map (partial apply max 0) (partition 3 1 (range -5 5))) => (0 0 0 0 1 2 3 4) On Oct 22, 5:40 pm, André Thieme <[EMAIL PROTECTED]> wrote: > On 22 Okt., 23:24, André

Patch: better exception from Script when user.clj is broken.

2008-10-27 Thread wwmorgan
I was in the process of porting our clojure code base to work with SVN head when I came across the following behavior. It turns out that the upgrade to head broke our user.clj. If user.clj is broken, and you invoke clojure.lang.Script from the command line (or from an ant build process, or you res

Re: macro questions, "can't embed unreadable..."

2008-10-31 Thread wwmorgan
It's possible but very ugly. You need to force the reader to make two passes over the code, using eval. Here's what I came up with: (defmacro declare-init [vecs] `(doseq v# ~(if (symbol? vecs) vecs (list 'quote vecs)) (eval (list 'def (first v#) (second v#) In use: user=> (decl

Re: quote + #^ bug?

2008-11-05 Thread wwmorgan
I don't know why you're getting this behavior, but you can do what you want by using with-meta instead of #^ user=> (meta (with-meta 'sss {:tag 'mmm})) {:tag mmm} On Nov 4, 8:24 pm, Stephen Wrobleski <[EMAIL PROTECTED]> wrote: > On rev 1086: > > user=> (meta `#^mmm sss) > {:tag user/mmm} > > use

Re: Nother N00B Question

2008-11-05 Thread wwmorgan
Peter, You might look at the loop special form: (defn game "Runs the game, returning the final state" [] (loop [state (initial-state)] (report state) (if (final? state) state (recur (compute-new-state state (read-line)) for your own definitions of initial

Re: Print compiled Clojure svn revision?

2008-11-07 Thread wwmorgan
As a matter of style, I would rather see this functionality in a global var than in a function. I think that's it's a more idiomatic place for it. user=> *version* "r1088" On Nov 7, 1:18 pm, "Graham Fawcett" <[EMAIL PROTECTED]> wrote: > On Fri, Nov 7, 2008 at 1:15 PM, Rich Hickey <[EMAIL PROTE

Re: "Can't create defs outside of current namespace"

2008-11-12 Thread wwmorgan
FWIW, there's also a hack without java calls, which Chouser referred to: (binding [*ns* (find-ns 'foo)] (eval '(def bar 3))) although you have to create the namespace yourself if it doesn't exist. On Nov 11, 3:39 pm, MikeM <[EMAIL PROTECTED]> wrote: > This may be a horrible hack, but you can

Re: writing binary values (bytes) to a file

2008-11-19 Thread wwmorgan
In general, Writers are for character data and OutputStreams are for binary data. If possible, create a FileOutputStream like this: (ns my.ns (:import (java.io FileOutputStream))) (def ofile (FileOutputStream. "/some/file/somewhere")) and use one of the write methods of FileOutputStream. --~-