简体   繁体   English

如何在VB.NET中实现Map函数

[英]How to Implement Map Function in VB.NET

I am trying to implement Map in VB.NET with the functionality described in this answer.我正在尝试使用 此答案中描述的功能在 VB.NET 中实现Map

It should take an IEnumerable(Of TIn) and a Function(Of TIn, TOut) , execute the function over every element, and return a new IEnumerable(Of TOut) .它应该接受一个IEnumerable(Of TIn)和一个Function(Of TIn, TOut) ,对每个元素执行函数,并返回一个新的IEnumerable(Of TOut)

I know VB.NET is not a true functional language.我知道 VB.NET 不是真正的函数式语言。 I have business requirements, but would still like to use some functional tidbits, especially in conjunction with LINQ.我有业务需求,但还是想用一些功能花絮,尤其是结合LINQ。

A similar question is asked here , but the question and answer are actually about implementing Each . 这里提出一个类似的问题,但问题和答案实际上是关于实现Each

Thanks to @Sehnsucht for pointing out that there already is a Map function in LINQ that is called Select .感谢@Sehnsucht指出 LINQ 中已经有一个Map函数,称为Select

Here is my original answer that is definitely not as good as what the smart people designing LINQ created:这是我的原始答案,绝对不如设计 LINQ 的聪明人创建的答案好:

Here is a an extension method that can be placed in a Module in the root namespace:这是一个扩展方法,可以放在根命名空间的Module中:

Module Extensions
    <System.Runtime.CompilerServices.Extension> _
    Public Function Map(Of TIn, TOut)(ByVal a As IEnumerable(Of TIn), ByVal f As Func(Of TIn, TOut)) As IList(Of TOut)
        Dim l As New List(Of TOut)
        For Each i As TIn In a
            Dim x = f(i)
            l.Add(x)
        Next
        Return l
    End Function
End Module

It can be called like:它可以被称为:

Sub Main()
    Dim Test As New List(Of Double) From {-10000000, -1, 1, 1000000}
    Dim Result1 = Test.Map(Function(x) x.ToString("F2"))
    'Result1 is of type IList(Of String)
    Dim Result2 = Test.Map(AddressOf IsPositive)
    'Result2 is of type IList(Of Boolean)
    Dim Result3 = Map(Test, Function(y) y + 1)
    'Result3 is of type IList(Of Double)
End Sub

Private Function IsPositive(d As Double) As Boolean
    If d > 0 then
        Return True
    Else
        Return False
    End If
End Function

I return an IList(Of TOut) because it is more useful for my purposes (and, well, it's returning a list!).我返回一个IList(Of TOut)因为它对我的目的更有用(而且,它返回一个列表!)。 It could return an IEnumerable(Of TOut) by changing the return line to Return l.AsEnumerable() .它可以通过将返回行更改为Return l.AsEnumerable()Return l.AsEnumerable() IEnumerable(Of TOut) Return l.AsEnumerable() It accepts an IEnumerable(Of TIn) instead of an IList(Of TIn) so it can accepts a wider range of inputs.它接受IEnumerable(Of TIn)而不是IList(Of TIn)因此它可以接受更广泛的输入。

I'm open to anybody suggesting performance improvements.我对任何建议性能改进的人持开放态度。

super simple超级简单

   Public Function map(equation As Func(Of Object, Object))

    For i = 0 To rows - 1
        For j = 0 To cols - 1
            Dim val = data(i, j)
            data(i, j) = equation(val)
        Next
    Next
End Function

and to use it并使用它

 m.map(Function(x) 3 > x)
 m.map(Address Sigmoid)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM