Live data from Hacker News

Practicing Elixir or any programming language

ericdouglas.github.io

51–59 of 59 posts

Re: Practicing Elixir or any programming language

#51
post #48

Great article, thanks! I'm not an expert Alchemist by any means, but whenever I see a `cond`, I try to see if it can be refactored out using pattern matching (after I was picked up on the same thing during an interview!) defp formatted_hour("AM", "12"), do: "00" defp formatted_hour("AM", hours), do: hours defp formatted_hour("PM", "12"), do: "12" defp formatted_hour("PM", hours), do: 12 + String.to_integer(hours)

I agree though one thing I would caution is it is good practice to always return the same type from a function even though it is an untyped language. The whole thing can be boiled down even more if pattern matching is used in parsing the initial string. def convert( > ":" > ":" > >) do "#{formatted_hour(h,period)}:#{m}:#{s}" end defp formatted_hour(hours,"PM"), do: "#{String.to_integer(hours) + 12}" defp formatted_ho…

Good point! If you're going to pattern match in the initial string then who even needs the second function?

    def convert("12:"  >  ":"  >  "AM") do
      "00:#{m}:#{s}"
    end

    def convert(>  ":"  >  ":"  >  "PM") do
      h = 12 + String.to_integer(h)
      "#{h}:#{m}:#{s}"
    end

    def convert(>  ":"  >  ":"  >  "AM") do
      "#{h}:#{m}:#{s}"
    end

Whether that's an improvement or not is another matter :)

Re: Practicing Elixir or any programming language

#52
post #12

Earlier quoted context omitted.

It's important to be able to look at a problem and think "i bet I could find a function in the standard library that could help me solve this problem" or "I should just solve this from scratch with the basic functions I know, like map/filter/reduce/etc. So I disagree somewhat here.

Yeah, that's the point: "i bet I could find a function in the standard library that could help me solve this problem"

Yeah, in my opinion that's an important skill to have when learning a language. I already know how to write quicksort, writing it in Elixir will give me some sense of understanding of the language, but knowing how to find it in the standard library will make me more efficent of a programmer in Elixir.

Re: Practicing Elixir or any programming language

#53

I am a fan of http://www.rubykoans.com for Ruby. I am thinking about developing something similar using the Go playground for that language, and maybe an alternative for Rust. Where I'd want to stretch it is some data structure and algorithm exercises, maybe even some design patterns. I'd love to be able to have a common environment where I can start to learn any one of multiple languages using similar exercises.

I would love something like that for Go. Just recently completed Python Koans and it was a good way to get into TDD.

Re: Practicing Elixir or any programming language

#54

I actually really enjoy doing katas on https://www.codewars.com/ , especially when I'm ramping up on a language. You solve a short problem, and then immediately see how other people solved it. I'll frequently write something and then see a solution that uses a library function I wasn't aware of, or a solution that's similar but better follows the language conventions. It's really helpful to solve something short and…

I do that with Exercism and Elixir. It's pretty awesome.

been meaning to try exercism for a while, thanks for the reminder

Re: Practicing Elixir or any programming language

#55
post #13

I always find it interesting how people learn so differently. Personally, I learn a language by building something in it. I want a feel for it, before digging deep and learning about theory. I only do that stuff much later, after I'm relatively proficient in the language. This is probably why I wasn't very good at picking up musical instruments. I was never satisfied learning at a slow pace and only being able to pla…

Spoken languages are like that too. A 3 year old doesn't know what verbs are. They just speak. Another interesting thing is how people forget their path to personal journey regarding learning. "I did `y`, but you should do `x` because now that I look back I should have done `x`." "But, but, you did `y`! "Yes, but `x` is the way to go." You mentioned musical instruments: great topic. The very best musicians have alway…

> Spoken languages are like that too. A 3 year old doesn't know what verbs are. They just speak.

Not sure how you meant this to be understood, but a 3 year old definitely knows that there are different word classes that behave differently. They don't know that they're called verbs, nouns, adjectives, etc. but they do correctly (to a large enough extent) use grammar applying specifically to the different word classes.

Re: Practicing Elixir or any programming language

#56
post #48

Great article, thanks! I'm not an expert Alchemist by any means, but whenever I see a `cond`, I try to see if it can be refactored out using pattern matching (after I was picked up on the same thing during an interview!) defp formatted_hour("AM", "12"), do: "00" defp formatted_hour("AM", hours), do: hours defp formatted_hour("PM", "12"), do: "12" defp formatted_hour("PM", hours), do: 12 + String.to_integer(hours)

I agree though one thing I would caution is it is good practice to always return the same type from a function even though it is an untyped language. The whole thing can be boiled down even more if pattern matching is used in parsing the initial string. def convert( > ":" > ":" > >) do "#{formatted_hour(h,period)}:#{m}:#{s}" end defp formatted_hour(hours,"PM"), do: "#{String.to_integer(hours) + 12}" defp formatted_ho…

I really liked this:

    "#{String.to_integer(hours) + 12}"
Used it now refactoring the code, many thanks for your suggestion!

Re: Practicing Elixir or any programming language

#57
post #48

Earlier quoted context omitted.

I agree though one thing I would caution is it is good practice to always return the same type from a function even though it is an untyped language. The whole thing can be boiled down even more if pattern matching is used in parsing the initial string. def convert( > ":" > ":" > >) do "#{formatted_hour(h,period)}:#{m}:#{s}" end defp formatted_hour(hours,"PM"), do: "#{String.to_integer(hours) + 12}" defp formatted_ho…

Good point! If you're going to pattern match in the initial string then who even needs the second function? def convert("12:" > ":" > "AM") do "00:#{m}:#{s}" end def convert( > ":" > ":" > "PM") do h = 12 + String.to_integer(h) "#{h}:#{m}:#{s}" end def convert( > ":" > ":" > "AM") do "#{h}:#{m}:#{s}" end Whether that's an improvement or not is another matter :)

Crazy stuff! :D

I'm wondering how our future-self would feel about such cleverness haha

Re: Practicing Elixir or any programming language

#58

How hard is it to design your page without javascript needing to be enabled? I now can't be bothered to see what you have to say. It is not worth my while.

Just changed the blog theme - it now works perfectly with JS disabled - serious, huge thanks for telling me this, I wasn't aware of that issue.

Re: Practicing Elixir or any programming language

#59
post #52

Earlier quoted context omitted.

Yeah, that's the point: "i bet I could find a function in the standard library that could help me solve this problem"

Yeah, in my opinion that's an important skill to have when learning a language. I already know how to write quicksort, writing it in Elixir will give me some sense of understanding of the language, but knowing how to find it in the standard library will make me more efficent of a programmer in Elixir.

Totally!
Post reply on HN