sarvagya agarwal
11/10/2022, 4:32 PMvar list = serviceA.call(list)
list = serviceB.call(list)
list = serviceC.call(list)
list = serviceD.call(list)
Kevin Healy
11/10/2022, 4:35 PMlist.call(serviceA).call(serviceB).call(serviceC).call(serviceD)
Casey Brooks
11/10/2022, 4:38 PM.fold
operation
listOf(
serviceA,
serviceB,
serviceC,
serviceD,
).fold(initial = emptyList()) { acc, service -> service.call(acc) }
Klitos Kyriacou
11/10/2022, 4:47 PMKevin Del Castillo
11/10/2022, 4:48 PMcall
function that receives a listKlitos Kyriacou
11/10/2022, 4:50 PMcall
function.Kevin Del Castillo
11/10/2022, 4:51 PMKevin Del Castillo
11/10/2022, 4:51 PMKevin Healy
11/10/2022, 4:52 PMCasey Brooks
11/10/2022, 4:53 PMlistOf()
down to multiple lines, it’s still only 1 statement which is easily understandable. I tend to break these kinds of lists down because I use IntelliJ’s “duplicate line” feature a lot, and this makes it easier
Really, the snippet is just 1 “line”, or 1 “statement”. The listOf()
is just boilerplate, those services could be injected via DI as a list so that wouldn’t necessarily actually be at the point where you do the sequence of .call
. So my snippet reduces 4 statements down to 1, but it does require that one understands what the .fold
operation is doing to actually make it more understandableKevin Del Castillo
11/10/2022, 4:54 PMKevin Healy
11/10/2022, 4:58 PMKevin Del Castillo
11/10/2022, 4:59 PMCharles Flynn
11/13/2022, 9:26 AMval newList = list
.let { serviceA.call(it) }
.let { serviceB.call(it) }
.let { serviceC.call(it) }
.let { serviceD.call(it) }