[plt-scheme] 299.402

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Sun Sep 18 08:56:35 EDT 2005

MzScheme and MrEd are now version 299.402 in the SVN repository trunk.

The only change is the addition of `current-preserved-thread-cell-values'.
When this procedure is called with no arguments, it captures the values
of all preserved thread cells (in the current thread). When the
procedure is called with one argument, the argument should be the
result of an earlier call, and the value of each preserved thread cell
is changed (in the current thread) to the previously captured value.
The capturing and restoring threads need not be the same.

Parameters are defined in terms of preserved thread cells, so the new
procedure captures parameter mutations, too.

The `current-preserved-thread-cell-values' procedure might be useful in
a servlet to capture parameter state along with a continuation. Below
is the standard "add" example, but when the title each page is
generated, it increments a parameter-based counter and includes the
value in the title. The counter counts from 1 to 3.

I don't know whether using `current-preserved-thread-cell-values' in
this way is a good idea, either for servlets or for other problems. But
I see little harm from exposing this extra bit of MzScheme
functionality, and maybe it will turn out to be useful.

Matthew

----------------------------------------

(require (lib "unitsig.ss")
         (lib "servlet-sig.ss" "web-server")
         (lib "date.ss"))

(define counter (make-parameter 0))

(unit/sig () (import servlet^)

  (define (my-send/suspend proc)
    (let ([vals (current-preserved-thread-cell-values)])
      (begin0
       (send/suspend proc)
       (current-preserved-thread-cell-values vals))))

  ; request-number : str -> num
  (define (request-number which-number)
    (string->number
     (extract-binding/single
      'number
      (request-bindings 
       (my-send/suspend (build-request-page which-number))))))

  ; build-request-page : str -> str -> response
  (define (build-request-page which-number)
    (counter (add1 (counter)))
    (lambda (k-url)
      `(html (head (title ,(format "Enter a Number to Add (~a)" (counter))))
             (body ([bgcolor "white"])
                   (form ([action ,k-url] [method "post"])
                         "Enter the " ,which-number " number to add: "
                         (input ([type "text"] [name "number"] [value ""]))
                         (input ([type "submit"] [name "enter"] 
                                 [value "Enter"])))))))

  (let ([v (+ (request-number "first") (request-number "second"))])
    (counter (add1 (counter)))
    `(html (head (title ,(format "Sum (~a)" (counter))))
	   (body ([bgcolor "white"])
		 (p "The sum is "
		    ,(number->string v))))))



Posted on the users mailing list.