Functional-core-imperative-shell icon indicating copy to clipboard operation
Functional-core-imperative-shell copied to clipboard

[Discussion] Is it sometimes good that functions have different "color"?

Open olleharstedt opened this issue 4 years ago • 4 comments

Article: https://journal.stuffwithstuff.com/2015/02/01/what-color-is-your-function/

Argues that functions should not be different between sync and async. But in terms of pure/effectful, maybe it's a good thing that functions cannot be mixed however which way? The "color" of a function might be used to enforce a functional core.

olleharstedt avatar Sep 25 '20 11:09 olleharstedt

Yes that is the gist of my thinking too. But it quickly becomes a blurred discussion as a pure function may very well take a function as a parameter, executing code that has side-effects - which may be fine from a testing perspective, if the function can be injected into the code and the side-effect removed.

But such a code pattern, effectively, means that a pure function may need to take n extra function parameters... really hurting readability and everything will look completely strange.

That being said, you can do stuff with the pattern, e.g. a simple twitter reader has been created as an example of how to apply the pattern.

The article is relevant so I suggest you sprinkle some text around that link, and put it in a PR ;)

I've implemented most of the fundamental GIT functionality in less than 500 lines of C# - https://github.com/kbilsted/KBGit/blob/feature/usefilesysstem/Git2.cs - perhaps it was fun rewriting it with this pattern in mind, and enhancing the error handling along the way. Anyone interested in a code jam session?

kbilsted avatar Sep 25 '20 16:09 kbilsted

But it quickly becomes a blurred discussion as a pure function may very well take a function as a parameter, executing code that has side-effects

No, such a code is not pure. If it can be effectful it's not pure - it's only pure if it's always pure. The input lambda has the color "effectful", and if any branch in the function runs it, the function is effectful.

A pure function can for example construct and return an effectful promise while remaining pure. Or, effect factories are still pure.

olleharstedt avatar Sep 25 '20 18:09 olleharstedt

Can you extend those thoughs into some C# code. For example, a small interactive console program

  • first action is to show a list of shapes (e.g. box, circle)
  • the user may select 0 for box, 1 for circle etc.. if he chooses non-number or out of range validate error and ask again
  • then ask for what to do, 0 calculate area, 1 calculate circumference,..
  • then depending on shape, ask for size of sides, radius or whatever is relevant for chosen size
  • calculate and show to user

(is this even a good example? ;)

kbilsted avatar Sep 25 '20 18:09 kbilsted

I've been trying to hunt down a good use-case for this design pattern, but it's hard! I can give it a try, but off the top of my head, couple of classes with toString implemented, then a factory class to map number to shape? Since shapes have no IO, it's pretty clear-cut. Another use-case I've used is a function that takes a filename, reads a blob from the filesystem and then uses curl to save it to a cloud service, including logging and exceptions for errors (it's actually from one of your links).

olleharstedt avatar Sep 26 '20 15:09 olleharstedt