RSSAmplifier

My.Thoughts v1 · Jun 6, 2019

Elixir Tips

0
Sign in to vote or save

My.Thoughts v1

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 ⚗️

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

— Alex Koutmos (@akoutmos) November 16, 2020

Enjoying the blog post so far? Follow me on Twitter for more content Follow @akoutmos


Sign up for the mailing list and get notified via email when new blog posts come out.

⚗️ Elixir Tip #30 - ExUnit assert_receive ⚗️

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

— Alex Koutmos (@akoutmos) November 13, 2020

⚗️ Elixir Tip #29 - Running mix commands in an umbrella app ⚗️

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

— Alex Koutmos (@akoutmos) November 11, 2020

⚗️ Elixir Tip #28 - Simple Struct inspect protocol ⚗️

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

— Alex Koutmos (@akoutmos) November 9, 2020

⚗️ Elixir Tip #27 - Concurrent port scanner ⚗️

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

— Alex Koutmos (@akoutmos) November 6, 2020

⚗️ Elixir Tip #26 - CLI argument parsing ⚗️

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

— Alex Koutmos (@akoutmos) November 4, 2020

⚗️ Elixir Tip #25 - Lazy Keyword get defaults ⚗️

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

— Alex Koutmos (@akoutmos) November 2, 2020

⚗️ Elixir Tip #24 - OTP Arrays ⚗️

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

— Alex Koutmos (@akoutmos) October 30, 2020

⚗️ Elixir Tip #23 - OTP Sets and Ordered Sets ⚗️

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

— Alex Koutmos (@akoutmos) October 28, 2020

⚗️ Elixir Tip #22 - Documenting your Structs ⚗️

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

— Alex Koutmos (@akoutmos) October 26, 2020

⚗️ Elixir Tip #21 - Disabling Plugs at Run-time ⚗️

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

— Alex Koutmos (@akoutmos) October 23, 2020

Enjoying the blog post so far? Follow me on Twitter for more content Follow @akoutmos


Sign up for the mailing list and get notified via email when new blog posts come out.

⚗️ Elixir Tip #20 - Erlang's Built-in Queue ⚗️

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

— Alex Koutmos (@akoutmos) October 21, 2020

⚗️ Elixir Tip #19 - GenServer Introspection ⚗️

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

— Alex Koutmos (@akoutmos) October 19, 2020

⚗️ Elixir Tip #18 - Catch All Struct Pattern Match ⚗️

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

— Alex Koutmos (@akoutmos) October 16, 2020

⚗️ Elixir Tip #17 - Binary Pattern Matching ⚗️

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

— Alex Koutmos (@akoutmos) October 14, 2020

⚗️ Elixir Tip #16 - Custom Guards ⚗️

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

— Alex Koutmos (@akoutmos) October 12, 2020

⚗️ Elixir Tip #15 - URI Parsing ⚗️

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

— Alex Koutmos (@akoutmos) October 9, 2020

⚗️ Elixir Tip #14 - Simple Elixir HTTP Stress Tester ⚗️

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

— Alex Koutmos (@akoutmos) October 7, 2020

⚗️ Elixir Tip #13 - Enum Sort and Sort By ⚗️

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

— Alex Koutmos (@akoutmos) October 5, 2020

⚗️ Elixir Tip #12 - Inspect Raw Struct ⚗️

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

— Alex Koutmos (@akoutmos) October 2, 2020

⚗️ Elixir Tip #11 - Ping a Host in Elixir Via a Port ⚗️

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

— Alex Koutmos (@akoutmos) September 30, 2020

Enjoying the blog post so far? Follow me on Twitter for more content Follow @akoutmos


Sign up for the mailing list and get notified via email when new blog posts come out.

⚗️ Elixir Tip #10 - Programmatically Fetch Docs ⚗️

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

— Alex Koutmos (@akoutmos) September 28, 2020

⚗️ Elixir Tip #9 - Determine Process PID Via Name ⚗️

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

— Alex Koutmos (@akoutmos) September 25, 2020

⚗️ Elixir Tip #8 - Write Shell Scripts in Elixir ⚗️

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

— Alex Koutmos (@akoutmos) September 23, 2020

⚗️ Elixir Tip #7 - String Distance Functions ⚗️

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

— Alex Koutmos (@akoutmos) September 21, 2020

⚗️ Elixir Tip #6 - Native Erlang Term Serialization ⚗️

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

— Alex Koutmos (@akoutmos) September 18, 2020

⚗️ Elixir Tip #5 - Simple GenServer Cron Job ⚗️

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

— Alex Koutmos (@akoutmos) September 16, 2020

⚗️ Elixir Tip #4 - Fixed ExUnit Random Seed ⚗️

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

— Alex Koutmos (@akoutmos) September 14, 2020

⚗️ Elixir Tip #3 - Using External Programs ⚗️

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

— Alex Koutmos (@akoutmos) September 11, 2020

⚗️ Elixir Tip #2 - Find Resource Hogs ⚗️

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

— Alex Koutmos (@akoutmos) September 9, 2020

⚗️ Elixir Tip #1 - Regex Named Capture ⚗️

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

— Alex Koutmos (@akoutmos) September 7, 2020

Enjoying the blog post so far? Follow me on Twitter for more content Follow @akoutmos


Sign up for the mailing list and get notified via email when new blog posts come out.

comments powered by

Read the original on akoutmos.com

Comments

Nothing yet. Say the first thing.

    Sign in to join the conversation.