[FEATURE]: Syntax for binding values and returning functions. #11
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
I found dojo through one of your YouTube videos, and am really impressed with how well it works! However, in messing around with it, I found that there was no way to define my own functions on numbers. That is to say, I couldn't find a way to define functions that return a value for use elsewhere.
It would be nice to be able to define functions and values that can be used elsewhere in the code, similar to how the sine table is implemented. Something like:
Obviously, you don't have to use this syntax. I just noticed that
->highlights as if it were an operator despite having no documented use, and thought it would work well for function definition.@mantacid Thank you for your request and kind suggestions, I had been mulling on how to create userspace mathematical expressions more idiomatically for a while.. This gave me a great opportunity to finally tackle it headon.
I tried to keep the syntax for binding constants and functions as close to how we already invoke builtin constants and functions for e.g
piorsin[x]Constants are bound using the
fncommand with a simple signature:Now we can break down the syntax for the
fncommandphi- the constant name (signature)[1+sqrt[5]]/2- the expression to bindThe signature
phiis shorthand forphi[], indicating no arguments are requiredNow that we can bind constants we are also able to rebound them for example here expressing the accumulator pattern with the varying constant
sizeFunctions can accept arguments by specifying them in the signature. Say I want to explore all the metallic ratios
Function Signature Patterns:
Single parameter:
metal[n]Multiple parameters:
metal[a,b,c,d]No parameters as before :
metalormetal[]In this case
metal[n]the signature reveals that this function has an arity of 1, if you had need of larger arities the signature can accommodatemetal[a,b,c,d...n]and so forth, of course the expression ideally should invoke these additional arguments else they would be just danglingEach function/arity pair is unique so metal/0 metal/1 metal/4 will not conflict on namespace
Hope this would be as much a joy to play with as it was for me to build it! Let me know what you think or if you have any feedback.