First Class Functions

I am looking forward to the new release with a Lua like language here - but will it have support for functions as first class objects?

My dream is to have a functional skeleton for playing instruments that I can spin up inside a whole series of loops, that I can pass in specific functions to play specific instruments off a shared timetable/structure

God my songs would be so much shorter, with so much less boilerplate…

Appreciate that I am a 22 year Erlang veteran and functional programming geek :wink: but it would be soooooo immense

You can actually achieve something like that in the current (Ruby) Sonic Pi. When you define a function, e.g.:

define :dosomething do |param1, param2|
  # Do something with params
end

You can pass around the name of the function as a symbol (:dosomething), and then call it later with send:

func = :dosomething
# ...
send func, 123, 456

I love you, I kiss your feet, can I wash your car? can I send you money? xxx

I mean I need to rewrite everything but this will make life miles better…

I should have thought to just try it - I have read all the documents and its not been mentioned. I know the Sonic Pi scripting language is Ruby-related somehow (a DSL?) and I think I sort of remember that functions are first class in Ruby but TBH its been 22 years since I left Ruby for Erlang and the BEAM and it all gets a bit hazy :wink:

I should write up a new section for the help on programming structures Section 5 - programing structures

With this I can write one mechanism for reading my song structure (which bar, which beat) and another one for responding - do this on the strong beat, this on the weak, and a third for playing a particular instruments all as functions and then just have a super simple set of live_loops that I can assemble into an ensemble - will be magic.

At the mo each loop has a copy of the whole internals :frowning:

1 Like

This sounds fascinating. Please post what you come up with.
Honestly, I’m not sure I get what you’re driving at with this, what you get by passing functions as objects.

1 Like

Ruby doesn’t have first-class functions, but it does have closures in the form of blocks, procs and lambdas. Blocks are used for passing blocks of code to methods, and procs and lambda’s allow storing blocks of code in variables.
Concerning Lua one might read
First-Class Functions in an Imperative World
I prefered Python and JavaScript know first class functions. (And there are some more languages.)
Have fun!