This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-11-07
Channels
- # aleph (15)
- # beginners (186)
- # boot (11)
- # bristol-clojurians (1)
- # clara (1)
- # cljdoc (2)
- # cljs-dev (5)
- # clojure (57)
- # clojure-austin (1)
- # clojure-dev (87)
- # clojure-italy (7)
- # clojure-spec (5)
- # clojure-uk (56)
- # clojurescript (18)
- # cursive (29)
- # data-science (10)
- # datomic (84)
- # duct (83)
- # figwheel-main (4)
- # fulcro (42)
- # jobs (3)
- # lambdaisland (2)
- # off-topic (28)
- # parinfer (3)
- # portkey (3)
- # re-frame (28)
- # reitit (7)
- # remote-jobs (8)
- # shadow-cljs (29)
- # spacemacs (30)
- # specter (6)
- # sql (8)
- # tools-deps (60)
I'm trying to (merge-with -) with 2 maps, the idea is, if one key is not exist in the first map, it should be treated as value 0. But merge-with doesn't do that, it only applies the - function on values exist in both maps.
If you know all the expected keys, you could merge a map with all zero values with the first map, and use merge-with in that result and the second map
No~ more like, I should add all the keys in map2 missing from map1 with value 0 into map1 first, before merge-with
yeah, unfortunately merge-with
doesn’t even execute the passed in fn if the key doesn’t exist in both
something like (map #(f (get m1 % 0) (get m2 % 0)) (clojure.set/union (keys m1) (keys m2)))
?
I think Leira, was describing that the desired semantics are something like
(merge-with f (merge (zipmap (keys m2) (repeat 0)) m1) m2)
(defn merge-always-with [f & maps]
(when (some identity maps)
(let [merge-entry (fn [m e]
(let [k (key e) v (val e)]
(assoc m k (f (get m k) v))))
merge2 (fn [m1 m2]
(reduce merge-entry (or m1 {}) (seq m2)))]
(reduce merge2 maps))))
user=> (merge-always-with - {:a 1} {:b 2})
NullPointerException clojure.lang.Numbers.ops (Numbers.java:1018)
user=> (merge-always-with (fnil - 0) {:a 1} {:b 2})
{:a 1, :b -2}
I think you'd want it to handle keys that show up in any subsequent maps, not just the second, though
yeah and you could update the zipmap approach to grab the keys from all additional maps
Nice solution @trailcapital. I suspect that variant also performs really well.
that's what I was thinking! I also popped it into a function to accept many maps and merge them all tail recursively.
(defn merge-with*
[f init & ms]
((fn [m maps]
(if (empty? maps)
m
(recur (reduce-kv #(update %1 %2 f %3) m (first maps))
(rest maps))))
init ms))
It would be cool if reduce
accepted n number of arguments and applied that sort of recursive processing...
(defn- recursive-reduce
[f i & a]
((fn [m args]
(if (empty? args)
m
(recur (reduce f m (first args))
(rest args))))
i a))
=> (recursive-reduce conj [] [1 2 3] [4 5 6] [7 8 9])
[1 2 3 4 5 6 7 8 9]
@trailcapital there's always (partial transduce cat)
so long as you wrap your reducing function with completing
you could even optimize that version with transient
though that's probably overkill...
snippet comes from http://commons.apache.org/proper/commons-math/userguide/leastsquares.html
@zhongtao_liu You can just ignore the generics
@zhongtao_liu you might want to check out https://neanderthal.uncomplicate.org/ for blazing fast native Clojure matrix math stuff
@didibus surprise, surprise, it works! Thanks. @schmee Great library, We'll have a try.
are there any examples of the use of Datafy ?
@octo221 None I know of but playing with it I think I get the idea: it more or less serializes things to EDN, and you can use the Datafiable protocol to specify custom behavior
e.g. (defrecord Foo [a b] clojure.core.protocols/Datafiable (datafy [self] "Sorry, you can't have my data!"))
whereas without extending it, it'll fall back to the standard behavior of #path.to.Foo{:a aval, :b bval}
Note that this isn't about serialization (EDN is notation) but to in-memory data like maps lists vectors sets scalars...
I'm wondering if Stu or Rich will talk about the new stuff they're doing on top of prepl and datafy at Conj?
Stu sort of hinted on the Apropos podcast that he'd be talking about some new tooling he's been using -- and Alex has basically said prepl etc all happened because of something Rich is building.
Given Rich's previous comments about "type systems", I doubt it... 🙂
@ghadi yeah good points both. record was just my arbitrary example but that's a good point about serialization
anyone using the "lein-git-version" plugin?
trying to get it set up but just generating a "clojure.lang.Keyword cannot be cast to java.base/java.lang.String" exception