(I know it seems a pretty obvious question, but I can't find satisfactory answers that make sense to me at least.)
(I know it seems a pretty obvious question, but I can't find satisfactory answers that make sense to me at least.)
Coming from imperative languages especially (speaking for myself), have to re-think/re-learn how you write code, but it does end up being a worthwhile investment
Short syntax:
Enum.map [1,2,3], &(&1 * &1)
Long syntax: Enum.map [1,2,3], (fn num ->
num * num
end)
Or even: square_me = &(&1 * &1)
Enum.map [1,2,3], square_me
square_me_too = fn(num) -> num * num end
Enum.map [1,2,3], square_me_too
Defining a function separately (e.g. even in another module) and referencing it: def square(num) do
num * num
end
def doing_something() do
...
Enum.map [1,2,3], &square/1
Enum.map [1,2,3], &SomeOtherModule.square/1
...
end class Array
def square
map {|e| e * e}
end
end
Why storing code in a seperate module is a superior way than storing code in its class?Think of:
items = ["file_a", "file_b", "file_c"]
Enum.map items, &HelpfulDownloaderModule.download/1
download/1 is probably pretty complex and/or can be of use in other pieces of code.Bad oop is clearly inferior to good functional. ie React. But if OOP is able to do functional as well, shouldn't OOP win on the long run?
If you try to code functional in an OOP language, you would have to make sure that no third party modules (or coworkers accidentally) write code that "misbehaves" in such aspects. Just like you currently have to make sure that the Ruby code you use is threadsafe.
With immutability you gain all those features baked into Erlang/OTP (scalability, reliability).
`List.get_elem` you can write an higher order function `List.get_Nth_elm` which takes `List.get_elem` as an agrument .