8.18
top
← prev up next →

Struct-type properties with a struct-like interfaceπŸ”— i

Source code: https://github.com/AlexKnauth/struct-like-struct-type-property.

syntax

prop-option...)
prop-option = #:propertyprop-exprval-expr
Defines these identifiers:

  • prop:name, a struct-type property

  • name, a constructor for a struct that implements the property

  • name?, a predicate for values that have the property

  • name-field... , an accessor for each field

The property prop:name expects a function that takes a "self" argument and returns a name result. When someone wants to use the value as a prop:name, this function should construct a more basic structure that contains the field values.

Examples:
> (require struct-like-struct-type-property)
> (foo-a(foo123))

1

> (foo?(foo"a""b""c"))

#t

> (struct bar[x]
#:propertyprop:foo;when a bar is used as a foo, do this
(lambda (self)
(foo(first (bar-xself))
(second (bar-xself))
(third (bar-xself)))))
> (foo-a(bar(list 456)))

4

> (foo?(bar(list 'd'e'f)))

#t

> (match (bar(list 135))
[(fooabc)c])

5

The #:propertyprop-exprval-expr options specify super-properties. Anything that implements prop:name will automatically implement all the properties specified by the given prop-exprs.

Examples:
> (require struct-like-struct-type-property)
#:propertyprop:procedure
(λ (selfx)
(+ (* (quadratic-aself)(sqr x))
(* (quadratic-bself)x)
(quadratic-cself))))
> (define f(quadratic1-2-3))
> (f0)

-3

> (f1)

-4

> (f2)

-3

> (f3)

0

> (f4)

5

> (struct vertex-form[avertex]
#:propertyprop:quadratic
(λ (self)
(match (vertex-form-vertexself)
[(list hk)
(define a(vertex-form-aself))
(define b(* -2ah))
(define c(+ (* a(sqr h))k))
(quadraticabc)])))
> (define g(vertex-form1'(1-4)))
> (g0)

-3

> (g1)

-4

> (g2)

-3

> (g3)

0

> (g4)

5

top
← prev up next →

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /