Venkat
04/30/2020, 6:23 PMstojan
04/30/2020, 7:04 PMstojan
04/30/2020, 7:04 PMVenkat
04/30/2020, 7:06 PMstojan
04/30/2020, 7:10 PMVenkat
04/30/2020, 7:17 PMVenkat
04/30/2020, 7:20 PMstojan
04/30/2020, 7:21 PMValid
stojan
04/30/2020, 7:21 PMSemigroup.combine
is applied if both are Invalid
stojan
04/30/2020, 7:22 PMinterface NonEmptyListSemigroup<A> : Semigroup<NonEmptyList<A>> {
override fun NonEmptyList<A>.combine(b: NonEmptyList<A>): NonEmptyList<A> = this + b
}
stojan
04/30/2020, 7:24 PMor what if change my lamda function from {a,b -> a+b} to {a,b -> a-b}then you get a different result (in case both are valid)
Venkat
04/30/2020, 7:25 PMVenkat
04/30/2020, 7:25 PMstojan
04/30/2020, 7:26 PMstojan
04/30/2020, 7:30 PMtupled
returning TupleN
in case of success (as a specialization of map)Venkat
04/30/2020, 7:30 PMfun <A, B, Z> map(
a: Kind<F, A>,
b: Kind<F, B>,
lbd: (Tuple2<A, B>) -> Z
): Kind<F, Z> =
a.product(b).map(lbd)
Venkat
04/30/2020, 7:33 PMfun <A, B> Kind<F, A>.product(fb: Kind<F, B>): Kind<F, Tuple2<A, B>> =
fb.ap(this.map { a: A -> { b: B -> Tuple2(a, b) } })
fun <A, B> Kind<F, A>.map(f: (A) -> B): Kind<F, B>
stojan
04/30/2020, 7:35 PMstojan
04/30/2020, 7:36 PMVenkat
04/30/2020, 7:38 PMVenkat
04/30/2020, 7:38 PMVenkat
04/30/2020, 7:38 PMstojan
04/30/2020, 7:39 PMVenkat
05/01/2020, 1:35 AMfun <E, A, B> ValidatedOf<E, A>.ap(SE: Semigroup<E>, f: Validated<E, (A) -> B>): Validated<E, B> =
fix().fold(
{ e -> f.fold({ Invalid(SE.run { it.combine(e) }) }, { Invalid(e) }) },
{ a -> f.fold(::Invalid) { Valid(it(a)) } }
)
I think this applicative type class method doing the trick. ValidatedOf Datatype extends applicative typeclass method ap() which takes semigroup and combines invalid results as you said, but the fold function inside it looks weird.stojan
05/01/2020, 8:40 AMVenkat
05/01/2020, 9:28 AMstojan
05/01/2020, 10:05 AMValidated
, (the implicit one, because the function is an extension function, and f
). And you need to unwrap both of them.
So again, like in my example you need to handle 4 cases.Venkat
05/01/2020, 10:08 AM