Fork me on GitHub
#beginners
<
2018-02-14
>
ajwerner05:02:19

what's a nice way to update a set of keys in a map?

seancorfield05:02:37

@awerner32 Depending on exactly what you want to do, you might thread the map into a series of assoc calls or do a merge...

seancorfield05:02:58

(merge my-map {:key-a "val-a" :key-c 42})

seancorfield05:02:10

Of course assoc accepts multiple key/value pairs too: (assoc my-map :key-a "val-a" :key-c 42)

ajwerner05:02:49

rather than setting a value, I'd like to apply a function to the current value for a set of keys. for example, I have a map with many keys and I want to increment only keys :a :b and :c

richardh12:02:33

If you want to make it repeatable:

(defn update-multi [m keys f]
  (reduce (fn [m k] (update m k f)) m keys))
(update-multi {:a 500 :b 600 :c 700} [:a :b] inc)
;; => {:a 501, :b 601, :c 700}

ajwerner05:02:43

fancy seeing you here david

noonian05:02:00

@awerner32 thats not something I’ve had to do often but I would probably reach for reduce

(def my-map {:a 1 :b 2 :c 3 :d 4 :e 5})
(reduce (fn [m k] (update m k inc)) my-map [:a :b :c])

seancorfield06:02:02

If it's only a small, fixed number of keys, threading through update is also a possibility

(-> my-map
    (update :a inc)
    (update :b inc)
    (update :c inc))
if you perhaps wanted different functions applied to the small set of keys you wanted to update.

hawari12:02:31

Hi everyone, how do you get the LocalDateTime object from a string with offset time like "2018-02-14T04:30:00.000Z"? For example, if I live in UTC +07:00, I want to parse that string into a LocalDateTime object with value of "2018-02-14T11:30:00.000".

hawari12:02:37

I've tried parsing it into OffsetDateTime first, and call .toLocalDateTime method with it, but it seems like the method only "get the localdatetime part", not necessarily convert it into your system time zone as it returns "2018-02-14T04:30:00.000"

hawari12:02:28

I've looked at the joda-time website, and it says that users are encouraged to migrate to java.time since Java 8, should I be concerned with that? If not, I'd be happily using a wrapper like that, it seems more "idiomatic".

beetleman12:02:49

its nice lib. Sorry for my dumb answer but i think they have some examples for timezones

rauh12:02:44

@hawari.rahman17 This should do it:

(LocalDateTime/ofInstant (Instant/parse "2018-02-14T04:30:00.000Z")
                         (ZoneId/of "+7"))

rauh12:02:37

IMO no reason for a wrapper library. The new java.time API is awesome. Very clojure like (immutable, funcitonal)

hawari12:02:07

Yeah, I kinda like it, there are more static methods than you'd usually find @rauh

hawari12:02:49

It seems there are more things that you need to set though, not as abstract as what I found on clj-time

richardh12:02:33

If you want to make it repeatable:

(defn update-multi [m keys f]
  (reduce (fn [m k] (update m k f)) m keys))
(update-multi {:a 500 :b 600 :c 700} [:a :b] inc)
;; => {:a 501, :b 601, :c 700}

dadair21:02:28

Is it possible to get websockets with ring-jetty? Or do websockets demand http-kit/sente, immutant, aleph, etc?