{"size": 1671, "ext": "scrbl", "max_stars_repo_path": "web-server-doc/web-server/scribblings/closure.scrbl", "max_stars_repo_name": "jessealama/web-server", "max_stars_repo_stars_event_min_datetime": "2015-01-21T08:11:58.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-20T08:28:39.000Z", "max_issues_repo_path": "web-server-doc/web-server/scribblings/closure.scrbl", "max_issues_repo_name": "jessealama/web-server", "max_issues_repo_issues_event_min_datetime": "2015-01-14T08:06:37.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-12T14:38:40.000Z", "max_forks_repo_path": "web-server-doc/web-server/scribblings/closure.scrbl", "max_forks_repo_name": "jessealama/web-server", "max_forks_repo_forks_event_min_datetime": "2015-03-05T15:07:36.000Z", "max_forks_repo_forks_event_max_datetime": "2021-07-30T18:59:25.000Z"}
{"max_stars_count": 82, "max_issues_count": 105, "max_forks_count": 37, "avg_line_length": 26.109375, "max_line_length": 90, "alphanum_fraction": 0.6618791143}
#lang scribble/doc
@(require "web-server.rkt")

@title[#:tag "closure"]{Serializable Closures}
@(require (for-label racket/serialize
                     web-server/lang/closure
                     web-server/lang/serial-lambda
                     web-server/private/define-closure))

The defunctionalization process of the Web Language (see
@secref["stateless" #:doc '(lib "web-server/scribblings/web-server.scrbl")])
requires an explicit representation of closures that is serializable.

@defmodule[web-server/lang/serial-lambda]{

@defform[(serial-lambda formals body ...)]{
  Returns @racket[(lambda formals body ...)], except it is serializable.
}

@defform[(serial-case-lambda [formals body ...] ...)]{
 Returns @racket[(case-lambda [formals body ...] ...)], except it is
 serializable.
}

}

@section[#:style 'hidden]{Definition Syntax}

@defmodule[web-server/private/define-closure]{

@defform[(define-closure tag formals (free-var ...) body)]{
 Defines a closure, constructed with @racket[make-tag] that accepts a closure that returns
 @racket[freevar ...], that when invoked with @racket[formals]
 executes @racket[body].
}

Here is an example:
@racketmod[
 racket
(require racket/serialize)

(define-closure foo (a b) (x y)
  (+ (- a b)
     (* x y)))

(define f12 (make-foo (lambda () (values 1 2))))
(serialize f12)
#,(racketresult '((1) 1 (('page . foo:deserialize-info)) 0 () () (0 1 2)))
(f12 6 7)
#,(racketresult 1)
(f12 9 1)
#,(racketresult 10)

(define f45 (make-foo (lambda () (values 4 5))))
(serialize f45)
#,(racketresult '((1) 1 (('page . foo:deserialize-info)) 0 () () (0 4 5)))
(f45 1 2)
#,(racketresult 19)
(f45 8 8)
#,(racketresult 20)
]

}
