|(require glass/prism)||package: glass|
A prism is a type of optic for focusing on a specific kind of subject and ignoring other kinds. A prism is built from a matching function, which focuses on subjects of the correct kind, and a casting function, which transforms a replacement focus back into the subject.
(define number-string-prism (make-prism (λ (s) (define num (string->number s)) (if num (present num) absent)) number->string #:name 'number-string-prism))
> (prism-match number-string-prism "124")
> (prism-match number-string-prism "elephant")
> (prism-cast number-string-prism 100)
> (prism-match success-prism (success 123))
> (prism-match success-prism (failure "oh no!"))