question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Multiline/-expression lambdas/anonymous functions?

See original GitHub issue

Not a bug, but I could not find an answer anywhere. I mostly use R and very much like the functional approach. Python lacks proper pipes which are essential IMO to functional programming, so I was happy to discover Coconut!

Another essential part of functional programming (to me) is using a bunch of anonymous functions. For instance:

#mean intercor by column
mibi = function(x) {
  v = sapply(1:ncol(x), function(col) {
    col = x[, col]
    (sum(col) - 1) / (ncol(x) - 1)
  })

  names(v) = colnames(x)

  v
}

mibi(r_cors$correlations[-1, -1])

So, we make a function, mibi, that finds the mean intercorrelation by column from a correlation matrix. The main part of the function is sapply (an implicit loop that returns a vector [equivalent to a simple list in Python]), which is given an anonymous function that spans 2 lines.

In the Coconut documentation, there is an example of a lambda:

dubsums = map((x, y) -> 2*(x+y), range(0, 10), range(10, 20))
dubsums |> list |> print

Compared to the ordinary Python:

dubsums = map(lambda x, y: 2*(x+y), range(0, 10), range(10, 20))
print(list(dubsums))

But this is still only a one-liner lambda. How would one use multi-line/expression lambdas?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

5reactions
evhubcommented, Jun 20, 2016

Thanks for the issue! Currently, Coconut only supports one-line, not multi-line lambdas. Multi-line lambdas is definitely a feature I could add, however. The reason I haven’t yet is that in most cases I find that, since a def block is valid anywhere, it’s always possible to simply use that when one feels the need to use a multi-line lambda.

I’m not opposed to adding multi-line lambdas, however. How does this look:

map(def (x, y) -> (
    some_func(x)
    some_other_func(y)
    ), xs, ys)

which would compile to something like

def _lambda(x, y):
    some_func(x)
    some_other_func(y)
map(_lambda, xs, ys)

which should do exactly what you want. Do you think that would be helpful?

2reactions
evhubcommented, Jul 12, 2016

@Deleetdk @fredcallaway @ymeine I think I should be able to do this, the only issue is that I need to respect the Python rule that whitespace is ignored inside of parentheses. Thus, I think my solution will be to allow lambdas to contain statements, which includes Python’s syntax for putting multiple statements together of putting a semicolon between them. I could even have it check for the use of multiple statement syntax and automatically return the last one. Thus,

map(((x, y) -> some_func(x); some_other_func(y)), xs, ys)

would be how you would write the above example (with the ability to insert whitespace wherever you wanted, as per the Python rule).

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can we write a multiline lambda expression in Java?
Lambda expression is an anonymous method that has used to provide an implementation of a method defined by a functional interface.
Read more >
Lambda Expressions - Visual Basic | Microsoft Learn
Lambda expressions can be functions or subroutines and can be single-line or multi-line. You can pass values from the current scope to a...
Read more >
Using the Lambda expression for multiline functions - O'Reilly
The Lambda expression can also be used for multiline functions, so we can put the body of the function on it. This will...
Read more >
Multi-line Lambdas | .NEXT - NET
Expression Trees in C# doesn't support multi-line lambda expressions. This limitation can be avoided using CodeGenerator.Lambda construction method.
Read more >
Anonymous functions (multi line lambdas) and the decorator ...
It should be an anonymous function, but python does not allow that. Okay, lambdas exist, but they are limited to a single expression,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found