Fork me on GitHub
#clojure
<
2020-08-13
>
Yehonathan Sharvit11:08:07

What’s an idiomatic way to pass a map to a function that receives keys and values without the wrapping curly braces? For instance,

(defn do-great-stuff [& {:keys [price time}] 
  ...)
And I have the info in a map
(def the-deal {:price 1000 :time 1})
How do I call do-great-stuff with the-deal ? I can make it with:
(apply my-keys (apply concat the-deal))
Is there a better way?

Yehonathan Sharvit11:08:39

I am looking for a solution that doesn’t mention explicitly the keys :price and :time

vlaaad11:08:49

apply concat -> mapcat identity

Yehonathan Sharvit11:08:14

@vlaaad why do you think it’s better?

vlaaad11:08:31

Because of semantics

vlaaad11:08:17

You are flattening a sequence of entries — this is a collection operation

vlaaad11:08:21

Anyway, currently this is the way to pass a map to a kwarg function. There are rumors it might be supported out of the box in the future by language itself

Yehonathan Sharvit11:08:00

Where did you hear the rumors?

dharrigan11:08:30

Here's a little discussion on apply concat and mapcat identity. https://stuartsierra.com/2019/09/25/sequences-in-flatland

vlaaad11:08:40

From Alex Miller here on slack

vlaaad11:08:04

He said Rich experimented with it and decided this is not crazy

vlaaad11:08:19

This - autogenerating a function arity that takes a map for functions taking kwargs

Yehonathan Sharvit11:08:30

Thanks for the info

souenzzo13:08:41

I'm using (sequence (comp cat cat) colls) and others transducer solutiobs

raydel9519:08:30

Hi! I came into this one on twitter

user> (reduce + ["1" "2" "3"])
java.lang.String cannot be cast to java.lang.Number
user> (reduce + ["1"])
"1"
I thought it was because of the + implementation when it receives just one parameter
([x] (cast Number x))
but then I tried
user=> (+ "3")
Execution error (ClassCastException) at java.lang.Class/cast (Class.java:3369).
Cannot cast java.lang.String to java.lang.Number
I was sure that the last one was not going to return an integer, And now i’m confused 😅 Can someone explain me, why that behavior in the reduce?? (sorry about my english), thanks in advance

kenny20:08:50

From the reduce docstring: "if coll has only 1 item, it is returned and f is not called."

👍 3
raydel9520:08:24

Ohh, I see, I should have started there, thanks

quoll20:08:29

Also worth noting is that behavior only applies when there is no initialization argument. So if you start with an empty string: (reduce + "" ["1" "2" "3"]) then it would fail because the first call would be to: (+ "" "1")

👍 3
raydel9520:08:28

Interesting

quoll20:08:37

Ghadi’s comment about + is important. It’s easy to mix it up because of Java and JavaScript! But you were looking for str

quoll20:08:09

OK… so now I have a TIL as well! This made me wonder, because I know that ClojureScript uses JavaScript’s + operator

quoll20:08:39

cljs.user=> (+ "" "1")
WARNING: cljs.core/+, all arguments must be numbers, got [string string] instead at line 1 <cljs repl>
"1"

quoll20:08:13

But that warning is swallowed in a reduce

cljs.user=> (reduce + ["1" "2" "3"])
"123"

quoll20:08:00

Note: Don’t do this! 🙂

raydel9520:08:57

but it’s always good to know the “why”

seancorfield20:08:48

Also worth mentioning this case:

user=> (reduce + "" [])
""
If you specify an init value and an empty collection, the init value is returned and f is not called then either.

👍 6
quoll20:08:25

Good call Sean! 🙂

ghadi20:08:00

+ works on numbers, not strings

👍 3
raydel9520:08:43

yes, I understand that, I just considered it was a weird behavior in the reduce, and like @U083D6HK9 told me in the other thread: from the `reduce` docstring: “if coll has only 1 item, it is returned and f is not called.”

ghadi20:08:05

It is not recommended to use the reduce arity that is missing the explicit init arg

👍 12
phronmophobic22:08:29

Is anyone else having trouble deploying to clojars or know what I might be doing wrong? I keep getting the following error:

Could not transfer metadata com.phronemophobic:membrane/maven-metadata.xml from/to snapshots (): Access denied to: , ReasonPhrase: Forbidden - S3 request failed.                                                                                                                                                                                                        
Failed to deploy metadata: Could not transfer metadata com.phronemophobic:membrane/maven-metadata.xml from/to snapshots (): Access denied to: , ReasonPhrase: Forbidden - S3 request failed.                                                                                                                                                                             
I've deployed successfully previously, but haven't been able to deploy today. Neither snapshots or releases for two separate projects seem to work. I'm deploying using the latest leiningen with `lein deploy clojars`