Fork me on GitHub

given a nested map structure like {1111 {:s 0 :r 1}} {1111 {:s 1 :r 2}} I'm trying to use merge-with to give me the output {1111 {:s 1 :r 3}}


so I need to merge-with + the inner map


but I'm not sure how to combine duplicates in the outer map


check-stock.core=> (apply merge-with (fn [l r] (merge-with + l r)) '({1111 {:s 0 :r 1}} {1111 {:s 1 :r 2}})) {1111 {:s 1, :r 3}}


some might prefer (apply merge-with (partial merge-with +) ...)


I did some heavy manipulation of data structures last week, and I think my main sticking points, or rather ugly pieces left in my code, have been that I often expected a single map entry from a map, or wanted to create one. In the first case, I would first the result of a filter, but I wonder if there's a cleaner way (before implementing a macro myself). In the second, I am not aware of a neat way of creating a map entry, other than enclosing a tuple in a vector, then stuffing it into a map at some later point in the logic. Other than saying map entries reflect residues of OO thinking, any comments to that? What do you think?


@matan nothing wrong with stuffing tuples into maps IMO


but I don't really understand this first-filter approach, could you give an example ?


@matan I find medley to be the sweet point of "stuff I find myself doing a lot" vs "this stuff is confusing"

Alex Miller (Clojure team)14:05:39

@matan 2-entry vectors are capable map entries. at some future point we may have actual tuples (there has been some work done, but retracted, on this in the past).


I have a case where I have many different records which extend a protocol the same exact way. Is there any concise way to do that? stating multiple classes under the extend-protocol doesn’t seem to work:

(extend-protocol AProtocol
  Class1 Class2 Class3
  (foo [_] true)
  (bar [_] false))


although (extenders AProtocol) returns (Class1 Class2 Class3) the extension of the protocol happens only to the last i.e. Class3. Calling foo with arguments other than an instance of Class3 results in IllegalArgumentException


@gerry you could write a custom macro and call it repeatedly :)


@alexmiller @dominicm thanks guys, I appreciate it! I just try to retrospect and this has been really helpful. I was otherwise a bit surprised to see some functions not preserving the collection type provided to them, but didn't have time to make a concrete description while at it as I was working towards a specific date.


Thanks again


@dominicm maybe medley will replace my own private evolutionary util lib then 🙂 although with leiningen's support for symlinks it's easy curating a util lib as I go


your let bindings look malformed


what is wrong with it?


i think you have rank-map and the value you want it to assume inverted


i'm assuming you want rank-map to have the value {"T" 10 ...}


otherwise as stated here you want a map literal to have the value of a var that has not been introduced. which would be your error i'd imagine


all of this assuming that card looks sufficiently like [rank suit] to destructure here


yes the card bidining is correct, I've already tested it


Is it possible to give a value multiple tags in edn? i.e. is {:port #num #env SERVER_PORT} valid, assuming that I’ve implemented reader functions for both num and env?

Alex Miller (Clojure team)22:05:06

I would not call this “multiple tags” - this is a tagged value that contains a tagged value

Alex Miller (Clojure team)22:05:53

so “nested tags” perhaps, but not two tags applied to the same value