-
-
Notifications
You must be signed in to change notification settings - Fork 3
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
define/curry
- Defines an automatically currying procedure
#5
Comments
I'm not very experienced writing Racket or macros, so any improvement suggestions are welcome. 😊 |
define/curry
- Defines an automatically currying procedure
This is a great little macro! Did you know, however, that there's another form for (define ((insert-between2 mid) left right)
(string-join (list left mid right) "")) The docs show that the |
Hi, Rotor! Thanks! Yes, I'm aware of that syntax. However as far as I understand it, that syntax doesn't allow you to do something like this. All of the following are equivalent: (insert-between "-" "left" "right")
((insert-between "-" "left") "right")
((insert-between "-") "left" "right")
((insert-between) "-" "left" "right") |
Oh wow you're totally right! |
@Fictitious-Rotor I updated with a link to the |
Looks great! Small suggestion: move the syntax class to the toplevel. It'll have to go inside a Annoying plumbing suggestion: use |
Hi, @bennn! Thanks! I tried your suggestions. As for (require (for-syntax syntax/parse))
(begin-for-syntax
(define-syntax-class name-params
#:description "name and parameters clause"
(pattern (name:id params:id ...+)
#:fail-when (check-duplicate-identifier
(syntax->list #'(params ...)))
"duplicate parameter name")))
(define-syntax (define/curry stx)
(syntax-parse stx
[(_ np:name-params body ...+)
#'(define np.name
(curry (λ (np.params ...)
body ...)))]))
(define/curry (insert-between mid left right)
(string-join (list left mid right) ""))
(insert-between 2 3 3 4 4 44) ; This line gets highlighted with an error |
Oh, that's great that the line with the function call gets highlighted. When I run on the command line, though, it prints the function and says that function is defined on line 17 (inside the
Maybe a better way to phrase it is: get If the definition of |
Yeah you're right… I tried using |
Hint: the |
@bennn Sorry for taking so long! I got a bit busy and distracted, but I tried your hint just now and it worked great! Thanks for the tip. |
Macro
Defines an automatically currying procedure in a single step. Uses
curry
internally.Example
Before and After
This code-cleaning macro reduces some boilerplate necessary to define a curried function. Code like this:
Is simplified to this, identical in form to defining a regular procedure:
I wrote an earlier version of this macro when implementing some Haskell code in Racket, and thought it was useful enough to share!
Licence
I release the above code under the MIT license, and accompanying text under the Creative Commons Attribution 4.0 International license.
The text was updated successfully, but these errors were encountered: