Since the summer of 2020 I have been publishing Elixir tips three times a week on Twitter. These are small and focused code snippets that show you some cool (and possibly lesser known) things about Elixir and tools from around the ecosystem. I found that I often needed to lookup some of my old tips for my own purposes and had a hard time tracking them down in my timeline. For that reason, I will be republishing all my tips here and link them back to the original Tweets given that some of these tips have spawned really great conversations. Enjoy!
⚗️ Elixir Tip #31 - Project Priv Dir ⚗️
— Alex Koutmos (@akoutmos) November 16, 2020
You can conveniently package arbitrary files along with your libraries and applications inside the priv dir
and fetching the path at run time using the Erlang standard library.#myelixirstatus #elixirlang #elixir #erlang pic.twitter.com/rMSC3l6Rka
⚗️ Elixir Tip #30 - ExUnit assert_receive ⚗️
— Alex Koutmos (@akoutmos) November 13, 2020
Often times in your ExUnit tests you'll want to ensure that you receive a certain message. You can use assert_receive to pattern match against the received message and check the structure.#myelixirstatus #elixirlang #elixir #erlang pic.twitter.com/F24dy0b1Ky
⚗️ Elixir Tip #29 - Running mix commands in an umbrella app ⚗️
— Alex Koutmos (@akoutmos) November 11, 2020
Ever wondered how you can run mix commands from the context of a specific umbrella app? All you need to do is add the --app flag to mix cmd and you are good to go!#myelixirstatus #elixirlang #elixir #erlang pic.twitter.com/ZwA88Btjsh
⚗️ Elixir Tip #28 - Simple Struct inspect protocol ⚗️
— Alex Koutmos (@akoutmos) November 9, 2020
Using the derive attribute in your Struct module, you can specify which fields from the Struct are visible when inspecting the struct.#myelixirstatus #elixirlang #elixir #erlang pic.twitter.com/VU4MLfYGCA
⚗️ Elixir Tip #27 - Concurrent port scanner ⚗️
— Alex Koutmos (@akoutmos) November 6, 2020
If you ever wondered how to write a concurrent port scanner in Elixir, then this tip is for you! Leverage Task.async_stream to have the workload processed concurrently.#myelixirstatus #elixirlang #elixir #erlang pic.twitter.com/zABummFC7Y
⚗️ Elixir Tip #26 - CLI argument parsing ⚗️
— Alex Koutmos (@akoutmos) November 4, 2020
The OptionParser module provides a convenient way of parsing CLI args and casting them to the appropriate types. Very helpful when writing exs scripts or Mix tasks https://t.co/zaqw53pYLV.#myelixirstatus #elixirlang #elixir #erlang pic.twitter.com/AkmmmazEvB
⚗️ Elixir Tip #25 - Lazy Keyword get defaults ⚗️
— Alex Koutmos (@akoutmos) November 2, 2020
The Keyword module provides a nifty way to lazily evaluate defaults when the desired element is not available in the Keyword list. Use this when calculating the default is expensive.#myelixirstatus #elixirlang #elixir #erlang pic.twitter.com/4XprL7EkDQ
⚗️ Elixir Tip #24 - OTP Arrays ⚗️
— Alex Koutmos (@akoutmos) October 30, 2020
You may think that arrays wouldn't be something that you would find in an FP language, but Erlang has a module for that! The :array module provides you with an immutable array for fast random access.#myelixirstatus #elixirlang #elixir #erlang pic.twitter.com/8asocMorW7
⚗️ Elixir Tip #23 - OTP Sets and Ordered Sets ⚗️
— Alex Koutmos (@akoutmos) October 28, 2020
Two of the built-in OTP data structures are sets and ordered sets. You can use these modules when you need to make sure that you only have one instance of a term in a collection.#myelixirstatus #elixirlang #elixir #erlang pic.twitter.com/kox8msjG73
⚗️ Elixir Tip #22 - Documenting your Structs ⚗️
— Alex Koutmos (@akoutmos) October 26, 2020
The documentation tooling in Elixir is seriously top notch. The following snippet shows how you can thoroughly document a Struct and pull up the docs during an IEx session.#myelixirstatus #elixirlang #elixir #erlang pic.twitter.com/5beNNa6d6E
⚗️ Elixir Tip #21 - Disabling Plugs at Run-time ⚗️
— Alex Koutmos (@akoutmos) October 23, 2020
Often times in your Phoenix apps you'll want to conditionally execute plugs based on the incoming request. Unplug is a library that makes this very simple https://t.co/C8t7V6ndBG#myelixirstatus #elixirlang #elixir #erlang pic.twitter.com/BeV3sF513d
⚗️ Elixir Tip #20 - Erlang's Built-in Queue ⚗️
— Alex Koutmos (@akoutmos) October 21, 2020
Did you know that the Erlang :queue module is one of the many data structure libraries that comes out of the box along with OTP? No need to reach for any 3rd party dependencies!#myelixirstatus #elixirlang #elixir #erlang pic.twitter.com/DBbhRYK7bN
⚗️ Elixir Tip #19 - GenServer Introspection ⚗️
— Alex Koutmos (@akoutmos) October 19, 2020
You can use the Erlang :sys module to introspect your GenServers and Agents. This can be useful when writing tests or debugging live system processes.#myelixirstatus #elixirlang #elixir #erlang pic.twitter.com/EEo1OIHywT
⚗️ Elixir Tip #18 - Catch All Struct Pattern Match ⚗️
— Alex Koutmos (@akoutmos) October 16, 2020
Did you know you can pattern match on any struct using %_{}? But be careful since this will throw a "no function clause matching" error if a regular map is provided.#myelixirstatus #elixirlang #elixir #erlang pic.twitter.com/LVuYf7G8RA
⚗️ Elixir Tip #17 - Binary Pattern Matching ⚗️
— Alex Koutmos (@akoutmos) October 14, 2020
Working with strings in Elixir is truly something special! You can pattern match using the <> and <<>> operators for easy data extraction and for function head matching.#myelixirstatus #elixirlang #elixir #erlang pic.twitter.com/svKWtpbYcN
⚗️ Elixir Tip #16 - Custom Guards ⚗️
— Alex Koutmos (@akoutmos) October 12, 2020
You can create your own guards for use in function heads using defguard/1. You can also write guards that are private to the module they are in using defguardp/1.#myelixirstatus #elixirlang #elixir #erlang pic.twitter.com/mS0pRsRldx
⚗️ Elixir Tip #15 - URI Parsing ⚗️
— Alex Koutmos (@akoutmos) October 9, 2020
The URI module in Elixir provides some convenient functions for dealing with and parsing string URIs into URI structs. Check it out the next time you need to dissect a URI https://t.co/diRRoxXEE3#myelixirstatus #elixirlang #elixir #erlang pic.twitter.com/Q1eNTIlLcr
⚗️ Elixir Tip #14 - Simple Elixir HTTP Stress Tester ⚗️
— Alex Koutmos (@akoutmos) October 7, 2020
Given how easy it is to write concurrent applications in Elixir, it should be of no surprise that you can write a pretty effective HTTP stress tester in less than 30 LoC!#myelixirstatus #elixirlang #elixir #erlang pic.twitter.com/mO8BXr9XL7
⚗️ Elixir Tip #13 - Enum Sort and Sort By ⚗️
— Alex Koutmos (@akoutmos) October 5, 2020
The Enum module provides some convenient ways to sort you data. You can use the simple sort/2 function or the sort_by/3 function if you require more control.#myelixirstatus #elixirlang #elixir #erlang pic.twitter.com/b4ypBHoD02
⚗️ Elixir Tip #12 - Inspect Raw Struct ⚗️
— Alex Koutmos (@akoutmos) October 2, 2020
Sometimes you may not want to invoke a struct's inspect protocol and would rather see the raw map representation of the struct. There is an inspect option for that: https://t.co/8FV7smyGli#myelixirstatus #elixirlang #elixir #erlang pic.twitter.com/70WelfhTvE
⚗️ Elixir Tip #11 - Ping a Host in Elixir Via a Port ⚗️
— Alex Koutmos (@akoutmos) September 30, 2020
You can easily interact with the STDOUT and STDERR output from a port by matching on the port identifier and the :data tuple. Check out how to call ping and extract the results!#myelixirstatus #elixirlang #elixir #erlang pic.twitter.com/pWGpJBLt8P
⚗️ Elixir Tip #10 - Programmatically Fetch Docs ⚗️
— Alex Koutmos (@akoutmos) September 28, 2020
The Code module has a function fetch_docs/1 that can be used to extract the module+function docs from a module. I make use of this in the Doctor library to calculate doc coverage.#myelixirstatus #elixirlang #elixir #erlang pic.twitter.com/PqZFRG26u9
⚗️ Elixir Tip #9 - Determine Process PID Via Name ⚗️
— Alex Koutmos (@akoutmos) September 25, 2020
It is often necessary to lookup the PID of a named process for introspection purposes. You can leverage Process.whereis/1 for this exact purpose!#myelixirstatus #elixirlang #elixir #erlang pic.twitter.com/BpjBZuKkfv
⚗️ Elixir Tip #8 - Write Shell Scripts in Elixir ⚗️
— Alex Koutmos (@akoutmos) September 23, 2020
Did you know that you can write shell scripts using Elixir? All you have to do is give your script.exs file execute permissions and you are good to go! Checkout this dir size script!#myelixirstatus #elixirlang #elixir #erlang pic.twitter.com/ZxBJ53jSDS
⚗️ Elixir Tip #7 - String Distance Functions ⚗️
— Alex Koutmos (@akoutmos) September 21, 2020
The Elixir String module provides 2 functions for performing string comparisons calculations. jaro_distance/2 and bag_distance/2 can be used to easily compute string similarity.#myelixirstatus #elixirlang #elixir #erlang pic.twitter.com/3eOYdqTTOa
⚗️ Elixir Tip #6 - Native Erlang Term Serialization ⚗️
— Alex Koutmos (@akoutmos) September 18, 2020
While JSON may be a convenient way to serialize/deserialize data, you can also use the built-in Erlang tools!#myelixirstatus #elixirlang #elixir #erlang pic.twitter.com/W7Ne0HHRDU
⚗️ Elixir Tip #5 - Simple GenServer Cron Job ⚗️
— Alex Koutmos (@akoutmos) September 16, 2020
GenServers provide endless amounts of utility. One of the things you can do with your GenServer is have it run a periodic or cron job.#myelixirstatus #elixirlang #elixir #erlang pic.twitter.com/SqCUUiYEmp
⚗️ Elixir Tip #4 - Fixed ExUnit Random Seed ⚗️
— Alex Koutmos (@akoutmos) September 14, 2020
ExUnit by default executes your tests in random order. If you find that your tests sometime fail due to order of execution, try setting the seed value.#myelixirstatus #elixirlang #elixir #erlang pic.twitter.com/1B8dqFwIIm
⚗️ Elixir Tip #3 - Using External Programs ⚗️
— Alex Koutmos (@akoutmos) September 11, 2020
A simple and safe way to interact with programs external to the BEAM is via a Port https://t.co/se7yQuriiG#myelixirstatus #elixirlang #elixir #erlang pic.twitter.com/WcEllNJnl4
⚗️ Elixir Tip #2 - Find Resource Hogs ⚗️
— Alex Koutmos (@akoutmos) September 9, 2020
Introspect your BEAM instance and find which processes are hogging your resources. Useful for finding memory leaks or processes stuck in computation loops.#myelixirstatus #elixirlang #elixir #erlang pic.twitter.com/tfY4vgE4dI
comments powered by Disqus⚗️ Elixir Tip #1 - Regex Named Capture ⚗️
— Alex Koutmos (@akoutmos) September 7, 2020
Pick out the portions of a string that you want using RegEx and output all of the pieces to a Map https://t.co/8geh7nbrow#myelixirstatus #elixirlang #elixir #erlang pic.twitter.com/wVfImGJ1Ka