Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wanted, anonymous function syntax with parameters #26

Open
PhilAndrew opened this issue Jun 7, 2013 · 2 comments
Open

Wanted, anonymous function syntax with parameters #26

PhilAndrew opened this issue Jun 7, 2013 · 2 comments

Comments

@PhilAndrew
Copy link

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
Copy link
Member

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
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants