This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-06-10
Channels
- # aleph (1)
- # beginners (39)
- # boot (14)
- # cider (2)
- # clara (6)
- # cljs-dev (39)
- # cljsrn (2)
- # clojure (276)
- # clojure-italy (1)
- # clojure-russia (22)
- # clojure-sg (2)
- # clojure-spec (7)
- # clojure-uk (9)
- # clojurescript (47)
- # core-async (1)
- # cursive (5)
- # emacs (1)
- # events (3)
- # leiningen (1)
- # luminus (2)
- # lumo (75)
- # om (14)
- # onyx (1)
- # parinfer (11)
- # pedestal (1)
- # ring-swagger (2)
- # spacemacs (4)
- # untangled (5)
- # yada (29)
Is there a concise way to change one item in a vector but have the others unchanged? Right now I have this:
(map (fn [[k v]] [k (do-something v)]) {:a 1 :b 2 :c 3})
for starters that's not a vector - if you want to change the value under a specific key, that's what assoc
and update
are for
oh, right - then it would be (fn [e] (update e 1 do-something))
but I find the destructure and vector literal easier to read
I think I want something like an update-val
fn seeing the alternative, so I can just do something like (map #(update-val % do-something) {:a 1 :b 2 :c 3})
you know about clojure.walk? might be overkill for your case but it's good to know about.
well that's what (update e 1 f) does...
I've used it before, but usually shy away from it, my assumption is it isn't that performant, but if update is implemented using it then I guess it should be?
I don't think clojure.walk makes sense for this at all
no idea, sorry. just wanted to make sure you knew about it, this being #beginners .
sorry - there was some crosstalk, update does not use clojure.walk
I was saying update e 1 was effectively update-val e
I’m been checking out Elm and Kotlin were they describe null as the “The Billion dollar mistake”. What is Clojure’s answer to dealing with null? Elm has a Maybe type, what is equivalent for Clojure?
clojure doesn't abstract away from the platform like that. The platform has null, so clojure has nil
right, but kotlin prevents you from doing things in it that the jvm allows
(clojure does that in some cases too, around inheritance in particular, but not with null)
@grierson and clojure isn't strongly typed - it enforces types far less than java does even
@grierson Clojure's answer to the null problem is called nil-punning. Basically nil instead of being treated as its own type, it will act like whatever type the function expects. For example:
- inc
expects a number, nil acts like 0 so (inc nil) => 1
- conj
expects a list, nil acts like an empty list so (conj nil 1) => (1)
and so on...
@akiroz but that's not true
it puns as an empty list for conj/ cons / seq, but it is never treated as a number
right, that's what I was saying about "not abstracting from the implementation"
that's js doing that, not clojure
right
clojure only puns nil with false and empty list - and there's other cases where it might seem like nil is punned but really just about anything could go there and get the same result
that's true
(defn str
"With no args, returns the empty string. With one arg x, returns
x.toString(). (str nil) returns the empty string. With more than
one arg, returns the concatenation of the str values of the args."
{:tag String
:added "1.0"
:static true}
(^String [] "")
(^String [^Object x]
(if (nil? x) "" (. x (toString))))
(^String [x & ys]
((fn [^StringBuilder sb more]
(if more
(recur (. sb (append (str (first more)))) (next more))
(str sb)))
(new StringBuilder (str x)) ys)))
it's not implicit - only in calls to str
regex ops on nil blow up, for example