2

Why I still allowed to do this?

  @spec url_hash(String.t) :: String.t
  def url_hash(url) do
    url = String.replace(url, ~r/\s/, "")
    Base.encode16(:crypto.hash(:md5, url), case: :lower)
  end

Instead of, what I think a proper functional programming paradigm?

  @spec url_hash2(String.t) :: String.t
  def url_hash2(url) do
    url_copy = String.replace(url, ~r/\s/, "")
    Base.encode16(:crypto.hash(:md5, url_copy), case: :lower)
  end

As you can see, I'm modifying parameter value inside the function body.

ardhitama
  • 1,949
  • 2
  • 18
  • 29

1 Answers1

-1

Pure functional languages wouldn't let you do this, and even some impure functional languages like Erlang. The convention in Erlang is to have code equivalent to this:

def url_hash(url) do
  url1 = some_func(url)
  url2 = some_other_func(url1)
  url3 = yet_another_func(url2)
  url3
end

The problem is that it gets pretty painful to reorder or add new lines of code when you do that. For this reason, Jose made the decision to allow variable reassignment. However, the original url string is unmodified - you've simply changed which String url points to.

Cody Poll
  • 7,690
  • 3
  • 27
  • 22