meander

sergey 2022-02-11T17:15:47.404019Z

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
Richie 2022-02-11T21:20:04.419559Z

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

noprompt 2022-02-11T22:40:28.438789Z

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

[_ _ & _]

sergey 2022-02-11T23:28:19.325919Z

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

sergey 2022-02-12T00:03:40.595309Z

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

👍 1
Richie 2022-02-11T21:21:11.951839Z

(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?

noprompt 2022-02-11T22:43:13.323299Z

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.

noprompt 2022-02-11T22:43:54.898719Z

defsyntax for all intents and purposes is like defmacro.

noprompt 2022-02-11T22:44:24.239579Z

defsyntax defined functions are being called with literal arguments.

Richie 2022-02-11T22:52:59.163039Z

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

Richie 2022-02-11T22:53:27.058159Z

Thanks!