Skip to content

Instantly share code, notes, and snippets.

@Capaverde
Last active October 17, 2022 01:00
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 Capaverde/1a03d32d86d9a18db999724f3fb6bec6 to your computer and use it in GitHub Desktop.
Save Capaverde/1a03d32d86d9a18db999724f3fb6bec6 to your computer and use it in GitHub Desktop.
{ /n def
1 /i def
&{ { n i gteq } { i i 1 add /i def } while &} apply
} /genlist defun
10 genlist prettyprint
{ This is a comment. Below I implement a map that is equivalent to the hardcoded one. } drop
{ dup mul } /square defun
{ /f def
/list def
&{ { list head null not } { list head f apply list tail /list def } while &} apply
} /mymap defun
10 genlist &square mymap prettyprint
{ Alternatively, use an anonymous function. } drop
10 genlist { dup mul } mymap prettyprint
{ The difference between def and defun is merely of the default behavior when met by the interpreter. } drop
{ 2 add } /add2 defun
1 add2
{ 2 add } /add2_ def
1 add2_ apply
{ A '&' before a defun'd block turns its behavior into a def'd block. } drop
1 &add2 apply
{ This is useful to pass functions as arguments, which otherwise would be impossible for defun'd blocks.
And without defun some pieces of code would turn excessively verbose. } drop
{ Below I implement filter and reduce like I did for map before. } drop
{ /f def /list def &{ { list head null not } { list head f apply { list head } if list tail /list def } while &} apply } /myfilter defun
{ 1 2 3 4 5 6 } { 2 mod 0 eq } myfilter prettyprint
{ /f def /acc def /list def
acc
{ list head null not } { list head f apply list tail /list def } while } /myreduce defun
{ 1 2 3 4 5 6 } 0 &add myreduce
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment