Fork me on GitHub
#meander
<
2022-02-11
>
sergey17:02:47

hey y'all I'm trying to write a pattern to match lists with two or more items, and I'm seeing the "n or more" operator match on a empty list when I use it in an m/search - here's what I'm seeing:

(m/match
    []
    [_ ..2] :matched-two-or-more
    _ :didnt-mach)
;; => :didnt-mach
  (m/search
    []
    [_ ..2] :matched-two-or-more)
;; => (:matched-two-or-more)
Is this expected behavior? I would assume that [_ ..2] means 2 or more elements in the list

🤷 1
Richie21:02:04

(m/match [1 2]
  (m/pred #(< 1 (count %))) :more
  _ :one)

noprompt22:02:28

This looks like a bug. Could you file it in the issue tracker? As a temporary work around try

[_ _ & _]

sergey23:02:19

sure thing -- will do! FWIW, the pattern I ended up using was [_ . _ . _ ...] but [_ _ & _] worked as expected as well

sergey00:02:40

alright, https://github.com/noprompt/meander/issues/228#issue-1133344037 - lmk if there's anything else I can help with

đź‘Ť 1
Richie21:02:11

(m/defsyntax
  compare-op
  [op a b]
  (case op
    :eq `(= ~a ~b)
    :not-eq `(not= ~a ~b)
    :lt `(< ~a ~b)
    :lt-e `(<= ~a ~b)
    :gt `(> ~a ~b)
    :gt-e `(>= ~a ~b)
    :is `(= ~a ~b)
    :is-not `(not= ~a ~b)
    :in `(some #{~a} ~b)
    :not-in `(not (some #{~a} ~b))))

(m/rewrite {:node :eq
            :a 1
            :b 2}
  {:node :eq
   :a ?a
   :b ?b}
  (compare-op :eq ?a ?b))
;; (clojure.core/= 1 2)

(m/rewrite {:node :eq
            :a 1
            :b 2}
  {:node ?op
   :a ?a
   :b ?b}
  (compare-op ?op ?a ?b))
;; 1. Caused by java.lang.IllegalArgumentException
;;    No matching clause: ?op
How can I understand what’s going on here?

noprompt22:02:13

When compare-op is expanded it is handed the literal arguments ?op, ?a, and ?b. The case is failing because ?op (the literal symbol ?op) has no clause.

noprompt22:02:54

defsyntax for all intents and purposes is like defmacro.

noprompt22:02:24

defsyntax defined functions are being called with literal arguments.

Richie22:02:59

Oh, I get it. ?op doesn’t show up anywhere on the rhs of that rewrite rule.