0

I came across a piece of scala code which makes use of map function in a very unexpected manner:

myList.map(item => someImpureFunction(item))

someImpureFunction has return type Unit.

Is it an acceptable way of doing FP?

Mandroid
  • 6,200
  • 12
  • 64
  • 134

1 Answers1

4

If someImpureFunction returns Unit it implies that the function performs some side effect e.g. writes to a database or standard output, so in that case, it's better to use:

myList.foreach(item => someImpureFunction(item))

If you want to write pure FP code you should take a look at deferring the execution of side effects (a good example is Cats IO)

Gaël J
  • 11,274
  • 4
  • 17
  • 32
Bondarenko
  • 225
  • 1
  • 7