Fl4m3Ph03n1x

Fl4m3Ph03n1x

Help understanding Question.ex from Designing Elixir System with OTP book

Backgrond

I am following the book Designing Elixir System with OTP book and I hit the (Fun)ctions part of the “Do Fun Things with Big Loud Wildbeests” layer.

To test my understanding I am adding dialyzer to everything while trying to make it compile.

Problem

However, I have hit a roadblock. I can’t understand the Questions.ex file. In the book they talk about lists and generators (which are functions) but I don’t understand what this lists and functions are supposed to have inside or return.

Here is the file in question (get it? The file in question? I’ll show myself out :smiley: )

defmodule Mastery.Core.Question do
  @moduledoc """
  Questions consist of the text the user was asked, the used to create them and
  the substitutions used to build the template.
  """

  alias Mastery.Core.Template

  @type t() :: %__MODULE__{
    asked: String.t,
    template: Template.t,
    substitutions: %{substitution: any}
  }

  @enforce_keys [:asked, :substitutions, :template]
  defstruct ~w[asked substitutions template]a

  @spec new(Template.t) :: __MODULE__.t
  def new(%Template{} = template) do
    template.generators
    |> Enum.map(&build_substitution/1)
    |> evaluate(template)
  end

  # @spec evaluate(?????, Template.t) :: __MODULE__.t
  defp evaluate(substitutions, template), do:
    %__MODULE__{
      asked: compile(template, substitutions),
      substitutions: substitutions,
      template: template
    }

  # @spec compile(Template.t, ??????) :: any????
  defp compile(template, substitutions) do
    template.compiled
    |> Code.eval_quoted(assigns: substitutions)
    |> elem(0)
  end

  # @spec build_substitution({atom???, [any???] | function()}) :: {atom, any???}
  defp build_substitution({name, choices_or_gen}), do:
    {name, choose(choices_or_gen)}

    # ???? What is this supposed to return? any? a String.t ?
  defp choose(choices) when is_list(choices), do: Enum.random(choices)
  defp choose(generator) when is_function(generator), do: generator.()

end

Questions

As you can see, I am having trouble identifying what my functions return, namely the last function, choose/1.

I have re-read the book and I couldn’t find anything regarding the types these functions should manage. I am focusing and guessing based on the official docs of EEx.

Can anyone help me figure this out?

Marked As Solved

JEG2

JEG2

Author of Designing Elixir Systems with OTP

The returned substitutions are few into your template, so it’s really what your template expects. The examples and tests in the book use numbers to feed math problems, but you could use strings to construct grammar problems or whatever. For this reason, I feel any() is the proper type.

Where Next?

Popular in Questions Top

lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
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
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
Kagamiiiii
Student & New to elixir. Nice language. I want to convert a english character, e.g. “a”, which is stored in a variable, to it’s asci...
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
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
wernerlaude
In AR this is so simple @articles = current_user.articles How to do in Ecto? def index(conn, _params) do current_user = conn.assig...
New

Other popular topics Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
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
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
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
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

We're in Beta

About us Mission Statement