user2408573
user2408573

Reputation: 21

How to return a unaryfunction from a multi-variables function in VB.NET

I have a function:

Public Function F(ByVal a As Double, ByVal b As Double,
                  ByVal c As Double, ByVal x As Double) As Double
  y = ax ^ 3 + bx ^ 2 + cx + d
  Return y
End Function

How could I create a function that allows me to read parameters a,b,c,d and return an unary function? For example a=1,b=1,c=3,d=4:

Public Function F(ByVal x As Double) As Double
  y = 1x ^ 3 + 2x ^ 2 + 3x + 4
  Return y
End Function

or in other words, how could I create a function that returns a function of type

Func(Of Double, Double)

Upvotes: 2

Views: 57

Answers (1)

user1937198
user1937198

Reputation: 5348

Use a lambda function.

Public Function F(ByVal a As Double, ByVal b As Double, ByVal c As Double) As Func(of Double, Double)
    Return Function(ByVal x As Double) As Double
               Return ax ^ 3 + bx ^ 2 + cx + d
           End Function
End Function

Upvotes: 2

Related Questions