← All series

Series contents

Log in to track your progress

  1. 1. Welcome to Alchemist Camp Episode
  2. 2. Lesson 1: The guessing game Episode
  3. 3. Solution 1: The Greeter Episode
  4. 4. Lesson 2: Word Count Episode
  5. 5. Word Count ULTIMATE EDITION! Episode
  6. 6. Minimal Todo List Part 1 Episode
  7. 7. Elixir lists vs Elixir tuples Article
  8. 8. The Image Snatcher Episode
  9. 9. Minimal Todo List Part 2 Episode
  10. 10. Elixir maps made effortless Article
  11. 11. Fibonacci with tail recursion Episode
  12. 12. Mini Markdown Episode
  13. 13. Extending Mini Markdown Episode
  14. 14. Alchemy Markdown Episode
  15. 15. Using mix to modularize and add tests Episode
  16. 16. Command line applications with escript Episode
  17. 17. Caboose, a simple Unix tail clone Episode
  18. 18. Game board (Tictac Part 1) Episode
  19. 19. Structs: Maps with checks and default values Article
  20. 20. Deck generation, Pythagorean Triplets and more Episode
  21. 21. Whiteboarding the Tictac state machine Episode
  22. 22. Game state machine (Tictac Part 2) Episode
  23. 23. Modelling the memory game Episode
  24. 24. Finishing Tictac and making a CLI Episode
  25. 25. Threatened by rooks, bishops and knights Episode
  26. 26. A worker to log stats from a YouTube’s API Episode
  27. 27. Constructing bitmap files with Elixir Episode
  28. 28. Making bitmaps with color palettes Episode
  29. 29. Flattening lists recursively in Elixir Episode

Lesson 2: Word Count

Overview

Our second project is a simple command line script that counts the words in a file, much like the Unix command wc.

We'll see some differences between writing Elixir modules and one-off scripts and we'll see how to read a file. The most difficult part of the lesson is deciding how to split the file into words. We'll get a taste of the both the joy and the terror of regular expressions in the process.


Source code for this episode


The process

  • Use IO.gets to ask user the name of a file to open
  • Use String.trim to get rid of trailing enter they press
  • Split the string into words with String.split and a regular expression
  • Filter out non words with Enum.filter
  • Count the words with Enum.count

The middle portion of the video also includes a detour showing some basics of regex and working with them in Elixir.

Elixir's mix utility is not used at all in this lesson. Neither is escript. Both are covered in a future lesson but there's some value in working with the very basics first, so that's what we did.

Challenge 2

See if you can extend the script build in this lesson so that the user can optionally count the number of lines or characters a file instead of the number of words.

Hint: The challenge in this lesson will be much easier if you look at the docs for Enum and see what Enum.each and Enum.at do.

(solution here)

Open standalone page ↗

7 Comments

For those who are curious about Code.load_file/2 being deprecated, don't worry! It won't be removed until Elixir 2.0, which isn't even on the road map and Jose has said Elixir is "done".

Going forward, the function is renamed to Code.compile_file/2, and the usage is still the same. Note that the 2nd argument defaults to nil, which is why it can be called with just one argument as in this video.

- alchemist · 6 years ago

Nice one! thanks @alchemist

- jtoulouse · 6 years ago

@abdulrahman, I have this in my VS Code settings.json (open it with cmd/ctrl + ,)

 "editor.formatOnSave": true,
- alchemist · 6 years ago

good lecture by the way auto formatting when save doesn't work for me i need to click Alt + Shift + F

- abdulrahman · 6 years ago

@zababa: I like the way you think! If you look closely at the video, you'll see that the terminal was Power Shell and not bash or similar, though. I think this would be the command:

type word_count.exs | (elixir word_count.exs)
- alchemist · 5 years ago

thank you for episodes

- Neon23 · 2 years ago

Great video! To accelerate testing, you can use echo "word_count.exs" | elixir word_count.exs to not have to rewrite "word_count.exs" to the program every time.

- Zababa · 5 years ago