Artifact ID: | d6e272adc973afa804ae93733de1c5b168f742340b54dde21ea3477cec689ab8 |
---|---|
Page Name: | struct.scm |
Date: | 2019-03-30 05:59:13 |
Original User: | lexi |
Next | ad63118aeb09738c34bc773d6382e22c5388c1c1103deff38dcde75fdf7c0330 |
functional structures in scheme
struct.scm
contains a function to generate functional "structs." these structs are immutable and are implemented as lambdas. you can create a new "struct type" by calling the (struct)
function. consider the following code:
(define user (struct 'name 'pw 'rank 'email 'age 'attunements))
(define juan (user "juán peralta" "p@labra-secr3ta" "special agent" "j.peralta@tla.gov" 69
'("hushed whispers" "the unconquered wilderness" "careful duplicity")))
this code first creates a struct type, characterized by the function (user)
. when (user)
is called on the correct number of arguments, it generates a new record of that type. the new record is itself a function. to access a record, you simply call the function with the name of the field to look up. the library also supports what's known as "functional record update syntax," a pattern in which a new struct is returned with different values for named fields. this library currently only supports updating one record at a time, but you can get around this through currying.
(juan) ; → '("juán peralta" "p@labra-secr3ta" "special agent" "j.peralta@tla.gov" 69
'("hushed whispers" "the unconquered wilderness" "careful duplicity"))
(juan 'email) ; → "j.peralta@tla.gov"
(juan 'attunements) ; → '("hushed whispers" "the unconquered wilderness" "careful duplicity")
(define juan-with-new-pw (juan 'pw "pendej0-supremo"))
(juan-with-new-pw 'name) ; → "juán peralta"
(juan-with-new-pw 'pw) ; → "pendej0-supremo"
(define clarabetta ((juan 'name "clarabetta kincaid") 'email "c.kincaid@tla.gov))
(clarabetta 'email) ; → "c.kincaid@tla.gov"
structs, as you can see above, can also be called without arguments to return a list of all the data they contain. the code is fairly straightforward, implemented completely without macros, and shouldn't be difficult to understand for anyone familiar with functional programming. it's also in my experience tremendously useful, simplifying a number of tasks that would otherwise be unnecessarily complicated in Scheme compared to C, and has the advantage of field names being expressions - you could just as easily call a function to return the appropriate field name as write one in yourself.