1 things
(require qtops/things) | package: qtops |
procedure
(create-core-thing) → procedure?
A thing is a procedure that accepts a symbol and conditionally more arguments, where the symbol is a procedure the thing has and the additional arguments are applied to that procedure.
A thing has a procedure if it is in its internal record, a hash-table of symbols and procedures.
A thing created by create-core-thing have one procedure in its record, the ’call~ procedure, which provides direct access to the thing’s record.
(Things’ procedures often have suffixes like ! or ~ which represent information about the procedure’s (potential) side-effects. See the documentation on qualities for details.)
> (define stone (create-core-thing)) > stone #<procedure:t>
> (stone 'call~~ (λ (r) (hash-set! r 'name (λ () "stone")))) > (stone 'name) "stone"
> (stone 'call~~ (λ (r) r))
'#hash((call~~ . #<procedure:...gs/qtops/things.rkt:145:23>)
(name . #<procedure>))
The procedures held within things follow certain conventions: not violating Racket’s own, but extending it. Procedures might have one of the four following suffixes. (Suffices?)
! means a procedure changes that thing
!! means a procedure changes that and at least another thing
^! means a procedure doesn’t change itself, but does change another thing.
~ means a procedure might change that thing
~~ means a procedure might change that and at least another thing.
You might see other combinations, like ~!, whose meanings can hopefully be deduced from the list above.
Procedures outside things, but affecting them, follow other conventions, and might have one of these prefixes:
> means a procedure returns a procedure to be added to a thing’s procedures. These always take a thing as their first argument.
>> means a procedure returns multiple procedures to be added to a thing’s procedures. These also always take a thing as their first argument.
<> means a procedure returns a thing, after doing something to it - usually calling one or more procedures of the first two types here.
procedure
(create-thing given-name additional-procedures) → procedure? given-name : string? additional-procedures : (listof procedure?)
> (define pear (create-thing "pear")) > (pear 'name) "pear"
> (pear 'set-procedure! 'fall (λ () (printf ((pear 'prerender-string (list 'name " falls.")))))) > (pear 'fall) pear falls.
> (define milkweed (create-thing "milkweed" (list (λ (t) (list (cons 'pop (λ () (printf "Pop!")))))))) > (milkweed 'pop) Pop!
procedure
(set-procedure! t) → procedure?
t : procedure?
> (stone 'call~~ (λ (r) (hash-set! r 'set-procedure! (>set-procedure! stone))))
> (stone 'set-procedure! 'roll (λ () "The stone rolls.")) > (stone 'roll) "The stone rolls."
procedure
(procedures t) → procedure?
t : procedure?
> (stone 'set-procedure! 'procedures (>procedures stone)) > (stone 'procedures)
'#hash((call~~ . #<procedure:...gs/qtops/things.rkt:145:23>)
(name . #<procedure>)
(procedures . #<procedure:...gs/qtops/things.rkt:18:2>)
(roll . #<procedure>)
(set-procedure! . #<procedure:...gs/qtops/things.rkt:45:2>))
procedure
(procedure t) → procedure?
t : procedure?
> (stone 'set-procedure! 'procedure (>procedure stone)) > (stone 'procedure 'roll) #<procedure>
> ((stone 'procedure 'roll)) "The stone rolls."
> (stone 'procedure 'fake-procedure) stone: contract violation
expected: thing with fake-procedure procedure
result: "stone with (name call~~ roll set-procedure!
procedures procedure) procedures"
procedure
(set-procedures! t) → (procedure?)
t : procedure?
> (stone 'set-procedure! 'set-procedures! (>set-procedures! stone))
> (stone 'set-procedures! (list (cons 'mass (λ () 10)) (cons 'set-mass! (λ (m) (stone 'set-procedure! 'mass (λ () m)))))) > (stone 'mass) 10
> (stone 'set-mass! 20) > (stone 'mass) 20
procedure
(remove-procedure! t) → (procedure?)
t : procedure?
> (stone 'set-procedure! 'remove-procedure! (>remove-procedure! stone)) > (stone 'roll) "The stone rolls."
> (stone 'remove-procedure! 'roll) > (stone 'roll) thing: contract violation
expected: #<procedure> is missing procedure roll
given: '(name call~~ set-procedure! procedures procedure
set-mass! remove-procedure! set-procedures! mass)
procedure
(has-procedure? t) → (procedure?)
t : procedure?
> (stone 'set-procedure! 'has-procedure? (>has-procedure? stone)) > (stone 'has-procedure? 'roll) #f
> (stone 'has-procedure? 'crack) #f
procedure
(with-procedure~~ t) → (procedure?)
t : procedure?
> ((pear 'with-procedure~~ 'set-mass!) 135 #:alternate (printf "Pear ain't got set-mass!")) Pear ain't got set-mass!
> (stone 'set-procedure! 'with-procedure~~ (>with-procedure~~ stone))
> ((stone 'with-procedure~~ 'set-mass!) 2700) > (stone 'mass) 2700