Skip to content

Instantly share code, notes, and snippets.

@snipsnipsnip
Created June 14, 2014 17:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save snipsnipsnip/6f00cab377bb74b6b6cd to your computer and use it in GitHub Desktop.
Save snipsnipsnip/6f00cab377bb74b6b6cd to your computer and use it in GitHub Desktop.
attr_reader, attr_writer and attr_accessor in J
coclass 'attr'
attr_reader =: monad define
loc =. > coname ''
names =. ;: y
dup =. ((# names) & $) & <
fullnames =. names ,. dup ('_' , loc , '_')
". joinstring"1 (dup 'get_') ,. fullnames ,. (dup '=: 3 : ''') ,. names ,. (dup '''')
)
attr_writer =: monad define
loc =. > coname ''
names =. ;: y
dup =. ((# names) & $) & <
fullnames =. names ,. dup ('_' , loc , '_')
". joinstring"1 (dup 'set_') ,. fullnames ,. (dup '=: 3 : ''') ,. names ,. (dup '=: y''')
)
attr_accessor =: monad define
attr_reader y
attr_writer y
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment