You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
2 weeks ago | |
---|---|---|
README.md | 2 weeks ago | |
main.slo | 2 weeks ago | |
module.json | 2 weeks ago |
README.md
structs
structs - a module to define and manipulate structs
api
creating a constructor
(struct [type: string] [args: list]) => lambda
testing whether something is a struct
(struct? [input: any]) => bool
accessing the type and data
(struct-data [input: struct]) => association-list
(struct-type [input: struct]) => string
accessing properties
(struct-props [input: struct]) => list
(has-prop? [input: struct] [property: any]) => bool
(get-prop [input: struct] [property: any]) => any
(set-prop [input: struct] [property: any] [value: any]) => struct
other interesting helpers
for association lists
(keys [input: association-list]) => list
(has-key? [input:association-list] [key:any]) => bool
(zip [list1: list] [list2: list] [[default: any]]) => association-list
for lists
(extend [list1: list] [list2: list]) => list
general
- `(assert [expr: expression] [message: string]) => ()|panic
usage
(define Person (struct "Person" ["name" "age" "gender"])) ; create a Person struct
(define jeff (Person "Jeff" 25 "Male")) ; jeff is a Person
(struct? jeff) ; => #t
(struct? 123) ; #f
; all structs are associated lists, but not all associated lists are structs
(assoc? jeff) ; => #t
(struct? [["key1" "value1] ["key2" "value2"]]) ; => #f
; access the "type" for comparisons,
; or get the associated-list of the data
(struct-data jeff) ; => (("name" "Jeff") ("age" 25) ("gender" "Male"))
(struct-type jeff) ; => "Person"
(struct-props jeff) ; => ("name" "age" "gender"), like (keys assoc-list)
(has-prop? jeff "name") ; => #t
(has-prop? jeff "occupation") ; => #f
(has-prop? [1 2 3] "name") ; => panic! not a struct
(get-prop jeff "name") ; => "Jeff"
(get-prop jeff "occupation") ; => panic! "occupation" isn't a valid property
(get-prop [1 2 3] "name") ; => panic! not a struct
(set-prop jeff "name" "Jeffrey") ; => a struct like 'jeff' with the altered name
(set-prop [1 2 3] "name" "Jeffrey") ; => panic! not a struct
; structs can't have new properties added to them
(set-prop jeff "occupation" "Programmer") ; => panic!
; however, they're just associated lists, so this can be worked around
; the below returns what you might expect the above to return
; probably don't do this, though
(assoc jeff STRUCT_DATA (assoc (assoc jeff STRUCT_DATA) "occupation" "Programmer"))