https://kotlinlang.org logo
#functional
Title
# functional
u

ursus

02/21/2021, 7:04 PM
im a FP noob. I have a bunch of nested Mapper functions, however one (QuaxMapper) needs to be varied depending on what app it is. So basically polymorphism, if the mappers were objects and other mappers ctor injected. What should I do now? Telescope a lambda all the way?
Copy code
fun FooMapper(netFoo: NetFoo, quaxMapper: (NetQuax) -> Quax): Foo {
	val meh = MehMapper(foo.meh, quaxMapper)
	val bar = BarMapper(foo.bar)
	return Foo(meh, bar)
}

fun MehMapper(netMeh: NetMeh, quaxMapper: (NetQuax) -> Quax): Meh {
	quaxMapper(netMeh.quax)
	...
}
s

stojan

02/22/2021, 8:12 AM
Assuming the mappers are pure functions, don't pass them in as params, use them directly... Except for the one that changes, the polymorphic one
w

wakingrufus

02/23/2021, 9:49 PM
I see nothing wrong with this. this is just higher-order functions. it also can make your code easier to isolate for unit tests as opposed to calling functions directly. but in this case hard to tell b/c not much context or semantics to go off of) you can just pas a Quax into MehMapper instead of the quaxMapper
Copy code
fun FooMapper(netFoo: NetFoo, quaxMapper: (NetQuax) -> Quax): Foo {
	val meh = MehMapper(foo.meh, quaxMapper(foo.meh.quax))
	val bar = BarMapper(foo.bar)
	return Foo(meh, bar)
}
fun MehMapper(netMeh: NetMeh, quax: NetQuax): Meh {
	...
}
u

ursus

02/24/2021, 5:07 AM
Yea sorry I didnt say, Foo and MehMappers are in core library. Implementation of QuaxMapper is in each app. So you'd cool with every call site looking like
FooMapper(foo, DI.quaxMapper)
?
2 Views