Fork me on GitHub
#clojure
<
2018-04-08
>
qqq05:04:32

What is a good name for the foo function below?

(defn foo [f ks]
  (into {} (for [k ks] [k (f k)])))

(foo #(* % %) (range 3))
(comment
 {0 0, 1 1, 2 4})

the2bears05:04:54

@qqq 'mapf' maybe? 'map-keys'? Neither seems quite right...

qqq05:04:03

the best name I have so far is (k->v)->ks->map<k,v> but that is not a valid clojure symbol name

qqq05:04:58

clearly, the solution is to use unicode chars šŸ™‚

the2bears05:04:56

Not a fan of that, but if you are consider 'mapk->fk'?

qqq05:04:10

I think text obfuscated the sarcasm in suggesting (k->v)->ks->map<k,v> -- I'd really prefer english words to type sigs šŸ™‚

andy.fingerhut06:04:39

partial-fn->map ?

andy.fingerhut06:04:15

It is a map that represents a partial version of a function.

leongrapenthin12:04:50

I'd call it memo-map.

dvcrn13:04:03

I just ran into clojure.core.match` and am in love. Is it bad to do things Elixir/Erlang style in clojure? Like

(if (... some validation ...)
        [:ok (response :body)]
        [:error (response :error-text)]))))

dvcrn13:04:03

I just ran into clojure.core.match` and am in love. Is it bad to do things Elixir/Erlang style in clojure? Like

(if (... some validation ...)
        [:ok (response :body)]
        [:error (response :error-text)]))))

dvcrn14:04:00

interesting

arrdem15:04:56

core.match is awesome and I've used it for a bunch of things!

arrdem15:04:22

I completely disagree with Rich, sometimes you want to bake in some brittleness

tbaldridge20:04:17

A lot of Rich's critiques are feel are stronger against typing systems when used with pattern matching. At run time, the parts of a OCaml type aren't named. That's to say you can't reflect on X and find out what parts X is made up of. I think a pattern matcher in the spirit of Clojure's values could be quite nice. Something like spec's keys or de-structuring:

(match x
  {:shape/width w :shape/h h} (* w h) 
  {:shape/radius r} (* PI (* r r)))

But most pattern matching just devolves down to matching on vectors, tuples or tagged unions, which IMO is quite ugly.

leongrapenthin08:04:28

Returning tuples of tag and data is fine

leongrapenthin08:04:26

For example clojure.spec s/conform also does that

leongrapenthin08:04:08

Usually you dont need core match to dispatch them

dvcrn00:04:51

For errors, when not explicit matching of a [:error e] / [:ok val] pair like in Elixir/Erlang, or having multi returns like in Go, what is the idiomatic way to handle errors explicitly in Clojure?

dvcrn00:04:05

also thinking about clojurescript where try/catch is a bit more tricky because of the lack of strong types

dvcrn00:04:01

looking at it, core.match looks to be exactly what I wanted in Clojure for all these cases

seancorfield01:04:52

@ If you return either {:ok val} or {:error e} then you can do

(let [{:keys [ok error]} (some-thing)]
  (if ok
    (process ok)
    (handle error)))
or if you don't care about the error at all
(if-let [result (:ok (some-thing))]
  (process result)
  (something-failed))

seancorfield01:04:47

(although you can't return false / nil that way -- you'd need to swap the test over to error instead and use the first approach)

seancorfield01:04:35

(let [{:keys [ok error]} (some-thing)]
  (if error
    (handle error)
    (process ok)))

dvcrn02:04:51

Oh cool, I might try that

dvcrn02:04:31

Though I barely see these patterns in clojure code so Iā€™m wondering on one side why not, and what other ways to handle errors are more commonly used

arrdem03:04:37

https://youtu.be/ZQkIWWTygio I really recommend this talk. I've personally found the tagged variant pattern to be superior for the reasons jneen talks about if you have appropriate tooling around it. http://github.com/arrdem/guten-tag was my take at that.

arrdem03:04:09

the internals of http://conj.io are mostly written in various variant patterns.

alex-dixon15:04:37

Anyone have a minimal prepl example?

dominicm08:04:11

It depends.

dominicm08:04:50

There's no single answer, and the divide of which to choose is rather large.