Anonymous user
Apply a callback to an array: Difference between revisions
→{{header|Visual Basic .NET}}: Added VB.NET
(→{{header|Retro}}: update for retro12) |
(→{{header|Visual Basic .NET}}: Added VB.NET) |
||
Line 3,278:
{'a': 'FOO', 'b': 'BAR', 'c': 'BAZ'}
{'a': 'A', 'b': 'B', 'c': 'C'}</pre>
=={{header|Visual Basic .NET}}==
'''Compiler:''' >= Visual Studio 2008
The .NET framework has got us covered.
System.Array.ForEach(T(), Action(Of T)) maps a non-value-returning callback,
System.Linq.Enumerable.Select(Of TSource,TResult)(IEnumerable(Of TSource), Func(Of TSource, TResult)) provides a way to lazily map a function, resulting in an IEnumerable(Of T),
and System.Linq.Enumerable.ToArray(Of TSource)(IEnumerable(Of TSource)) eagerly converts the enumerable to an array.
<lang vbnet>Module Program
Function OneMoreThan(i As Integer) As Integer
Return i + 1
End Function
Sub Main()
Dim source As Integer() = {1, 2, 3}
' Create a delegate from an existing method.
Dim resultEnumerable1 = source.Select(AddressOf OneMoreThan)
' The above is just syntax sugar for this; extension methods can be called as if they were instance methods of the first parameter.
resultEnumerable1 = Enumerable.Select(source, AddressOf OneMoreThan)
' Or use an anonymous delegate.
Dim resultEnumerable2 = source.Select(Function(i) i + 1)
' The sequences are the same.
Console.WriteLine(Enumerable.SequenceEqual(resultEnumerable1, resultEnumerable2))
Dim resultArr As Integer() = resultEnumerable1.ToArray()
Array.ForEach(resultArr, AddressOf Console.WriteLine)
End Sub
End Module</lang>
{{out}}
<pre>True
2
3
4</pre>
=={{header|Vorpal}}==
|