jackfirth / jackfirth/resyntax
Suggest `match` with `regexp` instead of `regexp-match`
Nobody has claimed this yet.
- Dominant language
- Racket
- Stars
- 70
- Forks
- 11
- PR merge metrics
- No merged PRs in 30d
Description
In this code, the use of cond and regexp-match can be expressed more cleanly using match with the regexp pattern. This probably shouldn't be rewritten unless 1) every non-else branch of the cond is using the lambda-based form and 2) all of the match patterns use define to name the extracted groups, if there are multiple groups. So this test case ought to pass:
#lang resyntax/test
require: resyntax/default-recommendations default-recommendations
header:
- #lang racket
test: "original code should be refactorable to new code"
--------------------
(define re #rx"^[ \t\n]*([0-9]+)[ \t\n]*,(.*)$")
(define (f str)
(cond
[(regexp-match re str)
=>
(lambda (m)
(define a (cadr m))
(define b (caddr m))
(list a b))]
[else 'else]))
====================
(define re #rx"^[ \t\n]*([0-9]+)[ \t\n]*,(.*)$")
(define (f str)
(match str
[(regexp re (list _ a b)) (list a b)]
[else 'else]))
--------------------
There are some similar cases in DrRacket's codebase but refactoring all of them is questionable. The cases where there's multiple capture groups and none of them have named variables associated with them ought to be left alone.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the linked drracket/browser/private/html.rkt examples and the supplied resyntax/test case, then find the recommendation implementation and existing tests for regexp-match and match patterns. Done means the shown test passes while cases with unnamed multiple capture groups or non-lambda branches remain unrecommended.
Written by the indexing model from the issue text.
Assessment
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100