On this page:
create-core-thing
create-thing
set-procedure!
procedures
procedure
set-procedures!
remove-procedure!
has-procedure?
with-procedure~~
8.2

1 things

 (require qtops/things) package: qtops

procedure

(create-core-thing)  procedure?

Creates a basic thing.

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.)

Examples:
> (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?)

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:

procedure

(create-thing given-name    
  additional-procedures)  procedure?
  given-name : string?
  additional-procedures : (listof procedure?)
Creates a core thing and Creates a hash-table of symbols and procedures, and returns a procedure which references that record.

Examples:
> (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?
Returns a procedure which accepts a symbol and procedure as arguments, and registers them as a new key and value in t’s hash-table.

Examples:
> (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?
Returns a procedure for t that queries its procedures. Used by create-thing to give things a 'procedures quality.

Examples:
> (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?
Returns a procedure for t that accepts one symbol and returns the corresponding procedure from t.

Examples:
> (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?
Returns a procedure for t that sets up new procedures for it. The returned procedure expects a list of pairs, each pair containing a symbol and procedure.

Examples:
> (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?
Returns a procedure for t that removes a procedure from it. That returned procedure expects a symbol.

Examples:
> (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?
Returns a procedure which accepts a symbol and returns a boolean indicating whether t has the provided symbol as a procedure.

Examples:
> (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?
Returns a procedure for t, that accepts one symbol as an argument and returns yet another procedure. If that symbol is a procedure t has, the returned procedure passes any arguments it gets to it. Otherwise, if an #:alternate keyword is provided, that is returned.

Examples:
> ((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