Skip to content

Commit

Permalink
Made values from data/maybe and data/either serializable with racket/…
Browse files Browse the repository at this point in the history
…serialize
  • Loading branch information
LiberalArtist authored and lexi-lambda committed May 8, 2017
1 parent 5c6f94f commit e1ed01a
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 7 deletions.
7 changes: 5 additions & 2 deletions functional-doc/scribblings/data/functional.scrbl
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,9 @@ single line of explicit error handling code.
@defproc[(just? [v any/c]) boolean?]
@defproc[(nothing? [v any/c]) boolean?])]{
Value constructors and predicates for @tech{optional values}. The @racket[just] function produces a
boxed value, and the @racket[nothing] value represents the absence of a value.
boxed value, and the @racket[nothing] value represents the absence of a value. Optional values
can be serialized with @racketmodname[racket/serialize] (as long as any nested value is
serializable).

@(functional-interaction
(just 'hello)
Expand Down Expand Up @@ -672,7 +674,8 @@ using @tech{either}.
@defproc[(failure? [v any/c]) boolean?])]{
Value constructors and predicates for @tech{either}, which are tagged @tech{optional values}. The
@racket[success] function produces a successful value, and the @racket[failure] constructor creates a
value that represents failure.
value that represents failure. Success and failure values can be serialized using
@racketmodname[racket/serialize] as long as the inner values are serializable.

@(functional-interaction
(success 'hello)
Expand Down
6 changes: 3 additions & 3 deletions functional-lib/data/either.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
(require racket/require
(prefix-in c: data/collection)
(multi-in data [functor applicative monad maybe])
(multi-in racket [contract generic match])
(multi-in racket [contract generic match serialize])
(for-syntax racket/base
syntax/parse))

Expand All @@ -21,7 +21,7 @@
(define (either? x)
(or (success? x) (failure? x)))

(struct success (value)
(serializable-struct success (value)
#:transparent
#:methods gen:functor
[(define (map f x)
Expand All @@ -36,7 +36,7 @@
[(define (chain f x)
(f (success-value x)))])

(struct failure (value)
(serializable-struct failure (value)
#:transparent
#:methods gen:functor
[(define (map f x) x)]
Expand Down
16 changes: 14 additions & 2 deletions functional-lib/data/maybe.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
(require racket/require
(prefix-in c: data/collection)
(multi-in data [functor applicative monad])
(multi-in racket [contract function generic match])
(multi-in racket [contract function generic match serialize])
(for-syntax racket/base
syntax/parse))

Expand All @@ -21,7 +21,7 @@
(define (maybe? x)
(or (just? x) (nothing? x)))

(struct just (value)
(serializable-struct just (value)
#:transparent
#:methods gen:functor
[(define (map f x)
Expand All @@ -40,6 +40,12 @@
(define nothing-value
(let ()
(struct nothing ()
#:property prop:serializable
(make-serialize-info (λ _ #())
#`deserialize-info:nothing-v0
#f
(or (current-load-relative-directory)
(current-directory)))
#:methods gen:custom-write
[(define (write-proc x out mode)
(display "#<nothing>" out))]
Expand All @@ -54,6 +60,12 @@
(define nothing-value (nothing))
nothing-value))

(define deserialize-info:nothing-v0
(make-deserialize-info (λ _ nothing-value)
(λ _ (error '|nothing: can't have cycles|))))
(module+ deserialize-info
(provide deserialize-info:nothing-v0))

(define-match-expander nothing
(syntax-parser [(_) #'(== nothing-value)])
(syntax-parser [(_ . args) #'(nothing-value . args)]
Expand Down
5 changes: 5 additions & 0 deletions functional-test/tests/data/either.rkt
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
#lang racket/base

(require racket/require
racket/serialize
(multi-in data [functor applicative monad maybe either])
rackunit
rackunit/spec)

(describe "either"
(it "is serializable"
(check-equal? (deserialize (serialize (success 12))) (success 12))
(check-equal? (deserialize (serialize (failure 12))) (failure 12)))

(describe "map"
(it "maps over the internal value for success"
(check-equal? (map add1 (success 12)) (success 13)))
Expand Down
5 changes: 5 additions & 0 deletions functional-test/tests/data/maybe.rkt
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
#lang curly-fn racket/base

(require racket/require
racket/serialize
(multi-in data [functor applicative monad maybe])
racket/match
rackunit
rackunit/spec)

(describe "maybe"
(it "is serializable"
(check-eq? (deserialize (serialize nothing)) nothing)
(check-equal? (deserialize (serialize (just 3))) (just 3)))

(describe "map"
(it "maps over the internal value for just"
(check-equal? (map add1 (just 12)) (just 13)))
Expand Down

0 comments on commit e1ed01a

Please sign in to comment.