browse_stuff(): # criteria = yield(ask_criteria()) # results = find_stuff(criteria) # , - yield (list_stuff(results)) # raise StopIteration() def buy_stuff(continuation, item) if not user.authentificated: # , , user = yield(login_form(user)) # , , # , quantity = yield(ask_quantity()) # , delivery = yield(ask_delivery()) # money = yield(ask_payment()) # status = launch_order(item,quantity,delivery,money) # "" yield(message(" . .")) # , ( browse_stuff) continuation.next() # , " " ?
Upd:
It is worth noting that neither in python, nor tembolee in pkhp, nor in Zhaveh, there is no full support for sequels, at the level of first-class objects.
Python's yield behaves very similarly and was used to illustrate an idea.
How it will work in reality is not entirely clear.
Full support for continuations is available in various exotic languages , but also in Ruby as well.
There are several servers with continuation support for the scheme, lisp, smalltalk, OCaml and JavaScript.
Somewhere in the comments a link to a simple PHP emulator was lost.
mega-UPD: illustration of the scheme
I can not vouch for the correctness of the code. but the brackets are balanced :)
;; server code (define (uri-> cont uri) "get from where the continuation by its URI") (define (cont-> uri cont) "saves a continuation somewhere and forms URIs for it) (define (insert-uri tmpl uri) "inserts a link into a template") (define (render-page tmpl args) "renders the page in html") (define (handle-request request) "causes a continuation, passing request to it" (if (eqv? (get-uri request) init-uri)); if the request is a starting URI (start); cause start ((uri-> cont (get-uri request)) request))); otherwise - continued (define (make-response cont template) "template: a page with links or a form. (render-page (insert-uri template (cont-> uri cont)))) ;; end of server code ;; application code (define (quest room) (define (parse-request request) "parses the form data or chonit and returns the choice") (define (walk-on choice) "selects a new room for the current and choice") (define (get-page) "returns the html template for the current room") (define (response cc) (make-response cc (get-page)) "stupid parameter karring") (quest (walk-on (parse-request (call / cc response)))))) ;; initialization code (define init-uri "/ mytextquest") (define (start) (quest 'start-room))
What is going on here (should be happening):
0. quest is called with a parameter identifying the room.
0.5 the only call is the last, the most extreme parameter - the expression (call / cc (response))
1. call / cc (this is a special construct that) calls a function (response continuation)
2. that in turn calls (make-response continuation template)
3. make-response inserts into the template (for example, in the action field, or in the navigation links) a URI identifying this continuation.
4. page rendered
5. The user clicks on one of the links on the page or submits the form.
6. handle-request by clicking on the link continued
7. and calls it with the request parameter
8. execution continues from the point where call / cc stands. the request value is substituted as the result of the call / cc call
9. request is parsed and we get the logic of what the user there has poked
10. walk-on calculates which room the user is in now
11. quest recursively called with a parameter that identifies the next room.
If, besides the room, the karma of the user influences the choice of path the contents of the pockets. and the state of the surrounding ecology - all this is encapsulated in a 'room'.
If the user pressed 'back' and switched to the previous URI, the previous state is pulled out and it really falls into the previous room, all the affected animals come alive, and so on.
Source: https://habr.com/ru/post/50204/