为什么应用[F [_]]这么慢?

时间:2018-01-24 14:23:09

标签: scala scalaz applicative

出于单元测试的目的,我需要将5个列表的内容压缩在一起。我有一个从未完成的测试用例。我设法跟踪它使用scalaz库中的Apply [List] .apply5。我假设在这种情况下不推荐使用apply5(只有当我希望我的代码运行完成时:-))。我想知道为什么会这样,主要是为了扩展我的知识,并避免将来出现类似的情况。请注意,每个列表保证的长度相同。

以下是apply5及以下的使用,即我使用的代码(快速完成运行)。

// This works, but runs extremely slow
def zip5UsingApply[A](l1: List[A], l2: List[A], l3: List[A], l4: List[A], l5: List[A]): List[(A, (A, (A, (A, A))))] =
Apply[List].apply5(l1, l2, l3, l4, l5)((a, b, c, d, e) => (a, (b, (c, (d, e)))))

// Relaced the above with four 'helpers' and the 'zip5' at the bottom
def lzip2[A](f1: => List[A], f2: => List[A]): List[(A, A)] = Zip[List].zip(f1, f2)

def lzip3[A](fa: => List[A], faa: => List[(A, A)]): List[(A, (A, A))] = Zip[List].zip(fa, faa)

def lzip4[A](fa: => List[A], faaa: => List[(A, (A, A))]): List[(A, (A, (A, A)))] = Zip[List].zip(fa, faaa)

def lzip5[A](fa: => List[A], faaaa: => List[(A, (A, (A, A)))]): List[(A, (A, (A, (A, A))))] = Zip[List].zip(fa, faaaa)


def zip5[A](l1: List[A], l2: List[A], l3: List[A], l4: List[A], l5: List[A]): List[(A, (A, (A, (A, A))))] =
lzip5(l1, lzip4(l2, lzip3(l3, lzip2(l4, l5))))

0 个答案:

没有答案