I'm trying clojure spec on a simple function that computes the "neighbours" of a (row,col) position in a square matrix. For example for the 4x4 matrix given below, the neighbours of cell (1,1) shall be: (0,1), (1,0), (1,2), (2,1). The neighbours of cell (4,3), which is not even in the matrix' range, shall be (3,3) etc.
The function's input is the size of the matrix and the (row,col) of the position of interest. The output is a collection of (row,col) of the neighbours. This collection can be empty if there are no neighbours.
This problem can be found in "The Joy of Clojure, 2nd editions, page 94; but this code is modified because the original was too compact for me. Then I tried to spec it and check the spec in the :pre and :post parts.
However, I don't get the :post part to work. When I run the test cases, I get:
java.lang.ClassCastException: java.lang.Boolean cannot be cast
to clojure.lang.IFn
What to change?
(require '[clojure.spec.alpha :as s]
'[clojure.test :as t])
; ===
; Specs
; ===
(s/def ::be-row-col
(s/coll-of integer? :count 2 :kind sequential?))
(s/def ::be-square-matrix-size
(s/and integer? #(<= 0 %)))
(s/def ::be-row-col-vector
(s/and (s/coll-of ::be-row-col) (s/int-in-range? 0 5 #(count %))))
; ===
; Function of interest
; ===
(defn neighbors [sqmsz rc]
{:pre [(s/valid? ::be-row-col rc)
(s/valid? ::be-square-matrix-size sqmsz)]
:post [(s/valid? ::be-row-col-vector %)]
}
(let [ cross [[-1 0] [1 0] [0 -1] [0 1]]
in-sq-matrix? (fn [x]
(and (<= 0 x) (< x sqmsz)))
in-sq-matrix-rc? (fn [rc]
(every? in-sq-matrix? rc))
add-two-rc (fn [rc1 rc2]
(vec (map + rc1 rc2)))
get-rc-neighbors (fn [rc]
(map (partial add-two-rc rc) cross)) ]
(filter in-sq-matrix-rc? (get-rc-neighbors rc))))
; ===
; Put a collection of [row col] into an expected form
; ===
; this is used to run the test code
(defn formify [rc-coll]
(let [ cmp (fn [rc1 rc2]
(let [ [r1 c1] rc1
[r2 c2] rc2 ]
(cond (< r1 r2) -1 ; sort by row
(> r1 r2) +1
(< c1 c2) -1 ; then by column
(> c1 c2) +1
true 0))) ]
(vec (sort cmp rc-coll))))
; ===
; Testing
; ===
(defn test-nb [ sqmsz rc expected txt ]
(do
(t/is (= (formify (neighbors sqmsz rc)) expected) txt)
))
(test-nb 0 [0 0] [] "Zero-size matrix, outside #1")
(test-nb 0 [1 1] [] "Zero-size matrix, outside #2")
(test-nb 1 [0 0] [] "One-size matrix, inside")
(test-nb 1 [1 0] [[0 0]] "One-size matrix, outside")
(test-nb 5 [0 0] [[0 1] [1 0]] "Testing top left")
(test-nb 5 [1 0] [[0 0] [1 1] [2 0]] "Testing left edge")
(test-nb 5 [1 1] [[0 1] [1 0] [1 2] [2 1]] "Testing middle #1")
(test-nb 5 [2 2] [[1 2] [2 1] [2 3] [3 2]] "Testing middle #2")
(test-nb 5 [3 3] [[2 3] [3 2] [3 4] [4 3]] "Testing middle #3")
(test-nb 5 [4 4] [[3 4] [4 3]] "Testing btm right")
(test-nb 5 [5 5] [] "Testing outside #1")
(test-nb 5 [5 4] [[4 4]] "Testing outside #2")
(test-nb 5 [4 3] [[3 3] [4 2] [4 4]] "Testing btm edge")
You're just missing the # prefix to make your anonymous function in the :post condition. The post condition needs to be a function that can take the output of the subject function's invocation.
:post [#(s/valid? ::be-row-col-vector %)]
Could also be rewritten as:
:post [(fn [o] (s/valid? ::be-row-col-vector o))]
But depending on your use case, you may want to look into function specs and instrument as an alternative to :pre and :post conditions. I wrote more examples here.
Related
If I have a vector [[[1 2 3] [4 5 6] [7 8 9]] [[10 11] [12 13]] [[14] [15]]]
How can I return the positions of each element in the vector?
For example 1 has index [0 0 0], 2 has index [0 0 1], etc
I want something like
(some-fn [[[1 2 3] [4 5 6] [7 8 9]] [[10 11] [12 13]] [[14] [15]]] 1)
=> [0 0 0]
I know that if I have a vector [1 2 3 4], I can do (.indexOf [1 2 3 4] 1) => 0 but how can I extend this to vectors within vectors.
Thanks
and one more solution with zippers:
(require '[clojure.zip :as z])
(defn find-in-vec [x data]
(loop [curr (z/vector-zip data)]
(cond (z/end? curr) nil
(= x (z/node curr)) (let [path (rseq (conj (z/path curr) x))]
(reverse (map #(.indexOf %2 %1) path (rest path))))
:else (recur (z/next curr)))))
user> (find-in-vec 11 data)
(1 0 1)
user> (find-in-vec 12 data)
(1 1 0)
user> (find-in-vec 18 data)
nil
user> (find-in-vec 8 data)
(0 2 1)
the idea is to make a depth-first search for an item, and then reconstruct a path to it, indexing it.
Maybe something like this.
Unlike Asthor's answer it works for any nesting depth (until it runs out of stack). Their answer will give the indices of all items that match, while mine will return the first one. Which one you want depends on the specific use-case.
(defn indexed [coll]
(map-indexed vector coll))
(defn nested-index-of [coll target]
(letfn [(step [indices coll]
(reduce (fn [_ [i x]]
(if (sequential? x)
(when-let [result (step (conj indices i) x)]
(reduced result))
(when (= x target)
(reduced (conj indices i)))))
nil, (indexed coll)))]
(step [] coll)))
(def x [[[1 2 3] [4 5 6] [7 8 9]] [[10 11] [12 13]] [[14] [15]]])
(nested-index-of x 2) ;=> [0 0 1]
(nested-index-of x 15) ;=> [2 1 0]
Edit: Target never changes, so the inner step fn doesn't need it as an argument.
Edit 2: Cause I'm procrastinating here, and recursion is a nice puzzle, maybe you wanted the indices of all matches.
You can tweak my first function slightly to carry around an accumulator.
(defn nested-indices-of [coll target]
(letfn [(step [indices acc coll]
(reduce (fn [acc [i x]]
(if (sequential? x)
(step (conj indices i) acc x)
(if (= x target)
(conj acc (conj indices i))
acc)))
acc, (indexed coll)))]
(step [] [] coll)))
(def y [[[1 2 3] [4 5 6] [7 8 9]] [[10 11] [12 13]] [[14] [15 [16 17 4]]]])
(nested-indices-of y 4) ;=> [[0 1 0] [2 1 1 2]]
Vectors within vectors are no different to ints within vectors:
(.indexOf [[[1 2 3] [4 5 6] [7 8 9]] [[10 11] [12 13]] [[14] [15]]] [[14] [15]])
;;=> 2
The above might be a bit difficult to read, but [[14] [15]] is the third element.
Something like
(defn indexer [vec number]
(for [[x set1] (map-indexed vector vec)
[y set2] (map-indexed vector set1)
[z val] (map-indexed vector set2)
:when (= number val)]
[x y z]))
Written directly into here so not tested. Giving more context on what this would be used for might make it easier to give a good answer as this feels like something you shouldn't end up doing in Clojure.
You can also try and flatten the vectors in some way
An other solution to find the path of every occurrences of a given number.
Usually with functional programming you can go for broader, general, elegant, bite size solution. You will always be able to optimize using language constructs or techniques as you need (tail recursion, use of accumulator, use of lazy-seq, etc)
(defn indexes-of-value [v coll]
(into []
(comp (map-indexed #(if (== v %2) %1))
(remove nil?))
coll))
(defn coord' [v path coll]
(cond
;; node is a leaf: empty or coll of numbers
(or (empty? coll)
(number? (first coll)))
(when-let [indexes (seq (indexes-of-value v coll))]
(map #(conj path %) indexes))
;; node is branch: a coll of colls
(coll? (first coll))
(seq (sequence (comp (map-indexed vector)
(mapcat #(coord' v (conj path (first %)) (second %))))
coll))))
(defn coords [v coll] (coord' v [] coll))
Execution examples:
(def coll [[2 1] [] [7 8 9] [[] [1 2 2 3 2]]])
(coords 2 coll)
=> ([0 0] [3 1 1] [3 1 2] [3 1 4])
As a bonus you can write a function to test if paths are all valid:
(defn valid-coords? [v coll coords]
(->> coords
(map #(get-in coll %))
(remove #(== v %))
empty?))
and try the solution with input generated with clojure.spec:
(s/def ::leaf-vec (s/coll-of nat-int? :kind vector?))
(s/def ::branch-vec (s/or :branch (s/coll-of ::branch-vec :kind vector?
:min-count 1)
:leaf ::leaf-vec))
(let [v 1
coll (first (gen/sample (s/gen ::branch-vec) 1))
res (coords v coll)]
(println "generated coll: " coll)
(if-not (valid-coords? v coll res)
(println "Error:" res)
:ok))
Here is a function that can recursively search for a target value, keeping track of the indexes as it goes:
(ns tst.clj.core
(:use clj.core tupelo.test)
(:require [tupelo.core :as t] ))
(t/refer-tupelo)
(defn index-impl
[idxs data tgt]
(apply glue
(for [[idx val] (zip (range (count data)) data)]
(let [idxs-curr (append idxs idx)]
(if (sequential? val)
(index-impl idxs-curr val tgt)
(if (= val tgt)
[{:idxs idxs-curr :val val}]
[nil]))))))
(defn index [data tgt]
(keep-if not-nil? (index-impl [] data tgt)))
(dotest
(let [data-1 [1 2 3]
data-2 [[1 2 3]
[10 11]
[]]
data-3 [[[1 2 3]
[4 5 6]
[7 8 9]]
[[10 11]
[12 13]]
[[20]
[21]]
[[30]]
[[]]]
]
(spyx (index data-1 2))
(spyx (index data-2 10))
(spyx (index data-3 13))
(spyx (index data-3 21))
(spyx (index data-3 99))
))
with results:
(index data-1 2) => [{:idxs [1], :val 2}]
(index data-2 10) => [{:idxs [1 0], :val 10}]
(index data-3 13) => [{:idxs [1 1 1], :val 13}]
(index data-3 21) => [{:idxs [2 1 0], :val 21}]
(index data-3 99) => []
If we add repeated values we get the following:
data-4 [[[1 2 3]
[4 5 6]
[7 8 9]]
[[10 11]
[12 2]]
[[20]
[21]]
[[30]]
[[2]]]
(index data-4 2) => [{:idxs [0 0 1], :val 2}
{:idxs [1 1 1], :val 2}
{:idxs [4 0 0], :val 2}]
I have a function
(defn x [w]
(let [[w1 w2 w3] w]
(println w1)
(println w2)
(println w3)))
If I call the function
(x [[1 1] [2 2] [3 3]])
=> [1 1]
[2 2]
[3 3]
which is what I expect
Is there a way to generalise this? In this case I knew that w was a vector containing 3 vectors so I know to have [w1 w2 w3] If w was then a vector of 4 vectors, the last vector would not be set to anything
What I want is where w is a vector of n vectors and then in the let of the function set them to [w1 w2 w3 ... wn]? (note doesn't necessarily have to be w1, w2, ... wn)
The println are just there for debugging so not that important for the function
Any help would be much appreciated.
(defn x [ws]
(dorun (map println ws)))
For example,
(x [[1 1] [2 2] [3 3]])
[1 1]
[2 2]
[3 3]
=> nil
The map applies println to each of the ws in turn, returning
the nil results as a sequence, on demand (lazily).
The dorun demands the whole sequence, discarding it as it goes,
returning nil.
If you want to see the sequence, replace dorun with doall:
(defn x [ws]
(doall (map println ws)))
=> (x [[1 1] [2 2] [3 3]])
[1 1]
[2 2]
[3 3]
=> (nil nil nil)
A more concise alternative to the former is
(defn x [ws]
(doseq [w ws] (println w)))
... and to the latter is
(defn x [ws]
(for [w ws] (println w)))
I understand the following snippet of code and its corresponding output
(let [ [x y] (map list [1 2] [3 4])] (prn x) (prn y))
(1 3)
(2 4)
nil
Now the following output confuses me:
(doseq [ [x y] (map list [1 2] [3 4])] (prn x y))
1 3
2 4
nil
I think in the above snippet x will get bound to [1 3] and y will get bound to [2 4] so the output should be"
1 2
3 4
nil
The binding is pulling out the individual elements inside the larger single element in the nesting. The map creates a list ((1 3) (2 4) so the element 1 3 is first and thus that is what the doseq outputs: this is "destructuring" and the x and y are both bound from inside a single element of the list. Thus x and y are 1 and 3, and then 2 and 4.
Note also that this is the same binding that occurs in a for and the destructuring works for all sequence types.
Let's isolate the source of your confusion.
(map list [1 2] [3 4])
evaluates to
((1 3) (2 4))
So your first example
(let [[x y] (map list [1 2] [3 4])] (prn x) (prn y))
... is equivalent to
(let [[x y] [[1 3] [2 4]]] (prn x) (prn y))
... or, printing slightly differently
(let [[x y] [[1 3] [2 4]]] (prn [x y]))
... which simplifies to
(let [z [[1 3] [2 4]]] (prn z))
... producing, as expected,
; [[1 3] [2 4]]
; nil
So far, so good.
If we boil down the confusing example,
(doseq [ [x y] (map list [1 2] [3 4])] (prn x y))
... in the same way, we get, taking the liberty of printing each z whole,
(doseq [z [[1 3] [2 4]]] (prn z))
which fairly clearly produces the observed order:
[1 3]
[2 4]
nil
The difference is that the doseq binds z to each successive vector in [[1 3] [2 4]], so we don't see an enclosing [ ... ], whereas the let binds z once to the whole thing.
So I have a problem I'm trying to solve in clojure. It's a bit of a programming exercise I try to do in many languages to "learn" the language..
This is my first immutable-functional language and I'm having some challenges.
The exercise reads from a file a number of "instructions" in the form of
PLACE X, Y, CARDINAL_DIRECTION (e.g. PLACE 0,0,NORTH)
And then can move forward one space using MOVE
The player can rotate left and right using LEFT, RIGHT.
finally REPORT will print out the x, y, and cardinal direction of the player
The challenge I have at the moment is I'm not sure where to store the players position. In a class based language I'd have a player class that holds the co-ordinates.
Below is my solution so far
(ns toyrobot.core
(use clojure.java.io))
(defstruct player :x :y :face)
(def ^:dyanmic robot nil)
(defn file->vec
"Read in a file from the resources directory"
[input]
(with-open [rdr (reader input)]
(into [] (line-seq rdr))))
(defn parse-command [line]
"Parse command"
(clojure.string/split line #"\s|,"))
(defn on-the-board? [co-ordinates]
"Check if the x & y co-ordinates are on the board"
(let [x (Integer/parseInt (first co-ordinates))
y (Integer/parseInt (second co-ordinates))]
(if (and (>= x 0) (>= y 0) (<= x 5) (<= y 5))
true
false)))
(defn place [co-ordinates]
"Place the robot at the co-ordinates"
(if (on-the-board? co-ordinates)
co-ordinates
nil))
(defn -main []
(doseq [command (file->vec "resources/input.txt")]
(case (clojure.string/upper-case (first (parse-command command)))
"PLACE" (place (rest (parse-command command)))
"MOVE" (println "move")
"LEFT" (println "left")
"RIGHT" (println "right")
"REPORT" (println "report")
"")))
Thumbnail's answer is good, and I didn't really want to distract from it, but your little exercise also gives opportunity to play with Clojure's lisp-nature that I couldn't resist.
;Agree that unit vectors are more convenient to work with than cardinal directions
(def north [0 1])
(def east [1 0])
(def south [0 -1])
(def west [-1 0])
;Just for a taste of macros
(defmacro name-value-map [& xs]
`(zipmap ~(mapv name xs) (vector ~#xs)))
(def direction->heading (name-value-map north east south west))
(def heading->direction (clojure.set/map-invert direction->heading))
;Robot commands just return an updated structure
(defn left [robot]
(update-in robot [:heading] (fn [[dx dy]] [(- 0 dy) dx])))
(defn right [robot]
(update-in robot [:heading] (fn [[dx dy]] [dy (- 0 dx)])))
(defn move [robot]
(update-in robot [:position] (partial mapv + (:heading robot))))
;Report and return unchanged
(defn report [robot]
(println "Robot at" (:position robot)
"facing" (heading->direction (:heading robot)))
robot)
;Create
(defn place [x y heading]
{:position [x y] :heading heading})
Now with those in place you already pretty much have your language in a mini-DSL via the threading macro
(-> (place 3, 3, north) report move report left report move report)
;Printed Output:
;Robot at [3 3] facing north
;Robot at [3 4] facing north
;Robot at [3 4] facing west
;Robot at [2 4] facing west
;Return Value:
;{:position [2 4], :heading [-1 0]}
Indeed, if you had a file that you trust with contents
(def sample-file-contents "(place 3, 3, north) move left move")
You could read in the data as a form
user=> (read-string (str "(" sample-file-contents ")"))
((place 3 3 north) move left move)
Interleave some reporting (at the REPL *1 is the previous value)
user=> (interleave *1 (repeat 'report))
((place 3 3 north) report move report left report move report)
Tack on the threading macro
user=> (cons '-> *1)
(-> (place 3 3 north) report move report left report move report)
And evaluate for the same output as above
user=> (eval *1)
;Printed Output
;Robot at [3 3] facing north
;Robot at [3 4] facing north
;Robot at [3 4] facing west
;Robot at [2 4] facing west
;Return Value:
;{:position [2 4], :heading [-1 0]}
To set this up a bit more properly doesn't require too much extra effort with a good parsing library
(require '[instaparse.core :as insta])
(require '[clojure.tools.reader.edn :as edn])
(def parse (insta/parser "
<commands> = place (ws command)*
<command> = place | unary-command
place = <'place '> location <', '> direction
unary-command = 'left' | 'right' | 'move' | 'report'
number = #'[0-9]+'
location = number <', '> number
direction = 'north' | 'east' | 'west' | 'south'
<ws> = <#'\\s+'> "))
(defn transform [parse-tree]
(insta/transform
{:number edn/read-string
:location vector
:direction direction->heading
:place (fn [[x y] heading] #(place x y heading))
:unary-command (comp resolve symbol)}
parse-tree))
(defn run-commands [[init-command & more-commands]]
(reduce (fn [robot command] (command robot)) (init-command) more-commands))
And now that we've dropped the requirement for parenthesis (and with some sample newlines included), redefine the sample file
(def sample-file-contents "place 3, 3, north report
move report
left report
move report")
And, again at the REPL to show intermediate results
user=> (parse sample-file-contents)
([:place [:location [:number "3"] [:number "3"]] [:direction "north"]] [:unary-command "report"]
[:unary-command "move"] [:unary-command "report"]
[:unary-command "left"] [:unary-command "report"]
[:unary-command "move"] [:unary-command "report"])
user=> (transform *1)
(#< clojure.lang.AFunction$1#289f6ae> #'user/report
#'user/move #'user/report
#'user/left #'user/report
#'user/move #'user/report)
user=> (run-commands *1)
;Printed Output:
;Robot at [3 3] facing north
;Robot at [3 4] facing north
;Robot at [3 4] facing west
;Robot at [2 4] facing west
;Return Value:
;{:position [2 4], :heading [-1 0]}
You are well in control of parsing the instruction file, so let's concentrate on the internal computation.
Clojure structs are essentially immutable, so that the history of the robot is a sequence of distinct robot state objects instead of a sequence of states of a single robot object. So it is natural to represent the robot commands as functions that, given a robot state, return another robot state.
We need
commands to turn-left, turn-right, and move-forward and
some way of creating a robot state in a given place and pointing in a
given direction.
How should we represent robot state? Let's keep it simple.
Places are simply pairs of numbers, x before y. So that the origin is
[0 0] and [5 0] is 5 along the x axis.
Directions are movement vectors, so that [1 0] stands for
east and [0 -1] stands for south. This makes movement easy to do.
The robot state is a record (easier to handle than a struct) with
:place and :direction fields.
Let's first deal with functions to change direction left or right.
We start by defining a helper function to generate a cycle from a sequence:
(defn cycle-map [fs]
"maps each element in the finite sequence fs to the next, and the last to the first"
(assoc (zipmap fs (next fs)) (last fs) (first fs)))
... which we use to generate a map that takes each direction to the one to its left:
(def left-of (cycle-map (list [0 1] [-1 0] [0 -1] [1 0])))
We can invert this to yield a map that takes each direction to the one to its right:
(def right-of (clojure.set/map-invert left-of))
We will use these maps as functions to modify robot states (strictly speaking, to return modified robot states).
Now we define our Robot state:
(defrecord Robot [place direction])
... and some of the functions we need to manipulate it:
(defn turn-left [robot] (assoc robot :direction (left-of (:direction robot))))
defn move-forward [robot] (assoc robot :place (mapv + (:place robot) (:direction robot))))
Let's try them:
toyrobot.core=> (Robot. [0 0] [1 0])
{:place [0 0], :direction [1 0]}
toyrobot.core=> (turn-left (Robot. [0 0] [1 0]))
{:place [0 0], :direction [0 1]}
toyrobot.core=> (move-forward (Robot. [0 0] [1 0]))
{:place [1 0], :direction [1 0]}
toyrobot.core=> (take 10 (iterate move-forward (Robot. [0 0] [1 0])))
({:place [0 0], :direction [1 0]}
{:place [1 0], :direction [1 0]}
{:place [2 0], :direction [1 0]}
{:place [3 0], :direction [1 0]}
{:place [4 0], :direction [1 0]}
{:place [5 0], :direction [1 0]}
{:place [6 0], :direction [1 0]}
{:place [7 0], :direction [1 0]}
{:place [8 0], :direction [1 0]}
{:place [9 0], :direction [1 0]})
It works!
I'm trying to write a function adjacents that returns a vector of a sequence's adjacent pairs. So (adjacents [1 2 3]) would return [[1 2] [2 3]].
(defn adjacents [s]
(loop [[a b :as remaining] s
acc []]
(if (empty? b)
acc
(recur (rest remaining) (conj acc (vector a b))))))
My current implementation works for sequences of strings but with integers or characters the REPL outputs this error:
IllegalArgumentException Don't know how to create ISeq from: java.lang.Long clojure.lang.RT.seqFrom (RT.java:494)
The problem here is in the first evaluation loop of (adjacents [1 2 3]), a is bound to 1 and b to 2. Then you ask if b is empty?. But empty? works on sequences and b is not a sequence, it is a Long, namely 2. The predicate you could use for this case here is nil?:
user=> (defn adjacents [s]
#_=> (loop [[a b :as remaining] s acc []]
#_=> (if (nil? b)
#_=> acc
#_=> (recur (rest remaining) (conj acc (vector a b))))))
#'user/adjacents
user=> (adjacents [1 2 3 4 5])
[[1 2] [2 3] [3 4] [4 5]]
But, as #amalloy points out, this may fail to give the desired result if you have legitimate nils in your data:
user=> (adjacents [1 2 nil 4 5])
[[1 2]]
See his comment for suggested implementation using lists.
Note that Clojure's partition can be used to do this work without the perils of defining your own:
user=> (partition 2 1 [1 2 3 4 5])
((1 2) (2 3) (3 4) (4 5))
user=> (partition 2 1 [1 2 nil 4 5])
((1 2) (2 nil) (nil 4) (4 5))
Here is my short answer. Everything becomes a vector, but it works for all sequences.
(defn adjacent-pairs [s]
{:pre [(sequential? s)]}
(map vector (butlast s) (rest s)))
Testing:
user=> (defn adjacent-pairs [s] (map vector (butlast s) (rest s)))
#'user/adjacent-pairs
user=> (adjacent-pairs '(1 2 3 4 5 6))
([1 2] [2 3] [3 4] [4 5] [5 6])
user=> (adjacent-pairs [1 2 3 4 5 6])
([1 2] [2 3] [3 4] [4 5] [5 6])
user=>
This answer is probably less efficient than the one using partition above, however.