2013-08-02 2 views
1

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. 
+0

내 질문 [여기] (http://stackoverflow.com/q/3015962/334519에 대한 답변을 참조하십시오) 하스켈에서 유사한 문제에 대한 해결책을 찾는다. –

답변

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

Stream 클래스에는 원하는대로 정확하게 수행 할 수있는 추가 연산자가 있습니다.

귀하의 설명 상태 :

스칼라에서
// while there are still elements of a, return (the corresponding element of) a 
// else return (the corresponding element of) b. 

, 즉 단지 :

a ++ b 
관련 문제