将压缩流投射到最长的流

时间:2013-08-02 13:17:52

标签: scala

Stream上的内置zip函数似乎在最短的输入流截断时截断。我该如何实现这个功能:

def firstOrLongest[T]( a : Stream[ T ], b : Stream[ T ) : Stream[ T ]
// resulting stream should have the property that:
// while there are still elements of a, return (the corresponding element of) a 
// else return (the corresponding element of) b.

2 个答案:

答案 0 :(得分:2)

您可以使用zipAll方法将较短的集合扩展为较长的集合。此方法涉及创建许多中间对象。

def firstOrLongest[T]( a : Stream[T], b : Stream[T]) : Stream[T] = {
  val oa = a.map{ e => Some(e): Option[T] }
  val ob = b.map{ e => Some(e): Option[T] }
  oa.zipAll(ob, None, None).collect{
    case (Some(e), _) => e
    case (None, Some(e)) => e
  }
}

答案 1 :(得分:1)

Stream类有一个追加运算符,可以完全按照您的意愿运行。

您的描述说明:

$(".dropdown-content").slideToggle();
var t = $(".dropbtn");
var mem = t.attr('src');
t.attr('src',t.data('src'));
t.data('src', mem);

在Scala中,这只是:

// while there are still elements of a, return (the corresponding element of) a 
// else return (the corresponding element of) b.