如何在VB.NET中实现地图功能 [英] How to Implement Map Function in VB.NET

查看:329
本文介绍了如何在VB.NET中实现地图功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在VB.NET中使用 Map -vs-each>在这个答案中。



它应该有一个 IEnumerable(Of TIn)函数(Of TIn,TOut),对每个元素执行该函数,并返回一个 new IEnumerable Tout)

我知道VB.NET不是真正的函数式语言。我有业务需求,但仍希望使用一些功能花絮,特别是与LINQ一起使用。



有类似的问题要求这里,但问题和答案实际上是关于实现每个

解决方案

感谢 @Sehnsucht

a>指出在LINQ中已经是 Map 函数,称为 Select

我的原始答案肯定不如设计LINQ的聪明人创建的那么好:

这是一个扩展方法,可以放在 Module 在根名称空间中:

 模块扩展
< System。 Runtime.CompilerServices.Extension> _
公共函数映射(TIn,TOut)(ByVal a As IEnumerable(Of TIn),ByVal f As Func(Of TIn,TOut))作为IList(Tout)
Dim l As新列表(Tout)
对于每个我作为TIn在
Dim x = f(i)
l.Add(x)
下一个
返回l
End Function
End Module

它可以这样调用:

  Sub Main()
Dim Test As New List(Double)from {-10000000,-1,1,1000000}
Dim 1 = Test.Map(Function(x)x.ToString(F2))
'Result1的类型为IList(Of String)
Dim Result2 = Test.Map(AddressOf IsPositive)
'Result2的类型为IList(Of Boolean)
Dim Result3 = Map(Test,Function(y)y + 1)
'Result3是IList类型的(双精度型)
End Sub

Private Function IsPositive(d As Double)与布尔
如果d> 0 then
返回True
Else
返回False
End If
End Function

我返回一个 IList(Of TOut),因为它对我的目的更有用(并且,它返回一个列表!) 。它可以通过将返回行更改为返回l.AsEnumerable()来返回 IEnumerable(Of TOut)。它接受 IEnumerable(Of TIn)而不是 IList(Of TIn),因此它可以接受更广泛的投入。



我向所有提议改善性能的人士开放。


I am trying to implement Map in VB.NET with the functionality described in this answer.

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).

I know VB.NET is not a true functional language. I have business requirements, but would still like to use some functional tidbits, especially in conjunction with LINQ.

A similar question is asked here, but the question and answer are actually about implementing Each.

解决方案

Thanks to @Sehnsucht for pointing out that there already is a Map function in LINQ that is called Select.

Here is my original answer that is definitely not as good as what the smart people designing LINQ created:

Here is a an extension method that can be placed in a Module in the root namespace:

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!). It could return an IEnumerable(Of TOut) by changing the return line to Return l.AsEnumerable(). It accepts an IEnumerable(Of TIn) instead of an IList(Of TIn) so it can accepts a wider range of inputs.

I'm open to anybody suggesting performance improvements.

这篇关于如何在VB.NET中实现地图功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆