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 Switch mode

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.

Last Post!

Fl4m3Ph03n1x

Fl4m3Ph03n1x

Thank you for your reply. I know understand where my issues are and I can fix the code!

Where Next?

Trending in Questions Top

jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
saveman71
Hello ! We want new/edit form pages to POST/PUT to their own URL rather than the resources REST defaults (post /things, put /things/:id)...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement