Skip to content

Instantly share code, notes, and snippets.

@ClarkeRemy
Last active January 5, 2023 10:43
Show Gist options
  • Save ClarkeRemy/1e8a99c6a19baebbf481602eee19e979 to your computer and use it in GitHub Desktop.
Save ClarkeRemy/1e8a99c6a19baebbf481602eee19e979 to your computer and use it in GitHub Desktop.
Unique Object
NB. for reference ( https://code.jsoftware.com/wiki/Guides/Lexical_Closure )
coclass 'Unique'
lines =. {{ <;._2 m }}
NB. format : 'ref proof' =: input Unique message
NB. on drop no proof is returned
proof =.{{)n
'message own'=. y
if. proof__m ~: own
do.
if. -. init__m
do. init__m =: 1 [ proof__m =: own
else. echo 'not the owner, no-op' return. end.
end.
proof__m =: >: proof__m
}} lines
dispatch_table_m=.{{)n
select. message
case. 'inc' do. state__m ; proof__m [ state__m=: state__m + 1 return.
case. 'dec' do. state__m ; proof__m [ state__m=: state__m - 1 return.
case. 'drop' do. tmp [ coerase m [ tmp=. state__m return.
end.
echo 'unknown monad message : ',: ": y
}} lines
dispatch_table_d=.{{)n
select. message
case. 'inc' do. state__m ; proof__m [ state__m=: state__m + x return.
case. 'dec' do. state__m ; proof__m [ state__m=: state__m - x return.
end.
echo 'unknown dyad message : ',: ": y
}} lines
dispatch =: proof , dispatch_table_m , (<':') , proof , dispatch_table_d
new =: {{)a
obj =. cocreate ''
state__obj =: m
proof__obj =: i. 0 0
init__obj =: 0
obj (adverb : dispatch)
}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment