[FEATURE]: Syntax for binding values and returning functions. #11

Closed
opened 2025-06-29 17:22:45 +00:00 by mantacid · 1 comment
mantacid commented 2025-06-29 17:22:45 +00:00 (Migrated from github.com)

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:

# function definition
fn myFunc x y do
    def z -> 2*x
    return z+y
end

# alternatively, for simple bodies the following could be used:
fn simpleFunc x y -> (2*x+y)

# value binding
def PHI -> 1.618033988

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.

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: ``` # function definition fn myFunc x y do def z -> 2*x return z+y end # alternatively, for simple bodies the following could be used: fn simpleFunc x y -> (2*x+y) # value binding def PHI -> 1.618033988 ``` 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.
ks0m1c commented 2025-09-26 19:51:19 +00:00 (Migrated from github.com)

@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 pi or sin[x]

Constants are bound using the fn command with a simple signature:

#binding the golden ratio
fn phi [1+sqrt[5]]/2
draw pentagram a  do
  # regular star
    loop 5 do
      fw a
      rt 144
    end
    rt 252
    jmp a
    rt 144
   pentagram a*phi^2
end
rt 360/5
pentagram 500 

Now we can break down the syntax for the fn command

  • First argument: phi - the constant name (signature)
  • Second argument: [1+sqrt[5]]/2 - the expression to bind

The signature phi is shorthand for phi[], indicating no arguments are required

Now that we can bind constants we are also able to rebound them for example here expressing the accumulator pattern with the varying constant size

fn size 100
fn phi [1+5^0.5]/2

def rect levels do
  beColour gold  
  loop levels do
    fw size
    rt 90
    fw size/phi
    rt 90
    fw size
    rt 90
    fw size/phi
    rt 180
    fn size size/phi
  end
end
hd
rect 100

Functions can accept arguments by specifying them in the signature. Say I want to explore all the metallic ratios

fn metal[n] [n+[n*n+4]^0.5]/2
def spiral order size color  do
  fn ratio metal[order]
  beColour color
  fw size
  rt 90
  when size>500 do
    wait 
    home
    faceto 0 0 
    spiral order+1 1 random
  end
  when true do
  spiral order size*ratio color
  end
end
hd

spiral 1 1 random

Function Signature Patterns:

Single parameter: metal[n]
Multiple parameters: metal[a,b,c,d]
No parameters as before : metal or metal[]

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 accommodate metal[a,b,c,d...n] and so forth, of course the expression ideally should invoke these additional arguments else they would be just dangling

Each 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.

@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 ```pi``` or ```sin[x]``` Constants are bound using the ```fn``` command with a simple signature: ```elixir #binding the golden ratio fn phi [1+sqrt[5]]/2 draw pentagram a do # regular star loop 5 do fw a rt 144 end rt 252 jmp a rt 144 pentagram a*phi^2 end rt 360/5 pentagram 500 ``` Now we can break down the syntax for the ```fn``` command - First argument: ```phi``` - the constant name (signature) - Second argument: ```[1+sqrt[5]]/2``` - the expression to bind The signature ```phi``` is shorthand for ```phi[]```, indicating no arguments are required Now that we can bind constants we are also able to rebound them for example here expressing the accumulator pattern with the varying constant ```size``` ```elixir fn size 100 fn phi [1+5^0.5]/2 def rect levels do beColour gold loop levels do fw size rt 90 fw size/phi rt 90 fw size rt 90 fw size/phi rt 180 fn size size/phi end end hd rect 100 ``` Functions can accept arguments by specifying them in the signature. Say I want to explore all the metallic ratios ```elixir fn metal[n] [n+[n*n+4]^0.5]/2 def spiral order size color do fn ratio metal[order] beColour color fw size rt 90 when size>500 do wait home faceto 0 0 spiral order+1 1 random end when true do spiral order size*ratio color end end hd spiral 1 1 random ``` Function Signature Patterns: Single parameter: ```metal[n]``` Multiple parameters: ```metal[a,b,c,d]``` No parameters as before : ```metal``` or ```metal[]``` 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 accommodate ```metal[a,b,c,d...n]``` and so forth, of course the expression ideally should invoke these additional arguments else they would be just dangling Each 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.
Sign in to join this conversation.
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
PaperLand/dojo#11
No description provided.