This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2019-02-20
Channels
- # announcements (1)
- # beginners (65)
- # calva (16)
- # cider (44)
- # clara (16)
- # clojure (84)
- # clojure-dev (48)
- # clojure-europe (5)
- # clojure-finland (4)
- # clojure-houston (1)
- # clojure-italy (19)
- # clojure-nl (27)
- # clojure-russia (6)
- # clojure-spec (37)
- # clojure-uk (123)
- # clojured (11)
- # clojurescript (21)
- # datomic (40)
- # duct (4)
- # emacs (6)
- # figwheel (4)
- # figwheel-main (5)
- # fulcro (34)
- # jackdaw (8)
- # juxt (117)
- # kaocha (3)
- # klipse (1)
- # leiningen (33)
- # luminus (2)
- # nyc (3)
- # off-topic (29)
- # om (1)
- # pedestal (7)
- # planck (4)
- # re-frame (27)
- # reagent (8)
- # reitit (5)
- # rum (2)
- # shadow-cljs (428)
- # spacemacs (5)
- # tools-deps (15)
- # yada (6)
Hi, looking for some simple solution: I have :
[["John" 32 12.3] ["Kate" 66 32.4]...]
Want to cast it to vector of maps:
[{:name "John" :age 32 :some-val 12.3} ....
I have come up with:
(mapv (fn [x]
(let [[a b c] x]
{:name a :age b :value c}))
[["John" 32 1.2]["Kate" 45 12.2]])
Any better ideas?@piotr.kurnik zipmap
would probably help you here...
(mapv (partial zipmap [:name :age :value]) list-of-vecs)
What are your preferred ways of consuming API's, either sever or client (ClojureScript) side. I'm starting off with a simple JSON API, but will be consuming more interesting ones soon. Do you just use slurp or http-kit for simple Api's? Liberator looks interesting for server side, any thoughts? Are you using Transit to manage transformation of formats? Or do you just drop all the data as a nice Clojure map onto a Kafka topic? Interested in understanding what people are using and why. Thanks.
liberator is for providing http apis, not for consuming.
I've been using https://github.com/JulianBirch/cljs-ajax client side and https://github.com/http-kit/http-kit server side. I use transit if I am the only provider/consumer of the API, regular JSON otherwise.
speaking of server side:
It depends - for simpler use cases we tend to use clj-http
directly.
In some cases we use wrappers such as https://github.com/Raynes/tentacles - we use github api quite extensively and this saves some effort; however, on one occasion, we've also used clj-http to call graphql api (because of performance reasons)
Thanks everyone, most helpful.
why this
(defn ok [y] 5)
(s/fdef ok
:args string?
:ret true?
:fn true?)
is not enforced?args needs to be (s/cat :y string?)
Right, you need to instrument
Specs are not checked by default
So you can call instrument
from clojure.spec.test.alpha
, but that will only check :args
specs. So it will detect a spec failure when you call (ok 5)
If you also want your :ret
and :fn
specs to be checked, you can use something like orchestra: https://github.com/jeaye/orchestra
I was trying to recreate the square root function from SICP: http://sarabander.github.io/sicp/html/1_002e1.xhtml#g_t1_002e1_002e7 in clojure. I came up with this but my answer keeps coming out wrong:
(defn average [x y]
(/ (+ x y) 2))
(defn improve [guess x]
(average guess (/ x guess)))
(defn abs [x]
(if (< x 0)
(- x)
x))
(defn square [x]
(* x x))
(defn good-enough? [guess x]
(< (abs (- (square guess) x)) 0.001))
(defn try [guess x]
(if (good-enough? guess x)
guess
(recur (improve guess x) x)))
(defn sqrt [x]
(try 1.0 x))
(sqrt 2.0)
;; => 2.0
I went to some "SICP in Clojure" resources and still can't seem to find what I'm doing wrong. Anyone notice what's funky?so (try 1.0 x) isn't call your function try, it is compiling as the try/catch special form without a catch, so (try 1.0 x)
~= (do 1.0 x)
A nice way to start learning clojure web apps https://github.com/mcknight816/cjweb
Nice! I'll check this out. I've been starting to slowly dip my toes into web dev. Would you consider Mongo DB beginner friendly? Is that interchangeable?
I think it is. Future improvements might be to allow other no-sql databases. The code minus the comments is less then 100 lines. I'm new to clojure and this just represents what Iv'e learned so far. Truly a powerful language.
(defn process-node [node] (def next (first (:event-list node))) (if (nil? next) "DONE" (do (println next) ;; do something with next ;; and return the node without the first event of the event-list (assoc node :event-list (pop (:event-list node)))))) On the code above. 1) Should I use 'let' instead of 'def'? 2) Is there any better idiomatic way of doing it?
def creates a new var at namespace scope
so yes, the normal thing would be to use let (also the name next
clashes with a function in clojure.core, so it might be good to use a different name as well)
also, checking for first
returning nil means your code can't handle an input like {:event-list [:a nil :b]}
- totally fine if nil would never make sense, but do be aware of the limit of the pattern. The reliable thing is to check if (seq (:event-list node))
is nil.
go it!
also, that pattern of iteration could be replaced with (doseq [event (:event-list node)] (frob event))
- no need to manage list traversal by hand
or even (run! frob (:event-list node))
oh - you aren't iterating here though
and what happens if I need to include new events on the list after processing 'next'? Sometimes one event creates two or more others. In this case I cannot use doseq. Can I?
no, it sounds like you aren't just doing a simple iteration here
you got it. It is part of a much more complex logic.
this would still fit a loop/recur
pattern though
sometimes it will return an (assoc ....) that will include new elements on the list. Só I have to manually transverse the list...
yes. using loop!
When I wrote (assoc node :event-list (pop (:event-list node))) I wondered if exists a better idiomatic way of doing it. Any kind of "pop-in"... 😉
(update node :event-list pop)
or (update-in node [:event-list] pop)
oohh!
but consider using rest
instead of pop, because if a vector will pop in a different order
No because I am using https://github.com/clojure/data.priority-map . But you are right! rest is better!
(and you can't pop a set at all, etc.)
Beautiful! There is always a better way of doing it in Clojure. I love it!
right, definitely stick to one pair and not mix them
(related, rest on a clojure.lang.PersistentQueue returns a seq, which turns your FIFO into a LIFO)
No because I am using https://github.com/clojure/data.priority-map . But you are right! rest is better!
rest will have the same problem as a queue here: calling rest on a priority-map probably won't return a priority-map, it's most likely to return a list, which is simple LIFO
because rest calls its seq
method, and the seq method returns a list not a priority-map
tmp=> (type (rest (pmap/priority-map :a :b))) clojure.lang.LazySeq tmp=> (type (pop (pmap/priority-map :a :b))) clojure.data.priority_map.PersistentPriorityMap
right - you want pop as it preserves the prioritizing feature
thank you guys. You are awesome! 👏