Re:I made a Phoenix webapp as a veteran Rails dev
by hartator on Sunday May 21, 2017 @02:13PM
(#14388368)
I want to beleive that, but I don't know. For example, in Elixir, you would write something like this to square a list: 'Enum.map [1,2,3], $($1 * $1)'. In Ruby: '[1,2,3].map {|e| e * e}'. The Ruby seems more natural to me, but maybe it's the years of OOP speaking and not reason.
[ Parent
| on HN ]
Re:I made a Phoenix webapp as a veteran Rails dev
by tsukaisute on Sunday May 21, 2017 @02:38PM
(#14388498)
Btw, there are a number of ways to write that expression depending on the complexity of the function
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
Re:I made a Phoenix webapp as a veteran Rails dev
by hartator on Sunday May 21, 2017 @04:06PM
(#14388980)
yeah and in Ruby, you can do:
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?
Re:I made a Phoenix webapp as a veteran Rails dev
by grzm on Sunday May 21, 2017 @04:19PM
(#14389043)
You've now altered a global object which other code may rely on. Indeed, you may overwrite someone else's Array#square method (or have yours overwritten). As a separate module, the additional code is logically distinct. You can find many discussions about monkey-patching elsewhere. It's not my intent here to argue one way or the other, just to answer your question as to why one may choose package the code as a separate module.
Re:I made a Phoenix webapp as a veteran Rails dev
by tsukaisute on Sunday May 21, 2017 @04:26PM
(#14389090)
If it is to be reused by other modules, or if it is complex (and likely itself needs to be split up into smaller functions)
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.