Created
November 10, 2024 22:27
-
-
Save gurzgri/79f04bf4d7313f08e1cd3d1ee38baae3 to your computer and use it in GitHub Desktop.
mutator-object
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Red [ | |
title: 'mutator-object | |
author: "Christian Ensel" | |
usage: [ | |
>> do %../mutator-object.red | |
>> circle: mutator-object [ | |
#set area: does [if number? new [set-quiet in self 'radius square-root divide new pi]] | |
#get area: does [multiply pi power radius 2] | |
radius: 10 | |
] | |
>> circle/radius ;== 10 | |
>> circle/area ;== 314.1592653589793 | |
>> circle/area: 200 ;== 200 | |
>> circle/radius ;== 7.978845608028654 | |
>> circle/area ;== 200.0 | |
] | |
] | |
mutator-object: function [ | |
"Makes a new object with setters/getters from an evaluated spec." | |
spec [block!] | |
][ | |
setters: parse spec [collect any [thru #set keep set-word! 'does keep block!] to end] | |
object: make object! append copy [on-change*: does []] spec | |
object/on-change*: func [word old new] bind/copy compose/only [switch word ( | |
new-line/skip/all collect [foreach [word body] setters [ | |
keep compose/deep [(to word! word) [set-quiet word :old (body)]] | |
]] on 2 | |
)] object | |
object | |
] | |
() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment