Open ccqpein opened 8 months ago
Nice, that is pretty interesting.
With some indentation the snippet becomes
(str-match "a1c5b"
(("a" b "c")
(parse-integer b))
(("a" x "c" y "b")
(print (parse-integer x))
(print (parse-integer y))
(list (parse-integer x) (parse-integer y)))
(t (print "aa")))
so by using &body
instead of &rest
we get this indentation:
(str-match "a1c5b"
(("a" b "c")
(parse-integer b))
(("a" x "c" y "b")
(print (parse-integer x))
(print (parse-integer y))
(list (parse-integer x) (parse-integer y)))
(t (print "aa")))
Would you not use the Trivia library for pattern matching? It probably does this, and more.
What are users going to ask for pattern matching features after we introduce this one?
like some other languages' match case.
what are your favourite examples?
(and yes "string-match" might be better)
so by using &body instead of &rest we get this indentation:
Nice catch!
Would you not use the Trivia library for pattern matching? It probably does this, and more.
Gonna check it now.
I checked the trivia
it looks good when I am trying to pattern matching the list like
(trivia:match '(1 2 3)
((list* 1 x _)
x)
((list* _ x)
x)) ;; => 2
but I have an issue when I run the string pattern. I am not sure because I am using sbcl or not (maybe because this?)
beside, I can match the whole string like
(trivia:match "a1c5b" ("a1c5b" 1))
;; or
(trivia:match "ab" ((vector #\a #\b) 1))
but not these:
(trivia:match "a1c5b" ((string "a1c" "5b") 1))
so look like I can only binding char rather than the sub-string like my purposal
Let's use and try this macro. I'm interested in everybody's feedback.
A stupid test: I match like your example, but I don't use the matching variable, so I get style warnings:
(match "a1c5b"
(("a" i "c")
(print "got axc"))
(("a" x "c" y "b")
(print "got axcyb"))
(t (print "default"))
)
;; =>
;; ; The variable I is assigned but never read.
;; (and for x and y)
Would it be possible to avoid the warnings? Using a _
placeholder?
Yes, I just try on my side. Will give PR soon.
Gave the PR #114
I tried this more on an AOC problem (day 19), and OMG this match macro felt so powerful. Easier and faster than searching for the right regexp.
Other quick test:
(str::match "123 hello 456"
(("\\d+" s "\\d+")
s)
(t "nothing"))
;; =>" hello 45"
I didn't expect to see "45". The first number regex was correctly matched, not the second?
(str::match "123 hello 456"
(("\\d+" s "\\d*")
s)
(t "nothing"))
;; " hello 456"
here I didn't expect "456".
@vindarel Just figure out fixing this issue need to write the un-greedy regex. I just fix it in the latest commit. Good catch!
The PR #114 is merged, I am not sure if we keep this idea issue open or not for future potential changes. I left this decision to repo owner.
I was thinking if the
cl-str
can "pattern match" the string like some other languages' match case.So I write my own version (like example below), what do you guys think? Is that fit the cl-str's purpose? I checked the doc and there is a
string-case
. Should I change the name of the macro? Thanks!