chemist

chemist

How to improve this snippet (piping result of a function into another function) with Mogrify

Hi, hope for any wisdom on how to improve this code snippet or if there are bits of knowledge I appear to be lacking

Basically I want to create a map called data containing a range of persons with names ranging from Person 0-100 with a dynamically generated image generated by Mogrify.

But I see my code is a little unwieldy and will love opinions on how to work on it

Many thanks!

def generate_data do
  for x <- 0..100 do
    {:ok, image_data} = Map.fetch(render_image(x),:buffer)
    data(
      name: "Person #{x}",
      image_data: image_data
      )
  end
end

def render_image(x) do
  import Mogrify
  %Mogrify.Image{}
  |> custom("size", "300x300")
  |> custom("background", "#000000")
  |> custom("gravity", "center")
  |> custom("fill", "white")
  |> custom("pango", ~s(<span foreground="yellow">Good day to you, </span>) <> "#{x}")
  |> custom("stdout", "png:-")
  |> create(buffer: true)
end

Marked As Solved

al2o3cr

al2o3cr

render_image is going to return a Mogrify.Image struct - it will always have a buffer key, so you can shorten that line to image_data = render_image(x).buffer

Also Liked

Adzz

Adzz

Well you are doing great so far.

Piping is just syntax sugar, using it is not refactoring. What you have is perfectly readable so I think it’s fine.

al2o3cr

al2o3cr

Not always error-handling exactly - Map.fetch is really useful when it’s important to your code whether the key is present at all and the value could potentially be nil.

For instance, if you need to distinguish:

without_baz = %{foo: "bar"}
# from
nil_baz = %{foo: "bar", baz: nil}

then you’ll get different results with different functions in Map:

# square-brackets return nil if the key isn't present
nil_baz[:baz] == without_baz[:baz]

# Map.get can return a default
Map.get(without_baz, :baz, "default value") # => "default value"
# vs
Map.get(nil_baz, :baz, "default value") # => nil

# But if there's no "safe" default that won't appear in the source, Map.fetch is better:
Map.fetch(without_baz, :baz) # => :error
# vs
Map.fetch(nil_baz, :baz) # => {:ok, nil}

Where Next?

Popular in Questions Top

belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

Other popular topics Top

josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
AstonJ
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New

We're in Beta

About us Mission Statement