Fork me on GitHub
#meander
<
2020-05-19
>
dominicm15:05:24

Has anyone made a try+ meander yet?

dominicm16:05:15

Like slingshot.

dominicm16:05:23

I was thinking this would be pretty dope:

(try+
  (add-user! user)
  (catch {:type ::data.user/user-exists :id ?user-id}
     {:message (format "Oh no, %s user exists!" ?user-id)})
  (catch {::anom/category :forbidden}
    {:message "GO AWAY"})
  (catch SQLException e
    {:message "Write better SQL, Idiot"}))

4
dominicm17:05:14

What's the "match anything" or what's the "else" for m/match?

dominicm17:05:04

Quite enjoying this, because I can write naively and optimize the macro output

🙂 4
jimmy17:05:39

Match anything is _

jimmy17:05:07

Or if you want to capture it, you can just do any any ?logic_variable.

noprompt17:05:18

LOL I need more messages like “Write better SQL, Idiot” in my life.

noprompt17:05:29

I can’t remember which command it is/was but it would complain about a “Cowardly attempt to…”

timothypratley18:05:17

For me its Cowardly refusing to create an empty archive from tar 🙂

noprompt19:05:41

HAHAHA! Yeah! Thats the one! 😂

noprompt19:05:07

For some reason I remembered it as calling me the coward. 😛

noprompt17:05:55

Just calling me out.

dominicm19:05:36

Hmm.. (if (= (System/getenv "USER") "noprompt") (throw ...))...

noprompt19:05:58

Okay… I’m slowly getting my head out of the vacation clouds.

dominicm19:05:00

It's 70loc, but I have it...

dominicm19:05:28

Something that's a little tricky is the fact you have to "jump" priorities a little bit.

dominicm19:05:42

This is fully functional though:

(try+
    (throw (ex-info "Invalid" {:type :pity :reason "???"}))
    (read (clojure.lang.LineNumberingPushbackReader. (java.io.StringReader. ")")))
    (throw (ex-info "Invalid" {:type :invalid :reason "I pity the fool"}))
    (/ 1 0)
    (catch ArithmeticException _
      ::infinity-stones)
    (catch clojure.lang.LispReader$ReaderException {:clojure.error/line ?line}
      (println "Check line" ?line))
    (catch {:type :invalid :reason ?reason}
      (prn ?reason)
      nil)
    (catch {:type :pity :reason ?reason}
      (println "PITYPITYPITY" ?reason)
      nil)
    (catch clojure.lang.ExceptionInfo e
      (prn (ex-data e)))

    (finally (println "Cleaning up")))

noprompt20:05:11

Pulled it off with rewrite?

dominicm20:05:34

Didn't need it at all in the end. Rewrite made it more complicated tbh, because you have to look ahead. I'm sure you'll show me the one-liner in a sec though ;)

dominicm20:05:36

I'll give it a real name later, but it seems like a neat idea & works in the REPL.

noprompt20:05:23

I kinda wanna mess around with this.

dominicm20:05:16

Do it. I consider the idea primitive, so if you want to build on top of it, go to town. I don't feel any particular ownership.

dominicm20:05:19

Meander has a great api for simple cases, like matching a few keys in a map on value. It makes this kind of lightweight thing really nice. The only real catch is that I need to specify that you need to use epsilon in order to interact with it, as it's incompatible with the other versions. But that's not a big deal really, eventually it'll be stable and I can "meander" rather than caveating