add struct-lens

closes https://github.com/jackfirth/lenses/issues/32
This commit is contained in:
AlexKnauth 2015-07-05 22:59:05 -04:00
parent d0e11af392
commit 71b070749e
2 changed files with 34 additions and 1 deletions

View File

@ -6,7 +6,9 @@
(define deps
'("base"
"rackunit-lib"
"fancy-app"))
"fancy-app"
"alexis-util"
))
(define build-deps

31
lenses/struct-lens.rkt Normal file
View File

@ -0,0 +1,31 @@
#lang racket/base
(provide struct-lens)
(require racket/local
syntax/parse/define
alexis/util/struct
"main.rkt"
(for-syntax racket/base
syntax/parse
racket/syntax
))
(module+ test
(require rackunit fancy-app (only-in lenses lens-transform*)))
(define-simple-macro (struct-lens s:id fld:id)
#:with s-fld (format-id #'s "~a-~a" #'s #'fld #:source #'fld)
#:with s-fld-set (format-id #'s "~a-~a-set" #'s #'fld #:source #'fld)
(local [(define-struct-updaters s)]
(make-lens s-fld s-fld-set)))
(module+ test
(struct foo (a b c) #:transparent)
(define foo-a-lens (struct-lens foo a))
(define foo-b-lens (struct-lens foo b))
(define foo-c-lens (struct-lens foo c))
(define f (foo 1 2 3))
(check-equal? (lens-transform* f foo-a-lens (* 100 _)) (foo 100 2 3))
(check-equal? (lens-transform* f foo-b-lens (* 100 _)) (foo 1 200 3))
(check-equal? (lens-transform* f foo-c-lens (* 100 _)) (foo 1 2 300))
)