Lambda expression - can VB Net do this?

  • Thread starter Thread starter Simon Woods
  • Start date Start date
S

Simon Woods

Hi

A blog post I was reading (http://www.defmacro.org/ramblings/fp.html)
included this lambda function.

Function makeIncrementer() {
int n = 0;

int increment() {
return ++n;
}
}

Can it be translated to VB.net? If so how?

Dim incrementer as Func(of Integer) = Function ?????? what next?

Thx

Simon
 
I don't think there's any support for closures in .NET 3.5 (at least,
none that I can find in VB.NET or C#, but my knowledge of Lambda/
Functional programming is limited).

Using the lambda stuff the closest I can come up with would be
something like (and it's really nothing like the closures presented in
that post):
Module Module1

Sub Main()
Dim inc1 As Func(Of Integer, Integer) = Function(i As Integer)
i + 1
Dim n As Integer = 0

n = inc1(n)
Console.WriteLine("{0}", n)
n = inc1(n)
Console.WriteLine("{0}", n)
Console.ReadKey()
End Sub

End Module
 
Mike said:
I don't think there's any support for closures in .NET 3.5 (at least,
none that I can find in VB.NET or C#, but my knowledge of Lambda/
Functional programming is limited).

Using the lambda stuff the closest I can come up with would be
something like (and it's really nothing like the closures presented in
that post):
Module Module1

Sub Main()
Dim inc1 As Func(Of Integer, Integer) = Function(i As Integer)
i + 1
Dim n As Integer = 0

n = inc1(n)
Console.WriteLine("{0}", n)
n = inc1(n)
Console.WriteLine("{0}", n)
Console.ReadKey()
End Sub

End Module

Thx Mike ... I was just about to get to there! I was wondering if there
were other ways to do this in vb. I've read elsewhere the problem of
lack of multiline lamba expressions for vb which I understand C# has. Is
that what you mean by closures? (I doubt it)
 
C# (and I assume that VB.NET in 3.5) has supported true closures since it's
2.0 release.  Using the anonymous delegate syntax.

Yeah that's right, I didn't make the connection between the closures
and anon. delegates... my bad. But anonymous delegates don't exist in
VB.NET, so the answer is still valid.
 
Simon,
The "problem" is that VB doesn't have an increment operator (C#'s ++
operator), it has no operator that will modify a variable *and* return the
result of that modification.

You could try (VB 9.0):

Dim n As Integer
Dim incrementer as Func(of Integer) = Function() (n + 1)

However it will not modify the value of n. You would need a multi-statement
lambda function, which unfortunately VB doesn't allow.

NOTE: The above will run on .NET 2.0, however you need to use VB 9 (VS 2008)
to compile it.
 
Simon,

For which actual problem are you using this.

As it is something as "To calulate how many breaststroke somebody has to do
swimming from London to New York", then save you the time, VB is in my idea
not really suited for that kind of problems, C# is then probably more your
friend.

Just my thoughts when I was reading the question and the anser.

Cor
 
Thx to everyone

Cor I am just trying to understand these features and this article came
up as part of my reading.


S
 
Back
Top