On Sunday, September 4, 2011 12:21:23 PM UTC-4, HamsterofDeath wrote:
>
> * in the last loop where i am just printing out what i want to do, i
> need something like "foldLeft" (from scala). how do i fold in clojure?
>
doseq is the way to iterate over a collection and perform side effects:
(let [moves [[0 0 p1] [1 0 p1] [2 0 p1]]]
(doseq [[x y player] moves]
(println "player" player "makes move at" x "/" y)))
reduce performs a left fold in Clojure.
> * is there no predefined "updated" function?
>
Not for sequential data structures. It exists for associative structures
like maps and vectors:
(assoc [:x :x :x] 1 :o)
=> [:x :o :x]
Some other comments:
- Nested defns are not good. You generally only want defs at the top-level.
To create lexically-scoped helper functions within other functions, use letor
letfn.
- Development (and debugging) is usually easier when your program consists
of small, testable functions. Your winner function is a bit hard to
decipher. Consider factoring out functions that check for runs of a value
across rows/columns.
- I recommend reading up on Clojure's basic data structures (list, vector,
hash-map, set) and get familiar with how to build and manipulate them:
http://clojure.org/data_structures
http://blip.tv/clojure/clojure-data-structures-part-1-714056
http://java.ociweb.com/mark/clojure/article.html
http://4clojure.com/problems
Justin
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en