If you had software that was fundamentally impure ...
# arrow
c
If you had software that was fundamentally impure (eg. You have a few type of actions that you can do, but you only know which one should be executed, in which order, and how many times, from user input. How would you write it to be functional? An example could be to parse a background program's output and detect when it requires user input (confirmation, additional input...), and display that input in a UI to the user; ahead of time it's possible to know which actions exist, but not which will be used, how many times or even in which order
k
this strangely sounds like a compiler, which is inherently full of FP concepts. First you know all the keywords (/actions) you can parse the user written code but you will not be able to tell beforehand the orders of the keywords or which will be used. FP is not about being all pure (blood), but to separate the pure and impure code which gives confidence for us, developers, in the codebase.
How would you write it to be functional?
The best way is to write the program the way you would normally do, make sure it works and step by step apply FP concepts that you know. One way is separating pure functions! In your example there are 4 high level steps that I can see:
Read output
->
parse
->
detect
->
User input UI
While
read output
and the
input UI
fundamentally impure actions you can’t do too much about them but parse and detect are a very good candidates to make them pure,
👍 1
c
Oh, I see, that's really nice thinking! That was the kind of info I was searching for, because the OOP way would be to have the parse and detect step work on the output directly, which would mean everything is impure. Doing the same with a
Flow
or something similar could help a lot, thanks.
👍 2