Fork me on GitHub
#beginners
<
2017-05-15
>
foamdino04:05:22

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

foamdino04:05:43

so I need to merge-with + the inner map

foamdino04:05:19

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

foamdino05:05:01

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

noisesmith05:05:47

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

matan10:05:01

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?

val_waeselynck12:05:22

@matan nothing wrong with stuffing tuples into maps IMO

val_waeselynck12:05:43

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

dominicm12:05:09

@matan http://weavejester.github.io/medley/medley.core.html#var-find-first 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).

gerry15:05:07

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

gerry15:05:31

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

val_waeselynck17:05:41

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

matan17:05:22

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

matan17:05:25

Thanks again

matan17:05:30

@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

dpsutton19:05:16

your let bindings look malformed

vitruvia19:05:00

what is wrong with it?

dpsutton19:05:27

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

dpsutton19:05:39

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

dpsutton19:05:08

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

dpsutton19:05:22

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

vitruvia19:05:35

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

mhuerster21:05:30

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