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

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
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
yawaramin
In the Dialyzer docs ( dialyzer — OTP 29.0.2 (dialyzer 6.0.1) ), there is a way to turn off a specific warning for a function: -dialyzer...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

Other popular topics Top

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
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
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
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39297 209
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 47930 226
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

We're in Beta

About us Mission Statement