why doesn't concat take effect here? - clojure

Here is the code:
(def city-nodes (atom nil))
(def city-edges (atom nil))
(def player-pos (ref nil))
(def visited-nodes (ref #{}))
(def player-status (atom :in-progress))
(def ^:dynamic *node-num* 8)
(def ^:dynamic *edge-num* 5)
(def ^:dynamic *worm-num* 3)
(def ^:dynamic *cop-odds* 4)
(defn filterb
[pred coll]
(let [rt (filter pred coll)]
(if (empty? rt)
nil
rt)))
(defn all-nodes []
(range 1 (inc *node-num*)))
(all-nodes)
(defn rand-node []
(inc (rand-int *node-num*)))
(defn edge-pair [x y]
(when-not (= x y)
[[x y] [y x]]))
(defn sample-2diff-nodes []
(let [[x y] [(rand-node) (rand-node)]]
(if (= x y)
(sample-2diff-nodes)
[x y])))
(edge-pair 1 1)
(take 4 (repeatedly #(sample-2diff-nodes)))
(def ^:dynamic t-edges (take 4 (repeatedly #(sample-2diff-nodes))))
(defn make-edge-vec []
(->> (repeatedly #(sample-2diff-nodes))
(take *edge-num*)
(apply concat)
set
vec))
(binding [t-edges (take 4 (repeatedly #(sample-2diff-nodes)))])
(prn t-edges)
(binding [t-edges (make-edge-vec)])
(prn t-edges)
(binding [t-edges (apply concat t-edges)])
(prn t-edges)
Results:
(binding [t-edges (take 4 (repeatedly #(sample-2diff-nodes)))])
(prn t-edges)
(binding [t-edges (make-edge-vec)])
(prn t-edges)
(binding [t-edges (apply concat t-edges)])
(prn t-edges)
([6 1] [1 2] [5 2] [3 1])
([6 1] [1 2] [5 2] [3 1])
([6 1] [1 2] [5 2] [3 1])
nil
I for the last prn command, I expected ( 6 1 1 2 5 2 3 1).

the closing ) of the binding call is in the wrong place:
(binding [t-edges (apply concat t-edges)])
(prn t-edges)
should be:
(binding [t-edges (apply concat t-edges)]
(prn t-edges))
Though in this case you almost certainly want to use a let expression instead

Related

How can I later get access to something created in a let

For example
(def inc-map (let [inum (atom 0)]
{:countup (fn[](swap! inum inc))
:get (fn[](#inum))}))
((inc-map :countup )) ;increase inside value
; ⇒ 1
((inc-map :get)) ;get current value
; ⇒ 1
Can I get access inum when I want to add more functions later?
E.g. I want to do this:
(def inc-map
(assoc inc-map :countdown (fn[] ???)))
How can I access inum at ????
it could be possible, if you expose one more function, say :update, enclosing the inum value.
(def inc-map (let [inum (atom 0)]
{:update (fn [f & args] (apply swap! inum f args))
:countup (fn [] (swap! inum inc))
:get (fn [] #inum)}))
user> (def inc-dec-map (assoc inc-map :countdown
(fn [] ((inc-map :update) dec))))
#'user/inc-dec-map
user> ((inc-dec-map :countup))
;;=> 1
user> ((inc-dec-map :countup))
;;=> 2
user> ((inc-dec-map :countdown))
;;=> 1
user> ((inc-dec-map :countdown))
;;=> 0
user> ((inc-dec-map :countdown))
;;=> -1
and then you can just seal it, dissoc'ing :update, (say if you make it publicly accessed to some other namespace.
otherwise you could provide an getter/updater to the ops object:
(def counter-ops (let [inum (atom 0)
ops (atom {:countup (fn [] (swap! inum inc))
:get (fn [] #inum)})]
(fn
;; get operations map snapshot
([] #ops)
;; get operation
([op] (-> ops deref op))
;; set operation
([op f & args] (swap! ops assoc op (fn [& args] (apply f inum args)))))))
user> (counter-ops :countdown (fn [inum] (swap! inum dec)))
user> (counter-ops :decrease-by (fn [inum n] (swap! inum - n)))
user> ((counter-ops :countdown))
;;=> -1
user> ((counter-ops :countdown))
;;=> -2
user> ((counter-ops :countup))
;;=> -1
user> ((counter-ops :countup))
;;=> 0
user> ((counter-ops :decrease-by) 10)
;;=> -10
seal it to be locked for any subsequent op additions:
user> (def counter-ops-sealed (counter-ops))
#'user/counter-ops-sealed
user> ((counter-ops-sealed :countup))
;;=> 2
user> ((counter-ops-sealed :countdown))
;;=> 1

Clojure binding of dynamic var not working as expected

From what I understand, setting a new binding on a dynamic var affects all functions called within that binding, and all functions called from those functions.
Why does the binding appear to be lost in the first example below?
(def ^:dynamic *out-dir* "/home/user")
(binding [*out-dir* "/home/dave"] (map #(str *out-dir* %) [1 2 3]))
; gives: ("/home/user1" "/home/user2" "/home/user3")
; expected: ("/home/dave1" "/home/dave2" "/home/dave3")
(binding [*out-dir* "/home/dave"] (conj (map #(str *out-dir* %) [1 2 3]) *out-dir*))
; gives: ("/home/dave" "/home/dave1" "/home/dave2" "/home/dave3")
This is caused by lazyness - map returns a lazy sequence which is defined inside the binding but is evaluated outside. You need to force the evaluation from inside:
(binding [*out-dir* "/home/dave"]
(doall (map #(str *out-dir* %) [1 2 3])))
It's true that laziness and dynamic bindings can cause problems; however, abandoning laziness is not the only solution. If you wish to preserve laziness (or to use dynamic bindings with pmap), use bound-fn or bound-fn*.
(def ^:dynamic x 0)
=> (binding [x 3] (map #(+ x %) (range 10)))
;; (0 1 2 3 4 5 6 7 8 9)
=> (binding [x 3] (map (bound-fn [y] (+ x y)) (range 10)))
;; (3 4 5 6 7 8 9 10 11 12)
=> (binding [x 3] (map (bound-fn* #(+ % x)) (range 10)))
;; (3 4 5 6 7 8 9 10 11 12)
Another solution is to use Python-style generator functions available via lazy-gen and yield from the Tupelo library:
(ns tst.demo.core
(:use demo.core tupelo.test)
(:require
[tupelo.core :as t] ))
(t/refer-tupelo)
(def ^:dynamic foo 1)
(dotest
(let [result (binding [foo 3]
(lazy-gen
(doseq [x (range 3)]
(yield {:foo foo :x x})))) ]
(println result)))
result => ({:foo 3, :x 0}
{:foo 3, :x 1}
{:foo 3, :x 2})

idiomatic selection sort in clojure

I was trying to implement selection sort O(n2) in clojure. Yes the underlying sort uses the very efficient java's array sort. However this is a learning exercise.
The code below works, however I was wondering if there is a more idiomatic way of rewriting it, as the below seems clunky -
(defn mins [coll]
(reduce (fn[[min-coll rest-coll] val]
(case (compare val (first min-coll))
-1 [[val] (apply conj rest-coll min-coll)]
0 [(conj min-coll val) rest-coll]
1 [min-coll (conj rest-coll val)]))
[[(first coll)] []]
(rest coll)))
;; (mins [3 1 1 2]) => [[1 1] [3 2]]
(defn selection-sort [coll]
(loop [[sorted coll] [[] coll]]
(let [[s c] (mins coll)]
(if-not (seq coll)
sorted
(recur [(concat sorted s) c])))))
(selection-sort [3 1 1 2 5 7 8 8 4 6])
A functional solution could be:
(defn selection-sort
[input]
(let [ixs (vec (range (count input)))
min-key-from (fn [acc ix] (apply min-key acc (subvec ixs ix)))
swap (fn [coll i j] (assoc coll i (coll j) j (coll i)))]
(reduce
(fn [acc ix] (swap acc ix (min-key-from acc ix))) input ixs)))
You can use the following:
(defn remove-first [coll e]
(if-let [pos (and (seq coll) (.indexOf coll e))]
(vec (concat
(subvec coll 0 pos)
(subvec coll (inc pos))))
coll))
(defn best [coll f]
(reduce f
(first coll)
(rest coll)))
(defn select-sort
([coll] (select-sort coll min))
([coll fmin]
(loop [sorted (transient []) c (vec coll)]
(if (seq c)
(let [n (best c fmin)]
(recur (conj! sorted n) (remove-first c n)))
(persistent! sorted)))))
=> (select-sort [3 5 2])
=> [2 3 5]
=> (select-sort [3 5 2] max)
=> [5 3 2]

Strange error when using atoms inside deftype

I have the following code, defining a type that has an atom in there.
(defprotocol IDeck
(vec-* [dk] "Output to a persistent vector")
(count-* [dk] "Number of elements in the deck")
(conj1-* [dk & es] "Adding multiple elements to the deck"))
(deftype ADeck [#^clojure.lang.Atom val]
IDeck
(vec-* [dk] (->> (.val dk) deref (map deref) vec))
(count-* [dk] (-> (.val dk) deref count))
(conj1-* [dk & es]
(try
(loop [esi es]
(let [e (first esi)]
(cond
(nil? e) dk
:else
(do
(swap! (.val dk) #(conj % (atom e)))
(recur (rest esi))))))
(catch Throwable t (println t)))))
(defn new-*adeck
([] (ADeck. (atom [])))
([v] (ADeck. (atom (vec (map atom v))))))
(defn conj2-* [dk & es]
(try
(loop [esi es]
(let [e (first esi)]
(cond
(nil? e) dk
:else
(do
(swap! (.val dk) #(conj % (atom e)))
(recur (rest esi))))))
(catch Throwable t (println t))))
;; Usage
(def a (new-*adeck [1 2 3 4]))
(count-* a)
;=> 4
(vec-* a)
;=> [1 2 3 4]
(conj1-* a 1 2) ;; The deftype case
;=> IllegalArgumentException java.lang.IllegalArgumentException: Don't know how to create ISeq from: java.lang.Long
(vec-* a)
;=> [1 2 3 4]
(conj2-* a 1 2) ;; The defn case
(vec-* a)
;=> [1 2 3 4 1 2]
Even though the two conj-* methods are exactly the same, except that one is in a deftype and the other is a normal defn, the first gives an error while the second succeeds. Why is this?
This is because protocols doesn't support variable number of arguments.
What you can do is make:
(conj1-* [dk & es] "Adding multiple elements to the deck"))
into
(conj1-* [dk es] "Adding multiple elements to the deck"))
such that the es param will be vector and called like:
(conj1-* a [1 2])

make sequence side-effectfull in Clojure

What I want to do is like following.
(def mystream (stream (range 100)))
(take 3 mystream)
;=> (0 1 2)
(take 3 mystream)
;=> (3 4 5)
(first (drop 1 mystream))
;=> 7
The stream function make sequence side-effectfull like io stream.
I think this is almost impossible.
Here is my attempt.
(defprotocol Stream (first! [this]))
(defn stream [lst]
(let [alst (atom lst)]
(reify Stream
(first! [this]
(let [[fs] #alst]
(swap! alst rest)
fs)))))
(let [mystream (stream (iterate inc 1))]
(map #(if (string? %) (first! mystream) %)
[:a "e" "b" :c "i" :f]))
;=> (:a 1 2 :c 3 :f)
Unfotunately this approach need to implement all function I will use.
Judging by your followup comment to Maurits, you don't need mutation, but rather simply need to emit a new sequence with the elements in the right place.
For example:
(defn replace-when [pred coll replacements]
(lazy-seq
(when (seq coll)
(if (seq replacements)
(if (pred (first coll))
(cons (first replacements)
(replace-when pred (rest coll) (rest replacements)))
(cons (first coll)
(replace-when pred (rest coll) replacements)))
coll))))
user=> (def seq1 [:a :b :c])
#'user/seq1
user=> (def seq2 [:x "i" "u" :y :z "e"])
#'user/seq2
user=> (replace-when string? seq2 seq1)
(:x :a :b :y :z :c)
This won't work with the standard take and drop, but you could quite easily write your own to work on a mutable atom, e.g. you could do something like this:
(def mystream (atom (range 100)))
(defn my-take [n stream]
(let [data #stream
result (take n data)]
(reset! stream (drop n data))
result))
(my-take 3 mystream)
=> (0 1 2)
(my-take 3 mystream)
=> (3 4 5)