Using closures in lua: who has examples?

Hi, as an enthusiast synth lover, I have the tendency to stretch a E1 preset to its limits, so we can do more with the synth the makers intended us to do with. That to me is the real attraction in using an Electra One.

When I discovered this forum, I had never hear of lua. My programming background is old fashioned but lua is quite forgiving in that area and let me build whatever I wanted, although perhaps not in the most efficient way.

After two years , it’s time to up the game and dive a bit deeper in lua.

I’m particularly looking for examples where lua “closures” are used in E1 presets, and understand why these were useful in making code smaller, run faster or use less memory.

Who has an example ?

They are more meant to make the code easier to read and maintain. You can consider the closure being a named reference you call functions.

I am quite sure you seen this one:

https://app.electra.one/preset/Jb3DJcTgWBdyWjRigxfs

this is another bit from one of my private presets:

local store = {
    baseSetupSelected = parameterMap.map(1, PT_VIRTUAL, 1000),
    cableRouting = {
             parameterMap.map(1, PT_VIRTUAL, 1001),
             parameterMap.map(1, PT_VIRTUAL, 1002),
             parameterMap.map(1, PT_VIRTUAL, 1003),
             parameterMap.map(1, PT_VIRTUAL, 1004),
             parameterMap.map(1, PT_VIRTUAL, 1005),
             parameterMap.map(1, PT_VIRTUAL, 1006),
             parameterMap.map(1, PT_VIRTUAL, 1007),
             parameterMap.map(1, PT_VIRTUAL, 1008)
    },
    inputCableSelected = parameterMap.map(1, PT_VIRTUAL, 1100),

    -- A function to process parameter map changes
    processChange = function(self, pn, value)
        -- intentionally removed code...
    end
}

having the data store defined like that I do not need to worry about parameterNumbers and types anymore, I simply work with Lua “variables” like store.cableRouting[1], etc.

A closure will require extra memory - its name and the reference to the function must be stored. As well as it will add an extra processing overhead. The benefits of having easier to understand code represent different quality here.

EDIT:

I’d suggest getting a book on Lua programming to those who want to get more serious. The Lua Programming from lua.org is great but it can be a bit harder to read for beginners. Also, it covers more advanced topics such as interfacing Lua a C. But I am sure there are other good books.

E1 firmware includes the latest version of Lua (5.4.6) from lua.org. The books, tutorials, videos you watch should covering 5.4 or higher. There are some notable differences between 5.1, 5.2, 5.3, and 5.4.

Another thing is that you can also play around with on your computer. For grasping the basics it might be faster than using E1.

EDIT 2:

using ChatGPT can also be quite efficient to find solutions and learn stuff:

or design a more specific solutions. eg.

of course you need to check, test, verify, and think about the answers you are getting. But most of the time it is correct or will help you to get moving.

1 Like