【问题标题】:Lambda / LINQ to VB.NETLambda / LINQ to VB.NET
【发布时间】:2018-09-27 18:05:32
【问题描述】:

我有一个想要转换为“普通”VB.NET 代码的 Lambda 代码。

我还没有找到任何可以将 Lambda 转换为代码的工具,我不明白这段代码的作用。

什么是理解这段代码的好方法?

特别是,我不知道这条线是做什么的:

Let p3 = uDestPoints.First(Function(p) Math.Abs(p.X - t.P3.X) < 1 AndAlso Math.Abs(p.Y - t.P3.Y) < 1)
Select New With {Key .X1 = destList.IndexOf(p1), Key .X2 = destList.IndexOf(p2), Key .X3 = destList.IndexOf(p3)}

谢谢。

Public Function GetWarps(ByVal uSourcePoints As IEnumerable(Of Point), ByVal uDestPoints As IEnumerable(Of Point), ByVal uDestTriangles As IEnumerable(Of Triangle)) As IEnumerable(Of Warp)
    ' build lists of source and destination landmark points
    Dim sourceList = uSourcePoints.ToList()
    Dim destList = uDestPoints.ToList()

    ' find all three triangle points in the list of destination landmark points
    Dim indices = From t In uDestTriangles
                  Let p1 = uDestPoints.First(Function(p) Math.Abs(p.X - t.P1.X) < 1 AndAlso Math.Abs(p.Y - t.P1.Y) < 1)
                  Let p2 = uDestPoints.First(Function(p) Math.Abs(p.X - t.P2.X) < 1 AndAlso Math.Abs(p.Y - t.P2.Y) < 1)
                  Let p3 = uDestPoints.First(Function(p) Math.Abs(p.X - t.P3.X) < 1 AndAlso Math.Abs(p.Y - t.P3.Y) < 1)
                  Select New With {Key .X1 = destList.IndexOf(p1), Key .X2 = destList.IndexOf(p2), Key .X3 = destList.IndexOf(p3)}

    ' return enumeration of warps from source to destination triangles
    Return From x In indices
           Select New Warp(New Triangle(sourceList(x.X1), sourceList(x.X2), sourceList(x.X3)), New Triangle(destList(x.X1), destList(x.X2), destList(x.X3)))
End Function

【问题讨论】:

  • 我也想知道。这些怪物吓死我了。
  • Lambda(函数)是代码——它只是编写静态方法的一种简写方式,然后您可以将其传递给另一个方法。也许您应该专注于 Enumerable.First 的作用?

标签: vb.net linq lambda


【解决方案1】:

这是我的示例扩展。我创建了一个不使用 lambda 参数的First 的特定问题版本,并修改了代码以使用ValueTuple 而不是匿名类,因为您不能从Function 返回匿名类。

我还使用了List 来聚合 LINQ 答案,因为使用Yield 对生成器函数的实际扩展在我看来并没有澄清。

由于这些变化,这段代码在生成中的效率不如 LINQ 代码,但在 First 中效率更高,所以也许要洗一下?

Public Function FirstClosePoint(points As IEnumerable(Of Point), TP As Point) As Point
    For Each p In points
        If Math.Abs(p.X-TP.X) < 1 AndAlso Math.Abs(p.Y-TP.Y) < 1 Then
            Return p
        End If
    Next
    Throw New Exception("Unable to find FirstClosePoint")
End Function

Public Function GetWarps(ByVal uSourcePoints As IEnumerable(Of Point), ByVal uDestPoints As IEnumerable(Of Point), ByVal uDestTriangles As IEnumerable(Of Triangle)) As IEnumerable(Of Warp)
    ' build lists of source and destination landmark points
    Dim sourceList = uSourcePoints.ToList()
    Dim destList = uDestPoints.ToList()

    ' find all three triangle points in the list of destination landmark points
    Dim indices = New List(Of (X1 As Integer,X2 As Integer,X3 As Integer))

    For Each t In uDestTriangles
        Dim p1 = FirstClosePoint(uDestPoints, t.P1)
        Dim p2 = FirstClosePoint(uDestPoints, t.P2)
        Dim p3 = FirstClosePoint(uDestPoints, t.P3)
        indices.Add( (destList.IndexOf(p1),destList.IndexOf(p2),destList.IndexOf(p3)) )
    Next

    ' return enumeration of warps from source to destination triangles
    Dim ans = New List(Of Warp)

    For Each x In indices
        ans.Add(New Warp(New Triangle(sourceList(x.X1), sourceList(x.X2), sourceList(x.X3)), New Triangle(destList(x.X1), destList(x.X2), destList(x.X3))))
    Next

    Return ans
End Function

【讨论】:

  • 非常感谢,您的回答真的是很好的学习资源!
  • 现在我正在考虑创建一个 LINQ to Procedural Code 配方:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-10-22
  • 2010-11-07
  • 2011-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多