About Monkey 2 › Forums › Monkey 2 Development › Less verbose lambda syntax
Tagged: lambda
This topic contains 11 replies, has 4 voices, and was last updated by 
 Mark Sibly
 2 years, 7 months ago.
- 
		AuthorPosts
 - 
		
			
				
September 5, 2016 at 6:23 am #3663[/crayon]Monkey12345678910111213141516[crayon-5cba161cada8e566665862 inline="true" ]' existing syntaxLocal foo := Lambda:Int(x:Int)Return x + 2EndPrint(foo(5))' slightly less verbose (note inferred return statement for single line expression)Local foo := Lambda:Int(x:Int) x + 2 End' even simpler, with type inference for the return valueLocal foo := Lambda(x:Int) x + 2 End' allows for very clean sorting code' note that Sort expects Int(T,T), so it can be completely inferredLocal foo := New Stack<Int>()foo.Sort(Lambda(lhs,rhs) lhs <=> rhs End)
Thoughts, before I fork and pull request?
September 5, 2016 at 6:33 am #3665Probably not gonna accept it – I ‘m fine with current syntax and find ‘shortcut’ lambdas one of the hardest things to read in other languages.
September 5, 2016 at 1:07 pm #3680Sorry, I’m not quite clear on your response. Was that “accept the pull request” or “accept the existing syntax”?
One of the more powerful uses of lambdas/closures is to represent a very simple piece of logic (even one expression) as a first-class type. If the lambda in question is as simple as a comparison or equation, I think the condensed one-line syntax is quite acceptable.
If the lambda has a lot of logic to it (event handlers, etc.) then it makes sense to spread it across as many lines as you need, as per the current syntax.September 5, 2016 at 6:49 pm #3683Sorry, that should have been ‘not’ – it was late.
Maybe it’s just my inexperience with lambdas, but the short form looks like a minor ‘win’ at the cost of making the code look a lot more obsfucated. It’s just not very monkey-like IMO.
Maybe it’ll grow on me (it has a tiny bit) but right now I just don’t see the need for it.
September 5, 2016 at 7:42 pm #3687TBH, I find this unreadable… If you want to return something, why not state it?
September 5, 2016 at 11:45 pm #3688TBH, I find this unreadable… If you want to return something, why not state it?
The idea is less about the implicit return statement and more about keeping a simple lambda on a single line.
With a return statement and no inferred types it’s still quite legible. It just makes it much cleaner if you want to pass a single expression as a parameter to a function.[/crayon]Monkey123456[crayon-5cba161cb9e55315743278 inline="true" ]' Single line:SomeMethod(Lambda:Int(x:Int) Return x + 1 End)' As opposed to:SomeMethod(Lambda:Int(x:Int)Return x + 1End)Maybe it’ll grow on me (it has a tiny bit) but right now I just don’t see the need for it.
If I add flatmap/filter/reduce to the Stack class and provide an example banana project, you’ll see the power of mini-closures and why it makes code much easier to understand. I’ve already added it, I’ll make a PR at some point.
September 6, 2016 at 12:57 am #3690Think I prefer ‘as opposed to’.
The problem I have with the first one is it’s just a string of ‘stuff’. At least in c++ you get the enclosing ‘{‘ ‘}’ to delimit the lambda scope from the expression scope, even if it’s on the same line.
September 6, 2016 at 2:05 am #3691How hard would it be to tell the parser to make newlines optional for single statement lambdas, then? That should be a simple enough change, and if the developer prefers to split it across multiple lines, that’s up to them.
September 7, 2016 at 12:58 am #3721At least in c++ you get the enclosing ‘{‘ ‘}’ to delimit the lambda scope from the expression scope
isn’t that what Lambda() and End are for?
September 7, 2016 at 2:41 am #3725isn’t that what Lambda() and End are for?
Pretty much, but the majority of the Monkey-X/MX2 community have an aversion to braces.
September 8, 2016 at 1:38 am #3752I’ll assume that’s a no, then.
September 9, 2016 at 5:48 am #3798Yes, that’s a no.
I was initially planning on supporting ‘looser’ eol rules, but that wasn’t as easy as I thought it’d be.
In the end, I sorted of just embraced the whole strict eol approach instead, thinking I might revisit it later. But now, I just don’t see a compelling reason to any more – I like it the way it is.
 - 
		AuthorPosts
 
You must be logged in to reply to this topic.