【问题标题】:Zipping unequal length lists in scala在scala中压缩不等长列表
【发布时间】:2014-05-23 08:38:27
【问题描述】:

我想要这样的东西:

def unequalZip[A, B](a: Iterable[A], b: Iterable[B]) = Iterable[(Option[A], Option[B])]

使用Nones 将较短可迭代的项目与较长可迭代的项目匹配

【问题讨论】:

    标签: scala functional-programming scala-collections zipper


    【解决方案1】:

    你想要

    a.zipAll(b, None, None)
    

    如果您已经有选择,或者

    a.map(x => Option(x)).zipAll(b.map(x => Option(x)), None, None)
    

    否则。

    【讨论】:

      【解决方案2】:

      def lift[A](a: Iterable[A]) = a map {Option.apply} def unequalZip[A, B](a: Iterable[A], b: Iterable[B]) = lift(a).zipAll(lift(b), None, None)

      【讨论】:

        【解决方案3】:

        看看 zipAll。

        /** 返回一个由这个 $coll 和另一个 iterable 组成的 $coll 集合 * 通过成对组合相应的元素。 * 如果两个集合中的一个比另一个短,* 占位符元素用于将较短的集合扩展到 较长的长度。

        【讨论】:

          猜你喜欢
          • 2019-09-29
          • 2012-07-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-10-13
          • 1970-01-01
          相关资源
          最近更新 更多