0

I am investigating Functional Programming, namely Arrow-kt with Kotlin

my use case is as follows:

I want to perform an Action and return the associated Reaction

Both Action and Reaction have associated Side Effects

the sequence of events is

1). On accepting the Action immediately tigger the associated Actions Side Effects

2). Perform the Action and obtain the Reaction

3). Trigger the Reactions Side Effects

4). Return the Reaction result.

Additional factors to take into account when the Action is requested to be performed

a). When the Action has never been performed; complete the entire sequence, e.g. steps 1 - 4.

b). When the Action is being performed; short circuit the sequence so that steps 2 - 4 are not completed

c). When the Action has completed; short circuit steps 1 - 3 and return the Reaction result

The Side Effects mentioned above include Logging, Analytics, and User Interface effects.

I've a feeling that my requirement that the Action and Reaction Side Effects are triggered immediately means that a Functional Programming solution is not appropriate for my use case

The Arrow-kt presentations and blogs I have found describe how Either can be used to short circuit sequences based on Error Conditions being raised or using Option with Some and None to achieve the same type of behaviour.

What I am having difficulty with

i). is where to start????

ii). with four steps to complete, how to control/manage "short circuiting" so that either steps 1 - 4, just step 1, or just step 4 is completed

Is it possible to achieve the desired behaviour using Functional Programming?

Hector
  • 4,016
  • 21
  • 112
  • 211
  • 1
    As I understand it, you can't do that in pure FP, because pure FP doesn't _have_ side-effects… – gidds Nov 27 '19 at 20:25
  • @gidds Functional programming does have side effects. They're just isolated to a small part of your program so that everything not in that small part can easily be tested. A program with no side effects basically can't do anything. Outputting to the screen is a side effect. Getting user input is a side effect. Reading from a file is a side effect. Naturally if you can't display anything or get user input, it's worthless. – user1713450 Dec 10 '19 at 20:47

1 Answers1

3

The best starting point for your case is https://arrow-kt.io/docs/patterns/error_handling/

If you'd like to do side-effects such as logging, the easiest thing you can do is make all of them suspend functions to mark them as impure. That way your whole program will be a chain of suspend function calls.

You haveto execute that first suspend function in the main, or the beggining of the business logic such as Activity#onCreate, or your Controller. To execute that first suspend function use IO { myBusinessLogic() }.unsafeRunAsync { }

El Paco
  • 546
  • 2
  • 6
  • Personally I don't like the convention of "suspend means impure" - there are plenty of things you'd put in a suspend function that is pure. Hashing a large ByteArray, for example, I'd put in a suspend function. Personally, I mark my side effects differently. Reading from a file, for example, belongs in an IO monad. Then you always know if you call a function has side effects because its return type is IO (for example). – user1713450 Dec 10 '19 at 20:50