This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-06-27
Channels
- # bangalore-clj (2)
- # beginners (37)
- # boot (16)
- # cider (17)
- # clara (4)
- # cljs-dev (351)
- # cljsrn (16)
- # clojure (219)
- # clojure-belgium (4)
- # clojure-dev (3)
- # clojure-france (2)
- # clojure-italy (24)
- # clojure-russia (23)
- # clojure-spec (55)
- # clojure-switzerland (3)
- # clojure-uk (89)
- # clojurescript (121)
- # cursive (2)
- # datomic (29)
- # devops (2)
- # graphql (8)
- # hoplon (15)
- # immutant (5)
- # lein-figwheel (4)
- # liberator (3)
- # luminus (18)
- # off-topic (9)
- # om (6)
- # onyx (31)
- # pedestal (48)
- # precept (9)
- # re-frame (19)
- # reagent (63)
- # ring-swagger (69)
- # robots (1)
- # slack-help (14)
- # spacemacs (12)
- # sql (2)
- # test-check (4)
- # unrepl (28)
- # untangled (5)
- # yada (3)
@curlyfry Thanks your solution.
feels almost perfect for ->>, if only I could figure out a way to get both the sum and the count threaded in there..
How to get current working directory in Clojure?
on UNIX platforms (System/getenv "PWD")
gives the right answer
actually I bet
(.getCanonicalPath ( "."))
is the most reliable optionThanks.
@noisesmith What is .getCanonicalPath
?
it's a method on a java.io.File object, which http://clojure.java.io/file creates
@clj.max ->>
is for threading, not splitting. Itās for when you want to take an argument, then apply function x to it, then apply function y to the result, then function z to the result of that, etc.
I think your āimperative letā approach is the most straightforward way to do it in this case. You could also do (defn avg [coll] (/ (reduce + coll) (count coll)))
, but the āletā approach is slightly more explicit as to what you are doing. IMO code is easier to maintain when itās more explicit
jff one-pass average with threading š
(defn avg
[coll]
(let [{:keys [sum cnt]}
(reduce
(fn [acc x]
(-> acc
(update :sum (partial + x))
(update :cnt inc)))
{:sum 0 :cnt 0}
coll)]
(when-not (zero? cnt)
(/ sum cnt))))
that's a really good piece of evidence that threading makes no sense for this
(when compared with the simpler alternatives)
other silly ways to get an average include
(apply / ((juxt (partial apply +) count) coll))
another, recursive one š
(defn avg
[coll]
(loop [[x & xs] coll avg 0 n 0]
(let [nextn (inc n)
avgn (/ (+ x (* n avg)) nextn)]
(if (seq xs)
(recur xs avgn nextn)
avgn))))
(s/def ::fixed-length (s/& (s/+ int?) #(= 2 (count %))))
(defn variable-length?
[{:keys [length data]}]
(= length (count data)))
(s/def ::variable-length (s/and (s/cat :length int?
:data (s/* int?))
variable-length?))
(s/conform ::fixed-length [1 2]) ;; => [1 2]
(s/conform ::variable-length [2 1 1]) ;; => {:length 2, :data [1 1]}
(s/conform (s/cat :fixed ::fixed-length
:variable ::variable-length)
[1 2 2 1 1]) ;; => :clojure.spec.alpha/invalid
I have three specs: ::fixed-length
, ::variable-length
and the two combined. The first two work as expected individually.
It looks like ::fixed-length
is too āgreedyā but Iām not sure how to prevent this.
@manutter51 thanks, good to know. i'll leave it as a let then.
(s/def ::fixed-length (s/& (s/+ int?) #(= 2 (count %))))
(defn variable-length?
[{:keys [length data]}]
(= length (count data)))
(s/def ::variable-length
(s/& (s/cat :length int? :data (s/* int?))
variable-length?))
(s/conform (s/cat :fix ::fixed-length
:var ::variable-length)
[1 2 3 1 1 1])
Thanks @a13 , thatās exactly what I was looking for. I need to read up on the distinction between s/& and s/and.
@harrigan The difference is that regex specs concatenate, while non-regex specs nest. For your first attempt [1 2 [2 1 1]]
is valid.
whats a good use case for this https://clojuredocs.org/clojure.core/any_q ?
@drewverlee In specs: (fdef identity :args (s/cat :x any?))