An Ode to My Favorite Line of Elixir !
Adrian P. Dunston
Posted on July 29, 2020
Gather around, and I'll tell you the tale of my favorite line of code
A piece of advice, I thought it concise to bequeath in the form of an ode
|> IO.inspect(label: "!!ADRIAN #{__ENV__.file}:#{__ENV__.line}", pretty: true)
Pipes
We'll start with a pipe |>
a wondrous device, feeds one call's return to another
Without such a token, our code would look broken, its calls piled up in a smother.
text_of_poem = "One fish\nTwo fish\nRed fish\nBlue fish"
# Without pipes
Enum.count(String.split(text_of_poem, "\n"))
# ==> 4
First argument in, next function begins; you can see at a glance it looks cleaner
Make functions designed with piped-calls in mind; Your test suite is sure to run greener
# With pipes
text_of_poem
|> String.split("\n")
|> Enum.count()
# ==> 4
Inspect
Now let us reflect on humble inspect/2
, a device indescribably lucid
If your bug's a big stinker that's stuck in your thinker, inspect/2
makes it jump as if goose-ed
# Convert data structure to readable string
quoth = %{raven: "nevermore"}
inspect(quoth)
# ==> "%{raven: \"nevermore\"}"
inspect/2
by itself is more than a help, it converts any data to string form
And with option pretty
, the output's less bitty; Your brain has more room for a brainstorm
quoth = %{raven: ["nevermore", "nevermore", "nevermore"],
poe: "Take thy form from off my door!"}
inspect(quoth) |> IO.puts()
# ๐%{poe: "Take thy form from off my door!", raven: ["nevermore", "nevermore", "nevermore"]}
# ==> :ok
# The pretty: true option gives us more readable spacing.
inspect(quoth, **pretty: true**) |> IO.puts()
# ๐%{
# poe: "Take thy form from off my door!",
# raven: ["nevermore", "nevermore", "nevermore"]
# }
# ==> :ok
IO.inspect
IO.inspect/2
has dual effects, first it needs not a puts/1
to be candid,
The second more subtle, not run in a bubble, this function returns what it's handed
IO.inspect(quote)
# ๐%{raven: "nevermore"}
# ==> %{raven: "nevermore"}
# Inspection without interference!
text_of_poem
|> String.split("\n")
|> IO.inspect()
|> Enum.count()
# ๐["One fish", "Two fish", "Red fish", "Blue fish"]
# ==> 4
Now let's look at :label
; It's part of the stable of options inspect/2
can make use of
ID your inspections for further reflection on whatever info's elusive
text_of_poem
|> IO.inspect(label: "Before split")
|> String.split("\n")
|> IO.inspect(label: "After split")
|> Enum.count()
# ๐Before split: "One fish\nTwo fish\nRed fish\nBlue fish"
# ๐After split: ["One fish", "Two fish", "Red fish", "Blue fish"]
# ==> 4
Calling Card
Now IO.inspect\2
's not a thing to forget when your feature is primed for submission,
When you fit-and-finish, prestige will diminish with inspect lines as part of commission
Remind to remove and avoid such reprove with calling-card text you're alert to
Make it unique, add your mystique, and before you commit do a search-through
#!!ADRIAN - debug lines easier to spot in console
# Remember to grep and remove !!ADRIAN lines before you push
text_of_poem
|> IO.inspect(label: "!!ADRIAN Before split")
|> String.split("\n")
|> IO.inspect(label: "!!ADRIAN After split")
|> Enum.count()
# ๐!!ADRIAN Before split: "One fish\nTwo fish\nRed fish\nBlue fish"
# ๐!!ADRIAN After split: ["One fish", "Two fish", "Red fish", "Blue fish"]
# ==> 4
ENV file and line
Final reflection on one-line perfection confesses the fact that I'm slothful.
It feels such a bother, each label to author; is cutting and pasting so awful?
Now in walks dear ENV
, our quite astute friend; she knows both the file and line number
With this pal in hand, inspect can be canned; and attention advance unencumbered
1 text_of_poem
2 |> IO.inspect(label: "!!ADRIAN #{__ENV__.file}:#{__ENV__.line}", pretty: true)
3 |> String.split("\n")
4 |> IO.inspect(label: "!!ADRIAN #{__ENV__.file}:#{__ENV__.line}", pretty: true)
5 |> Enum.count()
# ๐!!ADRIAN example.ex:2: "One fish\nTwo fish\nRed fish\nBlue fish"
# ๐!!ADRIAN example.ex:4: ["One fish", "Two fish", "Red fish", "Blue fish"]
# ==> 4
Coda - Clipboard Extension Tools
I hope you've admired this line so inspired by the folks who have taught me before,
And with your indulgence, I've one more divulgence, a quirk that can quickly be yours
Clipboard extension is carapal prevention; it works by saving you typing
Along with your copies, it pastes pre-typed drop-ins, so coding continues like lightning!
Ditto Clipboard Extension for Windows
https://www.microsoft.com/en-us/p/ditto-clipboard/9nblggh3zbjq
Clipy Clipboard Extension for Mac
https://github.com/Clipy/Clipy
Posted on July 29, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 29, 2024