-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
from syntax-objects/Summer2021#5 cc @agj
- Loading branch information
Showing
4 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#lang racket/base | ||
(module+ test | ||
(require rackunit racket/string syntax-parse-example/define-curry/define-curry) | ||
|
||
(test-case "insert-between" | ||
(define/curry (insert-between mid left right) | ||
(string-join (list left mid right) "")) | ||
|
||
(define dash-between (insert-between "-")) | ||
|
||
(check-equal? (dash-between "left" "right") "left-right") | ||
(check-equal? (((insert-between "-") "left") "right") "left-right") | ||
|
||
(check-true | ||
(string-suffix? (symbol->string (object-name insert-between)) | ||
"6:4")) | ||
|
||
(check-exn #rx"arity mismatch" ;; object name | ||
(lambda () (insert-between 2 3 3 4 4 44)))) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#lang racket | ||
(provide define/curry) | ||
(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 | ||
#,(syntax/loc stx | ||
(λ (np.params ...) body ...))))])) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#lang syntax-parse-example | ||
@require[ | ||
(for-label racket/base syntax/parse syntax-parse-example/define-curry/define-curry)] | ||
|
||
@(define define-curry-eval | ||
(make-base-eval '(require racket/string syntax-parse-example/define-curry/define-curry))) | ||
|
||
@title{@tt{define/curry}} | ||
@stxbee2021["agj" 5] | ||
|
||
@; ============================================================================= | ||
|
||
@defmodule[syntax-parse-example/define-curry/define-curry]{} | ||
|
||
@defform[(define/curry (fn-id arg-id ...+) body ...+)]{ | ||
Defines an automatically currying procedure. | ||
|
||
@examples[#:eval define-curry-eval | ||
(define/curry (insert-between mid left right) | ||
(string-join (list left mid right) "")) | ||
(define dash-between (insert-between "-")) | ||
(dash-between "left" "right") | ||
] | ||
|
||
The macro uses @racket[syntax/loc] to give newly-defined functions | ||
names that point to their definition rather than to the macro body. | ||
|
||
@racketfile{define-curry.rkt} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters