-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
View don't handle exceptions #20
Comments
|
I understand. But I expect that first example fails with "no match". |
For example I match a complex deep data structure.
|
I believe the contract of all pattern combinators should be as minimal as possible. If we had types, With your proposed changes, we make our semantics more complex. We will be making a choice of which exceptions to ignore on user's behalf without knowing their use case, which seems wrong. Additionally, we risk swallowing exceptions inadvertently. I would suggest doing one of the following:
akar.try-out=> (defn !try-view [f]
#_=> (fn [x]
#_=> (try [(f x)]
#_=> (catch RuntimeException _ nil))))
#'akar.try-out/!try-view
akar.try-out=> (match 0
#_=> [(!try-view last) x] (println x))
RuntimeException Pattern match failed. None of the clauses applicable to the value: 0.
akar.internal.utilities/fail-with (utilities.clj:24)
akar.try-out=> (match [1 0 9]
#_=> [(!try-view last) x] (println x))
9
nil |
I completely agree! 👍 |
(defmacro fn-silent-> [& xs]
`(fn [x#]
(try
(-> x# ~@xs)
(catch RuntimeException _# nil))))
{:via (:view (fn-silent-> peek s/form first) [(!constant `s/keys)])
:pred (:view (fn-silent-> last last) k)
:in in} May be I'll find a better solution. |
Why not use {:via [(!try-view (comp first s/form peek)) [(!constant `s/keys)]]
:pred [(!try-view (comp last last)) k]
:in in} |
Probably you are right. I am also confused by the mixture of syntax |
If you run I will draw your attention to three of those lines: pattern' => any' | literal' | constant' | bind' | guard-pattern' | view-pattern' | or-pattern' | and-pattern' | seq-pattern' | map-pattern' | variant-pattern' | record-pattern' | type-pattern' | arbitrary-pattern'
...
view-pattern' => (:view form pattern')
...
arbitrary-pattern' => [form pattern'*] Akar provides a dedicated syntactic support for a bunch of things, including view patterns. These special syntactic constructs always look like |
If you haven't done so already, I highly recommend working through the tutorial. |
You don't understand me =) |
I see. Its different syntactic appearance is intentional, to make it stand out from dedicated syntactic patterns. :) It's not supposed to be rare at all! With Lisp, we are limited to very few syntactic tools, and have to make the best of it. Another alternative I thought of, at the time I built this, was |
I had another idea too: |
It's complex task I think. It is necessary to consider approaches in other languages. Custom patterns should be looks like library patterns.
|
Another case. It's about clojure lists. Data example:
I can't describe this pattern in terms of data patterns because it's too complex for lists.
I can only understand approach with
|
You can use As for symbols, I wonder if we should add support for symbol literals at the syntax level, so you can say |
You could define some helper pattern functions too. Like |
Thanks for your answer. Hi! I rewrite my clojure module without Akar. Old solution https://github.com/darkleaf/publicator/blob/7d52ca2/src/publicator/web/problem_presenter.clj Is Akar suitable for my case? Thanks! |
You could, of course, rewrite it without Akar. But your problem seems like it could benefit from pattern matching. You are inspecting Spec outputs which are very structured. Akar supports view patterns for completeness' sake, but I would normally just define lots of small pattern functions, and then use those. That could make your code easier to read. |
The text was updated successfully, but these errors were encountered: