davidedc / livecodelab

a web based livecoding environment
http://livecodelab.net/
MIT License
327 stars 63 forks source link

Functions & Macros #211

Closed noio closed 10 years ago

noio commented 10 years ago

Allow the user to define functions and shorthands for calling those functions. The functions could be shared through a mechanism like #212.

1) Functions

Create syntax for defining functions that can take a code block as an argument. Like

grid(3, 4, <code>)

for

4.times (x) ->
  3.times (y) ->
    pushMatrix
    move (-(4-1)/2+x),(-(3-1)/2+y),0
    //--
    <code>
    //--
    popMatrix

2) Macros

Letting users define rewrite rules that make these functions easier to write:

3 x 4
  <code> 
// rewritten to
grid(3, 4, <code>)
rumblesan commented 10 years ago

so one of the things that I still want to have available in LiveCodeLang is first class functions and higher order functions. this would make it easy to have functions like the grid one above. This is currently how the times loop actually works.

At the moment I'm making live code lang very basic because I still need to get to grips with this language design stuff first, but extending it should be reasonably easy once everything is done.

Macros are something I'm less sure about. I think they can be powerful, but I'd rather look at other ways of creating that sort of functionality using higher order functions and language features. Ongoing discussion I guess

davidedc commented 10 years ago

1) can be done this way, is this what you have in mind?

grid = (columns,rows,code) ->
    for x in [1..columns]
        for y in [1..rows]
            pushMatrix
            move (-(4-1)/2+x),(-(3-1)/2+y),0
            code()
            popMatrix

grid 3,2,->
    rotate box
    move ball

I think that we could simplify the notation of grid 3,2,-> into grid 3,2. In fact I think we could also safely simplify that code() into code if we wanted to.

2) I'm so deep in non-macro aspects of the language so far that I don't have a view on macros yet. In principle, why not, but I'd probably need some more time to have a good opinion about them.

noio commented 10 years ago

Oh, actually, that is exactly what I had in mind. Nice! I just didn't know how easy it was to pass a code block by creating an anonymous function with -> (Even though I was doing it with times :sweat_smile: ). As far as I'm concerned that completely obviates the need for macros.

So this feature is in LCL and I just didn't know it, so feel free to close :ballot_box_with_check:

davidedc commented 10 years ago

closing. macros are outstanding but maybe we can live a bit more with just what we have.

rumblesan commented 10 years ago

higher order functions solve everything! or maybe I've just been coding scala and haskell too long...