We added this to our codebase: ```fun <A, B>...
# arrow
m
We added this to our codebase:
Copy code
fun <A, B> Collection<Either<A, B>>.allLefts(): List<A> = this.mapNotNull { either -> either.fold({ it }, { null }) }
Do you have any ideas for better ways to do it with Arrow or should something be added? Use case here is to not do short circuit, so we can't use traverseEither.
r
Hi @Marius Kotsbak, This looks good to me and maybe some of those utilities are useful to others as extensions over Iterable.
Copy code
fun <E, A> Iterable<Either<E, A>>.leftValues(): List<E>
fun <E, A> Iterable<Either<E, A>>.rightValues(): List<A>
fun <E, A> Iterable<Either<E, A>>.filterLeft(): List<Left<E>>
fun <E, A> Iterable<Either<E, A>>.filterRight(): List<Right<A>>
m
You mean it should be added to Arrow?
r
I think it’d be useful as Iterable extensions for Arrow core. I have also ran into this case where I wanted all right or left values inside an iterable. If someone thinks it’s not a good idea also happy to hear cons but initially I think it’s useful.
s
💯
j
Regarding naming: In
fp-ts
(https://github.com/gcanti/fp-ts), this is part of the
Filterable
typeclass:
Copy code
export interface Filterable<F> extends Functor<F>, Compactable<F> {
  readonly partitionMap: <A, B, C>(fa: HKT<F, A>, f: (a: A) => Either<B, C>) => Separated<HKT<F, B>, HKT<F, C>>
  readonly partition: Partition<F> // <A>(fa: HKT<F, A>, predicate: Predicate<A>): Separated<HKT<F, A>, HKT<F, A>>
  readonly filterMap: <A, B>(fa: HKT<F, A>, f: (a: A) => Option<B>) => HKT<F, B>
  readonly filter: Filter<F> // <A>(fa: HKT<F, A>, predicate: Predicate<A>): HKT<F, A>
}
I did not check with Haskell but would expect similar naming there. Personally, I think the following would look most consistent with those conventions:
Copy code
fun <E, A, B> Iterable<A>.filterLeft(p: (A) -> Either<E, B>): List<E>
fun <E, A, B> Iterable<A>.filterRight(p: (A) -> Either<E, B>): List<B>