im a FP noob. I have a bunch of nested Mapper func...
# functional
u
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
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
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
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)
?