From ebd901fe9dceeb9668a8497b28c166f702bf864c Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Wed, 6 Jul 2016 20:04:06 -0500 Subject: [PATCH] Add Dynamically Generating Atoms as an elixir til --- README.md | 3 ++- elixir/dynamically-generating-atoms.md | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 elixir/dynamically-generating-atoms.md diff --git a/README.md b/README.md index 57334ba..4c81fd4 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ variety of languages and technologies. These are things that don't really warrant a full blog post. These are mostly things I learn by pairing with smart people at [Hashrocket](http://hashrocket.com/). -_446 TILs and counting..._ +_447 TILs and counting..._ --- @@ -84,6 +84,7 @@ _446 TILs and counting..._ - [Append To A Keyword List](elixir/append-to-a-keyword-list.md) - [Assert An Exception Is Raised](elixir/assert-an-exception-is-raised.md) - [Create A Date With The Date Sigil](elixir/create-a-date-with-the-date-sigil.md) +- [Dynamically Generating Atoms](elixir/dynamically-generating-atoms.md) - [Execute Raw SQL In An Ecto Migration](elixir/execute-raw-sql-in-an-ecto-migration.md) - [Expose Internal Representation](elixir/expose-internal-representation.md) - [List Functions For A Module](elixir/list-functions-for-a-module.md) diff --git a/elixir/dynamically-generating-atoms.md b/elixir/dynamically-generating-atoms.md new file mode 100644 index 0000000..3c26cdf --- /dev/null +++ b/elixir/dynamically-generating-atoms.md @@ -0,0 +1,15 @@ +# Dynamically Generating Atoms + +> Atoms are constants where their name is their own value. + +The use of atoms like `:ok` and `:error` show up all over the place in +Elixir. These are atoms that tend to be statically defined. Atoms can also +be dynamically defined using string interpolation. + +For example, I can generate a handful of atoms by mapping over a range of +integers. + +```elixir +> Enum.map(1..5, &(:"some_atom_#{&1}")) +[:some_atom_1, :some_atom_2, :some_atom_3, :some_atom_4, :some_atom_5] +```