Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/alexknauth/struct-like-struct-type-property
Creating struct-type properties with a struct-like interface
https://github.com/alexknauth/struct-like-struct-type-property
Last synced: about 1 month ago
JSON representation
Creating struct-type properties with a struct-like interface
- Host: GitHub
- URL: https://github.com/alexknauth/struct-like-struct-type-property
- Owner: AlexKnauth
- License: mit
- Created: 2018-06-05T19:54:54.000Z (over 6 years ago)
- Default Branch: main
- Last Pushed: 2022-01-07T14:24:50.000Z (almost 3 years ago)
- Last Synced: 2024-10-16T02:55:19.985Z (3 months ago)
- Language: Racket
- Size: 13.7 KB
- Stars: 5
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# struct-like-struct-type-property
Creating struct-type properties with a struct-like interface.[_Documentation_](https://docs.racket-lang.org/struct-like-struct-type-property/index.html).
```racket
(require struct-like-struct-type-property)
``````racket
(define-struct-like-struct-type-property name [field ...]
prop-option ...)
prop-option = #:property prop-expr val-expr
```Defines these identifiers:
- `prop:name`
- `name`
- `name?`
- `name-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.Example:
```racket
(define-struct-like-struct-type-property foo [a b c])> (foo-a (foo 1 2 3))
1
> (foo? (foo "a" "b" "c"))
#true
> (struct bar [x]
#:property prop:foo
(lambda (self)
(foo (first (bar-x self))
(second (bar-x self))
(third (bar-x self)))))
> (foo-a (bar (list 4 5 6)))
4
> (foo? (bar (list 'd 'e 'f)))
#true
> (match (bar (list 1 3 5))
[(foo a b c) c])
5
```The `#:property prop-expr val-expr` options specify
super-properties. Anything that implements `prop:name` will
automatically implement all the properties specified by the
given `prop-exprs`.Example:
```racket
(define-struct-like-struct-type-property quadratic [a b c]
#:property prop:procedure
(λ (self x)
(+ (* (quadratic-a self) (sqr x))
(* (quadratic-b self) x)
(quadratic-c self))))> (define f (quadratic 1 -2 -3))
> (f 0)
-3
> (f 1)
-4
> (f 2)
-3
> (f 3)
0
> (f 4)
5
```