Forums

## Lambdas

General discussion about Cobra. Releases and general news will also be posted here.
Feel free to ask questions or just say "Hello".

### Lambdas

t = [3, 2, 1]
t.sort(do(a as int, b as int)=a.compareTo(b))

As you can see, it uses the same "do" keyword as closures, but the argument list is followed by "=<expr>".

The argument types are not yet inferred which is why you see them explicitly typed in the example above.

Enjoy.
Charles

Posts: 2515
Location: Los Angeles, CA

### Re: Lambdas

Hi,
I really like the simple straightforward syntax. It makes lambdas lucid and understandable. The same about mixins.
(I don't know how many times I recently have read about lambdas in other languages and just got confused.)

Regards
Csaba
Last edited by Csaba on Mon Mar 02, 2009 8:47 am, edited 1 time in total.
Csaba

Posts: 42

### Re: Lambdas

Glad to hear it. Cobra is shaping up quite nicely.
Charles

Posts: 2515
Location: Los Angeles, CA

### Re: Lambdas

First of all, kudos to Chuck for these two additions. They will certainly make Cobra a much more modern and productive language.

About lambdas: this is just syntactic sugar for creating a single-line anonymous method, right? What I mean is that your example would be equivalent to:

t = [3, 2, 1]
t.sort(do(a as int, b as int))
return a.compareTo(b)

The compiler should probably emit a warning message ("warning: lambda syntax is the preferred way for declaring single-line anonymous methods", because the lambda way is certainly more readable.

Although the current implementation is nice, I wonder how much work it will be to add parameter type inference. Isn't it just a matter of analyzing the signatures of the potential candidates, and choosing the one that fits (or aborting compilation if none exists)?

I'm asking because this will be sooo much nicer:

t = [3, 2, 1]
t.sort(do(a, b) = a.compareTo(b))

On a related note, I've seen some languages that use an even more streamlined approach to lambdas, through the use of a coupe of special parameter placeholders:

t = [3, 2, 1]
t.sort(_a.compareTo(_b))

I've made up the _a, _b part, other obvious options include \$a, \$b or _1, _2. I wonder what's your opinion on this approach. Mine is that is allows for more readable code, which is always nice. I don't think it would be hard to implement, we just have to take the whole expression, append "do(_a, _b)" in front of it, and apply the code that deals with regular lambdas.
jonathandavid

Posts: 159

### Re: Lambdas

jonathandavid wrote:First of all, kudos to Chuck for these two additions. They will certainly make Cobra a much more modern and productive language.

About lambdas: this is just syntactic sugar for creating a single-line anonymous method, right? What I mean is that your example would be equivalent to:

t = [3, 2, 1]
t.sort(do(a as int, b as int))
return a.compareTo(b)

The compiler should probably emit a warning message ("warning: lambda syntax is the preferred way for declaring single-line anonymous methods", because the lambda way is certainly more readable.

Yes. You can open a ticket if you like.

jonathandavid wrote:Although the current implementation is nice, I wonder how much work it will be to add parameter type inference. Isn't it just a matter of analyzing the signatures of the potential candidates, and choosing the one that fits (or aborting compilation if none exists)?

I'm asking because this will be sooo much nicer:

t = [3, 2, 1]
t.sort(do(a, b) = a.compareTo(b))

Yes, I want that to. Regarding effort, I haven't it looked into it yet. Patches are welcome or I will eventually get to it myself.

jonathandavid wrote:On a related note, I've seen some languages that use an even more streamlined approach to lambdas, through the use of a coupe of special parameter placeholders:

t = [3, 2, 1]
t.sort(_a.compareTo(_b))

I've made up the _a, _b part, other obvious options include \$a, \$b or _1, _2. I wonder what's your opinion on this approach. Mine is that is allows for more readable code, which is always nice. I don't think it would be hard to implement, we just have to take the whole expression, append "do(_a, _b)" in front of it, and apply the code that deals with regular lambdas.

It's an interesting idea, but some things come to mind:
-- It's not clear what prefix should be used. Certainly not _ which is already used for object vars! And I've been reserving \$ if we integrate regexes into the language.
-- I don't know if lambdas come up often enough to warrant a less obvious syntax. I'm sure in some pure functional languages they do.
-- Cobra has a theme of kicking things off with keywords like "ref", "def", "for" expressions, etc. "do" fits in with that.
For now, it's "do(..."
Charles

Posts: 2515
Location: Los Angeles, CA

### Re: Lambdas

For the record: D 2.0 uses a somewhat unexpected approach, based on expression strings:

http://www.digitalmars.com/d/2.0/phobos ... rithm.html

I don't like that at all, though, especially from a language that aims at static typing like D.
jonathandavid

Posts: 159

### Re: Lambdas

Yeah that's a strange approach because editors are going to have a hard time doing syntax highlighting, autocompletion, etc. Plus it just looks weird.
Charles

Posts: 2515
Location: Los Angeles, CA

### Re: Lambdas

This blog post is worth a read.

http://unlimitednovelty.com/2009/03/ind ... ortem.html

I post it in this thread because it talks about multi-line lambdas.
jonathandavid

Posts: 159

### Re: Lambdas

High all,

I run into problem when I tried to do a simle Lambda example!
How is labda declared correctly?
Any help is wellcome!

(And can the summation be done in some more functional (and hopefully faster and more effctive) way as in, for instance, Scala?)

Regards
Csaba

EDIT: Changed code due to many simple trivial errors in previous version, this version gives output:

>C:\Cobra\Cobra.bat "LambdaSimpleDemo.cobra"
Sum of even numbers from 1 to 10 are:20 Comment: Correctly, this should be 30
Sum of odd numbers from 1 to 10 are: 25
Sum of every third number from 1 to 10 are: 18

Code: Select all
""
LambdaSimpleDemo  by CsabaU@Hotmail.com 2009-04-11
Purpose: A simple complete Lambda example.
"""
class LambdaSimpleDemo

sig FunctionWithOneIntArgAndReturnsBool( i as int32) as bool

def sumOfSelectedValues(lowerLimit as int32, upperLimit as int32, doSum as FunctionWithOneIntArgAndReturnsBool) as int32 is shared
sum  = 0
for i in lowerLimit : upperLimit
if doSum(i)
sum += i
return sum

def main is shared # the execution starts here
low =1
high = 10
print 'Sum of even numbers from [low] to [high] are:[.sumOfSelectedValues(low, high, do(i as int32) = i % 2 == 0)] '
print 'Sum of odd numbers from [low] to [high] are: [.sumOfSelectedValues(low, high, do(i as int32) = i % 2 == 1)] '
print 'Sum of every third number from [low] to [high] are: [.sumOfSelectedValues(low, high, do(i as int32) = i % 3 == 0)] '
Last edited by Csaba on Sat Apr 11, 2009 2:26 pm, edited 6 times in total.
Csaba

Posts: 42

### Re: Lambdas

Delegates are declared as "method signatures":
sig FunctionWithOneIntArgAndReturnsBool(i as int32) as bool

# general form:
sig TYPENAME(ARGS) as TYPE
Charles

Posts: 2515
Location: Los Angeles, CA

Next