by aequitas · 7y ago · 0 comments
Though it is sometimes educational to apply only a single solution to all problems. Just to see how far you can stretch the usefulness of the tool.
A nice talk about where (and where not) to apply FP is this talk by Gary Bernhardt: https://www.destroyallsoftware.com/talks/boundaries
Some things really feel like objects, they've got little personalities, friends, parents... a whole list of talents and characteristics. OOP it is.
Other things are more like physics. It doesn't really need to have a name to exist, there doesn't need to be a container, it always acts predictably. Time to get functional.
I've also been using a tactic which I have no idea what it's really called, but I call it augmentation. I think of them like those crystals you add to your weapons in diablo or other RPGs. The gist is: a public readonly member which takes the object that holds it as an argument and has extra functionality. So like:
``` class Staff { public readonly flamePower = new FlameAugment(this); } (new Staff()).flamePower.doSomethingSpecialInvolvingStaff(); ```
class Staff {
public readonly flamePower = new FlameAugment(this);
}
(new Staff()).flamePower.doSomethingSpecialInvolvingStaff();EDIT: Just read into this a bit more (http://vasir.net/blog/game-development/how-to-build-entity-c...). It's something different. Kind of a mix between this and IOC.
Python lacks tail call optimizations and recursion dept is limited to ~1000, so if your algorithm is not O(log n) or better you may have to convert from recursive to iterative (there is a recipe that makes conversion trivial) but other than that I'm content with its FP-style features.
Another language is JavaScript. It uses function as parameter all the time but often just one time but not in a general way. Seems no JavaScript developer really seriously care about the term FP or OO.
Oh no, they care. The reason it doesn't seem like FP is big in the web dev world is because of reactive programming paradigm.
Reactive programming paradigm is designed so when one part of the program is processing it doesn't lock up the UI elements. This defaulting to being thread safe and lock free comes from the FP world.
Reactive programming is a marriage between functional programing and procedural programming paradigms and is incredibly popular. I suspect it is far more popular in the front end world than FP is in the back end world, showing their adoption, despite different, is stronger.
--
On the backend side of things, many of the popular languages have been slowly gaining FP toys, not just Python. C++, Java, Scala, and so on..
This is a healthy attitude to most paradigms. It's important to understand things from different perspectives because that enables us to frame problems in the correct fashion. If you only know C or other procedural languages, for instance, and are faced with a decision problem that is naturally expressed in a few lines of Prolog, you may end up writing a large chunk of Prolog to solve your problem. On the other hand, if you only know Prolog, you can solve the problem quickly, but may not be able to write a specialized program in C that executes that solution quickly enough for your target application.
FP Makes Everybody Think Hard. Go METH! (Andrew Yang want to Make American Think Hard)
For example, I write in an SSA-like style whenever possible i.e. no variable reuse unless declared in loops. Keeping programs as pure as is reasonable can also do wonders for future multithreading even if slightly slower single-threaded.
I think an understanding of what a modern compiler will and won't do can improve the tradeoff between speed and readability (when present, I think optimization-theatre is quite common amongst low level programmers - based on my own biases)