组合 2 个列表的元素 [英] Combining the elements of 2 lists

查看:49
本文介绍了组合 2 个列表的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有两个列表:

val l1=List("a","b","c")
val l2 = List("1","2","3")

我想要的是:List("a1", "b2", "c3") 即把l1的第n个元素与l2的第n个元素相加

What I want is : List("a1", "b2", "c3") that is, adding the nth element of l1 with the nth element of l2

实现它的方法是:

(l1 zip l2).map (c => {c._1+c._2})

我只是想知道是否可以通过 Applicative 实现它.我试过了:

I just wonder if one could achieve it with an Applicative. I tried :

(l1 |@| l2) { _+ _ } 

但它给出了所有的组合:

but it gives all the combinations :

List(a1, a2, a3, b1, b2, b3, c1, c2, c3)

有什么想法吗?

谢谢

贝努瓦

推荐答案

你不能用严格列表来做到这一点,所以改用惰性列表,即流.您必须定义 Applicative[Stream] 实例,如下所示.(您可以在 Haskell 标准库中以 ZipList 的名称找到它.)

You cannot do that with strict lists, so instead use lazy lists i.e. streams. You have to define the Applicative[Stream] instance as shown below. (You'll find it in Haskell standard library under the name ZipList.)

scala> val s1 = Stream("a", "b", "c")
s1: scala.collection.immutable.Stream[java.lang.String] = Stream(a, ?)

scala> val s2 = Stream("1", "2", "3")
s2: scala.collection.immutable.Stream[java.lang.String] = Stream(1, ?)

scala> implicit object StreamApplicative extends Applicative[Stream] {
     |   def pure[A](a: => A) = Stream.continually(a)
     |   override def apply[A, B](f: Stream[A => B], xs: Stream[A]): Stream[B] = (f, xs).zipped.map(_ apply _)
     | }
defined module StreamApplicative

scala> (s1 |@| s2)(_ + _)
res101: scala.collection.immutable.Stream[java.lang.String] = Stream(a1, ?)

scala> .force
res102: scala.collection.immutable.Stream[java.lang.String] = Stream(a1, b2, c3)

使用严格列表无法做到这一点的原因是,不可能在它们上定义一个满足应用法则的 pure.

The reason this cannot be done with strict lists is because it is impossible to define a pure on them that satisfies the applicative laws.

顺便说一句,Scala 可以让您比在 OP 中使用的代码更简洁:

As an aside, Scala lets you do this more concisely than the code you have used in OP:

scala> (l1, l2).zipped.map(_ + _)
res103: List[java.lang.String] = List(a1, b2, c3)

这篇关于组合 2 个列表的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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