I am working on creating some kafka extension func...
# arrow
c
I am working on creating some kafka extension functions and I want to take a
KStream<Either<Error, Result>>
and create a
Pair
of
KStream<Error>
and
KStream<Result>
. I was not sure how to accomplish this using Either from arrow. We have some helpers in scala that do this now, and for my learning I wanted to do the same in kotlin - in scala it looks something like this -
Copy code
def forkValuesEither: (KStream[K, LV], KStream[K, RV]) = (
      kStream.flatMapValues(_.left.toSeq),
      kStream.flatMapValues(_.right.toSeq)
    )
but I was not sure how to get to the value in arrow/kotlin of the either to return.
r
What is KStream and does it have an extension for Traverse or Foldable?
is this from the Kafka Streams libs?
c
yes - from kafka streams api
not sure on the Traverse/Foldable question..
s
Looks like the same code should also work for Kotlin/Arrow
What is the signature of
flatMapValues
?
c
Copy code
def flatMapValues[VR](mapper: V => Iterable[VR]): KStream[K, VR]
I was not able to do the exact same - let me try again and see what the error is
s
Oh nevermind, it’s the
_.left.toSeq
that threw me off
It’s doing
leftProjection
and then
toSeq
which is
Copy code
fun <A, B> Either<A, B>.leftIterable(): List<A> = when(this) {
   is Either.Left -> listOf(l)
   is EIther.Right -> emptyList()
}
Which means you can use
flatMapValues
to filter
left
and
right
using
Seq
but that’s a little hacky IMO 😄
So maybe you can also do something like this
c
I think thats exactly what the scala leftProjection is doing
s
Copy code
fun <A, B> KStream<A>.mapFilter(f: (A) -> B?): KStream<B> = ...
c
this is the toSeq on the LeftProjection from scala
Copy code
def toSeq: Seq[A] = e match {
      case Left(a) => Seq(a)
      case _       => Seq.empty
    }
s
And then you could do
Copy code
kStream.mapFilter { (it as? Left)?.l }
let me write a complete snippet 😉 I’m not familiar with Kafka tho
c
thanks! - I think your suggestions are already getting me further 😉
kafka knowledge or not - I think you see/understand the types better than I
s
Here you go
I mocked KStream using List but all the signatures and functions are there
👏 1
Was a bit confusing originally with the left projection in Scala 😄 took me a second
c
@simon.vergauwen - what a huge help, thanks for this! Really can’t say enough about Arrow and its community! Thanks for taking the time!
❤️ 1
s
Glad I was able to help 👍