featurist / pogoscript

A readable, DSL friendly programming language with excellent concurrency primitives
http://pogoscript.org/
BSD 2-Clause "Simplified" License
130 stars 10 forks source link

Wanted, anonymous function syntax with parameters #26

Open PhilAndrew opened 11 years ago

PhilAndrew commented 11 years ago

In Scala you can write.

scala> (1 until 1000).filter(n => n % 3 == 0 || n % 5 == 0).foldLeft(0)( + ) res2: Int = 233168

n => n % 3 == 0... this is a function with parameter n.

Could

_.each (abc) @(num, key)
  console.log ( "#(num) #(key)" )

Be written more concisely as something like:

_.each (abc) @{console.log ( "#(_.1) #(_.2)")}

I do not have any good suggestion for syntax.

refractalize commented 11 years ago

Have to give this one some thought...

Was originally intending to use @1, @2, etc. But thought on reflection that it's usually better to be a little verbose with your params anyway. For common functions, like map and filter that only pass one parameter for example, this sort of thing could be useful.

PhilAndrew commented 11 years ago

Yes the interesting thing is there's so many ways to do it. For example in Scala.

(1 to 1000).reduceLeft( _ + _ )

The + is a notation which produces the function (a,b) => a + b which could also be written as

def add(a,b) = {
  a + b
}

(1 to 1000).reduceLeft(add)

Then you could look at LiveScript and how it chains functions together to map fold etc. With so many ways to do it, you have a problem of choice, what is going to work well with your language.