Oliver Eisenbarth
09/23/2022, 6:03 AMsimon.vergauwen
09/23/2022, 7:23 AM1-n of those functions via a configuration file would be.You mean generating
Validated.zip
for more than 9 parameters?
It's probably quite easy to write a little KScript that generates it, that is sadly the annoying thing about zip
and n-arity
functions like this in Kotlin.
There is however another way of composing zip
to achieve higher arities that is quite simple.
validatedA.zip(
validatedB,
validatedC.zip(vaidatedD, ::Pair)
) { a, b, (c, d) -> ... }
By nesting zip2 inside another zip2 and using an intermediate Tuple
you can compose 2x zip2
to achieve zip4
.
Same is applicable for zip9 + zip9
to get arity-18
.Oliver Eisenbarth
09/23/2022, 12:07 PMsimon.vergauwen
09/23/2022, 12:51 PMList<() -> Validated<E, A>>
than you can turn it into Validated<E, List<A>>
by using traverse
, an extension on Iterable
.
Depending on how you load the booleans/functions from the config you could perhaps do something like.
val config: List<Pair<Boolean, () -> Validated<E, A>>> = ....
val validationsToRun: List<() -> Validated<E, A>> =
config.mapNotNull { (boolean, function) -> function.takeIf { boolean } }
val result = validationsToRun.traverse { f -> f() }
stojan
09/23/2022, 1:46 PMOliver Eisenbarth
09/23/2022, 5:11 PMOliver Eisenbarth
09/27/2022, 5:38 AMval config: List<Pair<Boolean, (Thing) -> ValidatedNel<E, Thing>>>
and when I traverse those functions like in the example above, I get a List<Thing>
where I would like to have a Thing
. Zip does not work on lists of functions with arbitrary length...I must be missing something. 🤔
I also don't get why @simon.vergauwen chose validations functions to be () -> Validated<E,A>
? Does that mean my approach with passing or extending from the thing to validate is wrong?simon.vergauwen
09/27/2022, 6:24 AMOliver Eisenbarth
09/27/2022, 6:43 AMsimon.vergauwen
09/27/2022, 7:03 AM