Skip to content

Instantly share code, notes, and snippets.

@rrmckinley
Created October 10, 2013 21:39
Show Gist options
  • Save rrmckinley/6926069 to your computer and use it in GitHub Desktop.
Save rrmckinley/6926069 to your computer and use it in GitHub Desktop.
def yipWhenL[I,O,O2](ord: (I,O) => Ordering)(f: (I,Option[O]) => O2): Wye[I,O,O2] = {
val fbL = tee.passL[I] map (f(_,None : Option[O]))
def go(io : Option[I], oo : Option[O]): Wye[I,O,O2] = {
io match {
case None => awaitL[I].flatMap { i => go(Some(i), oo) }
case Some(i) => oo match {
case None => awaitR[O].flatMap(o => go(io,Some(o))) // TODO receiveROr(fbL)...
case Some(o) => ord(i,o) match {
case Ordering.LT => emit(f(i,None)) then go(None,oo)
case Ordering.EQ => emit(f(i,oo)) then go(None,None)
case Ordering.GT => go(io,None)
}
}
}
}
go(None,None)
}
@rrmckinley
Copy link
Author

What would you name this combinator? Like a left-join for sorted input. But, I can't make it passL when right exhausts.

@rrmckinley
Copy link
Author

((1,"Mel"),(2,"Sid"),(4,"Gil")) with ((1,"F"),(3,"M"),(4,"M")) will produce
((1,"Mel","F"),(2,"Sid",""),(4,"Gil","M"))
when ord is (a: (Int,String), b: (Int,String)) => if(a._1 < b._1) Ordering.LT...
and f is (a, b) => (a._1, a.2, b.map(._2).getOrElse(""))

@rrmckinley
Copy link
Author

What do you think about the ord function? I and O are different types and we may be joining on only one field from each. The conditional for producing Ordering is cumbersome but, I couldn't think of a better way.

@pchiusano
Copy link

You should be able to do: awaitR[0].flatMap(o => go(io,Some(o))).orElse(fbL). Also then is now fby.

For the name, I'm not sure. You are basically resampling the right input to match the left, but using an ordering to find the alignment. Maybe orderedAlignL or alignOrderedL? 'Align' usually means some sort of zipping, but allowing for differences in shape. And it is left-biased, and based on some ordering rather than based on position.

@pchiusano
Copy link

I think the ord function is fine. You definitely want to allow the two types to be different, since you might be using this to join like a (A,B,C) with a B, using only the B for comparison.

@rrmckinley
Copy link
Author

Thanks, for showing me how to get to the fallback state. Also what is the meaning of the fby name?

OK, I'll work with those names and we can discuss it once again when I send the PR.

@pchiusano
Copy link

fby is short for 'followedBy'. It's a semi-standard name in stream processing / dataflow programming. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment