4.12.2.1 - Higher-order functions, map, filter and fold (VB.NET)
Learn to pass functions into reusable transformations, map and filter a sequence, and fold its values into one result.
The VB.NET route is incomplete: runnable support pending. The native source is studied and answered in writing, with no Run. Functional programming is examined in Paper 2.
Functional programs and higher-order functions
A functional program builds results by applying and combining functions. A pure function returns the same result for the same inputs and has no observable side effects such as changing an external variable or overwriting its input. This makes reasoning about a calculation easier: its result depends on its arguments rather than hidden changing state.
VB.NET supports several paradigms. Functional style uses its lambdas, delegates and sequence transformations; writing a VB.NET program does not by itself make that program functional. The specification also names functional languages such as Haskell, Standard ML, Scheme and Lisp. Our examples construct programs using VB.NET's functional support.
Higher-order function
A function is higher-order if it takes a function as an argument, returns a function as its result, or does both.
Imports System
Module Program
Function ApplyTwice(operation As Func(Of Integer, Integer),
value As Integer) As Integer
Return operation(operation(value))
End Function
Function MakeThreshold(minimum As Integer) As Func(Of Integer, Boolean)
Return Function(value As Integer) value >= minimum
End Function
Sub Main()
Console.WriteLine(ApplyTwice(Function(n As Integer) n + 3, 2))
Dim passes As Func(Of Integer, Boolean) = MakeThreshold(5)
Console.WriteLine(passes(5))
Console.WriteLine(passes(4))
End Sub
End Module
The first output is 8 because the supplied function changes 2 to 5 to 8. The other outputs are True then False. ApplyTwice is higher-order because of its delegate parameter; MakeThreshold is higher-order because its result is a delegate. The returned predicate takes an integer and produces a Boolean. That predicate is first-class, but it is not itself higher-order: its own input and result are ordinary values. First-class describes how a value can be used; higher-order describes a function's signature.
Map and filter transform sequences
Map applies a function to every element and returns the resulting sequence. Filter keeps exactly the original elements for which a predicate is true. Map computes one result per input and preserves the number of elements, even if some results equal their inputs; filter chooses values and can shorten the sequence. Both are higher-order because a function is supplied as an argument.
VB.NET's LINQ sequence methods express these operations. Select implements map and Where implements filter. Imports System.Linq makes their extension-method syntax available. A sequence of integers here represents a mathematical list; it is stored as an array for this example.
Imports System
Imports System.Linq
Module Program
Sub Main()
Dim original As Integer() = {2, 5, 8}
Dim mapped As Integer() =
original.Select(Function(n As Integer) n + 1).ToArray()
Dim kept As Integer() =
mapped.Where(Function(n As Integer) n Mod 2 = 0).ToArray()
Console.WriteLine(String.Join(", ", mapped))
Console.WriteLine(String.Join(", ", kept))
Console.WriteLine(String.Join(", ", original))
End Sub
End Module
The three outputs are 3, 6, 9, then 6, then 2, 5, 8. The map is ; the predicate tests evenness. Filter does not replace retained elements with True or False; it retains 6 itself. The source array is unchanged.
LINQ Select and Where describe deferred sequences: their elements are computed when enumerated. ToArray() enumerates them and stores the results in a new array, making the stage values explicit. Arrays are mutable in VB.NET; our functional code deliberately avoids changing them. Fresh output arrays and pure lambdas preserve the source in these examples, rather than proving that the array type is immutable.
Continue this lesson.
You’re previewing NovaLearn without an account. Create a free account to read the rest.
Create free account