There's `fold` for things like that.
# announcements
k
There's
fold
for things like that.
s
maybe
reduce
since the transform is just
acc + next
until the end?
k
Doesn't work for empty lists!
s
oh, interesting - that makes sense, but is there not a safe way to call
reduce
on a potentially empty list?
or at least something more idiomatic than
if (list.isEmpty()) ... else reduce ...
?
e
Maybe sth like this?
Copy code
list.run {
  if (it.isEmpty) return 0
  else return it.reduce(...)
}
s
that’s just what I wrote but wrapped in a
run
block
you’d just
Copy code
return if (list.isEmpty()) BigDecimal(0) else list.reduce { ... }
which is fine, I suppose, it just feels a little funny to me that
reduce
is kinda unsafe in that regard
but I suppose there isn’t a good alternative like there is with
fold
k
reduce
is safe, it just returns
null
if empty.
list.reduce(...) ?: BigDecimal.Zero
is the same as the fold way.
m
k
Damm that is a strange design decision.