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
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
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]
#{"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
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
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
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
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
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
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,
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
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
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
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é
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
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
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
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
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
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
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.
--~-
21 matches
Mail list logo