Fork me on GitHub
#beginners
<
2017-06-10
>
colinkahn17:06:23

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})

noisesmith17:06:26

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

colinkahn17:06:20

Sorry, the vector is the key/value getting destructured in the map callback

noisesmith17:06:47

oh, right - then it would be (fn [e] (update e 1 do-something))

noisesmith17:06:16

but I find the destructure and vector literal easier to read

colinkahn17:06:59

ah cool. good to know that's an option

colinkahn17:06:07

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})

mobileink17:06:52

you know about clojure.walk? might be overkill for your case but it's good to know about.

noisesmith17:06:56

well that's what (update e 1 f) does...

colinkahn18:06:20

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?

noisesmith18:06:33

I don't think clojure.walk makes sense for this at all

mobileink18:06:12

no idea, sorry. just wanted to make sure you knew about it, this being #beginners .

noisesmith18:06:17

sorry - there was some crosstalk, update does not use clojure.walk

colinkahn18:06:33

oh ok, gotcha

noisesmith18:06:48

I was saying update e 1 was effectively update-val e

grierson21:06:35

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?

noisesmith22:06:06

clojure doesn't abstract away from the platform like that. The platform has null, so clojure has nil

grierson22:06:38

Kolin uses JVM as well doesn’t it?

noisesmith22:06:46

right, but kotlin prevents you from doing things in it that the jvm allows

noisesmith22:06:25

(clojure does that in some cases too, around inheritance in particular, but not with null)

noisesmith22:06:51

@grierson and clojure isn't strongly typed - it enforces types far less than java does even

akiroz23:06:08

@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...

noisesmith23:06:34

@akiroz but that's not true

noisesmith23:06:59

it puns as an empty list for conj/ cons / seq, but it is never treated as a number

akiroz23:06:56

Wow, looks like I've been writing CLJS for too long

noisesmith23:06:17

right, that's what I was saying about "not abstracting from the implementation"

akiroz23:06:22

and it was only because in JS null + 1 => 1

noisesmith23:06:22

that's js doing that, not clojure

akiroz23:06:50

I always thought this was by design of nil-punning

noisesmith23:06:34

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

akiroz23:06:17

nil to "" seem to work

noisesmith23:06:48

(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)))

noisesmith23:06:00

it's not implicit - only in calls to str

noisesmith23:06:07

regex ops on nil blow up, for example