0

As the title says, regarding a part of functional programming I have yet to see anyone discuss or answer areas of applications which isn't possible to write pure functions. I feel that it is indeed impossible to write applications with pure functions if there are dependencies on operations outside the control of the actual application.

Examples of this would be:

  • User permissions of the user starting the application
  • Ports already being used by other applications on machine
  • Hard drive being full
  • Files not being able to be written to due to file in use by other applications
  • Network issues

These are just a few that spring to mind.

I would like to believe that writing code which could never be pure should be grouped and written in a way to maximize other parts of code to fulfill pure function status. So the question is: Is it possible for an application to be built by only pure functions and if not what are things that can stop this and how would we approach these matters?

basickarl
  • 37,187
  • 64
  • 214
  • 335
  • Functional programming doesn't eliminate exceptions. You have to handle them as you would do in imperative style, but with other means. –  Dec 02 '19 at 09:41
  • _should be grouped and written in a way to maximize other parts of code to fulfill pure function status_ - exactly. You achieve that by just wrapping any impure effect in a function, because its body is only evaluated on demand, i.e. lazily evaluated. Now you can defer the evaluation and thus the release of effects to the edge of your application. You can use thunks `() => ...` if your expression is already complete and it is only about deferring it. Otherwise use functions of arbitrary arity. –  Dec 02 '19 at 09:48
  • Every effect acts differently and therefore you need to specify how you can compose the corresponding functions. This is where combinators that are part of the functor, applicative and monad typeclass come into play. –  Dec 02 '19 at 09:53

0 Answers0