Add anaphoric map and filter (squashed commit)
This commit is contained in:
parent
2467143b69
commit
3d7ca1f5ac
13
afilter.rkt
Normal file
13
afilter.rkt
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
#lang racket/base
|
||||||
|
|
||||||
|
(provide afilter it)
|
||||||
|
(require anaphoric/it
|
||||||
|
racket/stxparam
|
||||||
|
(for-syntax racket/base))
|
||||||
|
|
||||||
|
(define-syntax-rule (afilter body lst)
|
||||||
|
(let ([func
|
||||||
|
(λ (var)
|
||||||
|
(syntax-parameterize ([it (make-rename-transformer #'var)])
|
||||||
|
body))])
|
||||||
|
(filter func lst)))
|
13
amap.rkt
Normal file
13
amap.rkt
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
#lang racket/base
|
||||||
|
|
||||||
|
(provide amap it)
|
||||||
|
(require anaphoric/it
|
||||||
|
racket/stxparam
|
||||||
|
(for-syntax racket/base))
|
||||||
|
|
||||||
|
(define-syntax-rule (amap body lst)
|
||||||
|
(let ([func
|
||||||
|
(λ (var)
|
||||||
|
(syntax-parameterize ([it (make-rename-transformer #'var)])
|
||||||
|
body))])
|
||||||
|
(map func lst)))
|
|
@ -2,7 +2,7 @@
|
||||||
@require[@for-label[anaphoric
|
@require[@for-label[anaphoric
|
||||||
racket/base]]
|
racket/base]]
|
||||||
|
|
||||||
@title{Anaphoric conditionals}
|
@title{Anaphoric macros}
|
||||||
@author[@author+email["Suzanne Soy" "racket@suzanne.soy"]]
|
@author[@author+email["Suzanne Soy" "racket@suzanne.soy"]]
|
||||||
|
|
||||||
@defmodule[anaphoric]
|
@defmodule[anaphoric]
|
||||||
|
@ -10,7 +10,7 @@
|
||||||
@section{Overview}
|
@section{Overview}
|
||||||
|
|
||||||
This package provides anaphoric versions of @racket[if],
|
This package provides anaphoric versions of @racket[if],
|
||||||
@racket[when] and @racket[cond]. These bind the syntax
|
@racket[when], @racket[cond], @racket[map], and @racket[filter]. These bind the syntax
|
||||||
parameter @racket[it] to the value produced by the
|
parameter @racket[it] to the value produced by the
|
||||||
condition expression.
|
condition expression.
|
||||||
|
|
||||||
|
@ -155,3 +155,30 @@ using @racket[it].
|
||||||
@racket[conditionᵢ] is successful.
|
@racket[conditionᵢ] is successful.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@section{Anaphoric map and filter}
|
||||||
|
|
||||||
|
@defform[[(amap body lst)]]{
|
||||||
|
Anaphoric @racket[map]. Binds the syntax parameter @racket[it]
|
||||||
|
in the @racketid[body], and maps it over the list @racketid[lst]. Effectively the same
|
||||||
|
as wrapping the @racketid[body] in a @racket[lambda] with an @racket[it] parameter. Unlike @racket[map], @racket[amap]
|
||||||
|
only works on a single list.
|
||||||
|
|
||||||
|
@racket[amap] works with nested function calls:
|
||||||
|
|
||||||
|
@racketblock[(amap (string-append (string-upcase it) "!")
|
||||||
|
'("apple" "banana"))]
|
||||||
|
|
||||||
|
The syntax parameter @racket[it] may be used multiple times in the procedure:
|
||||||
|
|
||||||
|
@racketblock[(amap (* it it) '(1 2 3))]
|
||||||
|
}
|
||||||
|
|
||||||
|
@defform[[(afilter body lst)]]{
|
||||||
|
Anaphoric @racket[filter]. Binds the syntax parameter @racket[it]
|
||||||
|
in the @racketid[body], and filters the list @racketid[lst] using it. Effectively the same
|
||||||
|
as wrapping the body in a @racket[lambda] with an @racket[it] parameter.
|
||||||
|
|
||||||
|
@racket[afilter] works with nested function calls:
|
||||||
|
|
||||||
|
@racketblock[(afilter ((* it it) . > . 50) lst)]
|
||||||
|
}
|
||||||
|
|
21
test/afilter-test.rkt
Normal file
21
test/afilter-test.rkt
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
#lang racket/base
|
||||||
|
|
||||||
|
(require anaphoric/afilter
|
||||||
|
rackunit)
|
||||||
|
|
||||||
|
(define lst '(5 6 7 8 9))
|
||||||
|
|
||||||
|
(check-equal?
|
||||||
|
'(6 8)
|
||||||
|
(afilter (odd? (add1 it)) lst)
|
||||||
|
"Nested function call.")
|
||||||
|
|
||||||
|
(check-equal?
|
||||||
|
'(8 9)
|
||||||
|
(afilter ((* it it) . > . 50) lst)
|
||||||
|
"Multiple 'it' in a nested expression.")
|
||||||
|
|
||||||
|
(check-equal?
|
||||||
|
'()
|
||||||
|
(afilter (= it 42) '())
|
||||||
|
"Empty list.")
|
19
test/amap-test.rkt
Normal file
19
test/amap-test.rkt
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
#lang racket/base
|
||||||
|
|
||||||
|
(require anaphoric/amap
|
||||||
|
rackunit)
|
||||||
|
|
||||||
|
(check-equal?
|
||||||
|
'(2 3)
|
||||||
|
(amap (add1 it) '(1 2))
|
||||||
|
"Sanity check.")
|
||||||
|
|
||||||
|
(check-equal?
|
||||||
|
'("2" "4")
|
||||||
|
(amap (number->string (+ it it)) '(1 2))
|
||||||
|
"Multiple 'it' in a nested expression.")
|
||||||
|
|
||||||
|
(check-equal?
|
||||||
|
'()
|
||||||
|
(amap (/ it 0) '())
|
||||||
|
"Empty list.")
|
Loading…
Reference in New Issue
Block a user