vb.net 中的映射函数 [英] map function in vb.net

查看:34
本文介绍了vb.net 中的映射函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 VB.NET 中实现地图功能,并且我尝试了以下方法.

I am trying to implement map function in VB.NET and I have tried the below.

 Function Map(a,f)
    Dim i
    for each i in a
      f(i)
    next
 End Function

 Function alert(a)
    MessageBox.Show(a)
 end function

但是上面的代码不起作用并且说没有声明警报.
请帮我解决这个问题.

But the above code is not working and saying alert is not declared.
Please help me on this.

推荐答案

您的函数不返回任何内容.试试这个:

Your functions don't return anything. Try this:

Public Sub Map(Of T)(ByVal a As IEnumerable(Of T), ByVal f As Action(T))
    For Each i As T In a
      f(i)
    Next
End Sub

Public Sub alert(ByVal a As Object)
    MessageBox.Show(a)
End Sub

以上是基于问题中的方法.一个实际的传统 Map() 函数可能看起来更像这样:

Above is based on the methods from the question. An actual traditional Map() function might look more like this:

Public Iterator Function Map(Of T)(ByVal a As IEnumerable(Of T), ByVal f As Func(Of T,T)) As IEnumerable(Of T)
    For Each i As T In a
        Yield f(i)
    Next
End Function

或者这个:

Public Iterator Function Map(Of T, U)(ByVal a As IEnumerable(Of T), ByVal f As Func(Of T, U)) As IEnumerable(Of U)
    For Each i As T In a
        Yield f(i)
    Next
End Function

这篇关于vb.net 中的映射函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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