3 ms·
Yep, FP is really nice. A lot of what people think is going to be different is actually not that different from OO. Elixir especially ends up looking a lot li
by sodapopcan 5y ago
Yep, FP is really nice. A lot of what people think is going to be different is actually not that different from OO. Elixir especially ends up looking a lot like OO with its pipe operator ("string" |> String.split() vs "string".split(), for eg). Some of the major wins are immutability (which is actually very easy to grasp and a lot of people write in an immutable style in OO) and the fact you can every functions dependencies right there. The latter is actually huge for me as I always hated jumping around files, trying to see where things are coming from. FP minimizes this. There are lots of other niceties (some language-specific) but those are some of my faves.
Sometimes things take a little longer to write as you have to be slightly more verbose, but it's so much quicker to understand when coming back to it.
- h0l0cube 5y agoWhat's better (and more flexible) than Object Oriented is Polymorphism. The pipe operator |> that you mention is great example because it does something in OO that is typically done using a 'fluent interface'[0] often used in 'Builder' classes (i.e., the class methods return the object so that it can be chained together). The great thing about the pipe operator is that it's much more simple and flexible, it simply passes the result of the prior function as the first argument to the second. This means, unlike with a fluent API, you can pipe together functions from completely different modules: :crypto.hash(:md5, some_binary) |> :base64.encode_to_string() Elixir also has protocols[1], which allow a function to be used across a variety of types (similar to obj-c/Swift). Which again is more flexible, as the functionality can extend a 'class' that you don't control. Elixir also has behaviors[2], which just guarantee that a module contains a number of functions, something akin to interfaces Java. Dialyzer can take advantage of behaviors and protocols to enforce typing constraints. [0] https://en.wikipedia.org/wiki/Fluent_interface https://en.wikipedia.org/wiki/Fluent_interface [1] https://hexdocs.pm/elixir/1.12/Protocol.html https://hexdocs.pm/elixir/1.12/Protocol.html [2] https://hexdocs.pm/elixir/1.12/Behaviour.html https://hexdocs.pm/elixir/1.12/Behaviour.html
- sodapopcan 5y agoYes! Another thing I love about FP is the simplicity of terms. So like how you mention a return value can flow through different modules, if you wanted to do something like that in OO, you’d have to come up with a concept for what the new object is (ie, Service or what have you) which in turn has to take an object, breaking the whole “data and behaviour combined” idea. In FP, you just have modules. You don’t have think much past that a module is a group of functions that work on a certain data structure and other modules might also work on that data structure and that is fine (though of course you’ll want to hide those behind another module and delegate if you are dealing with a complex model, but that is a whole other problem that also exists in OO).