Fl4m3Ph03n1x

Fl4m3Ph03n1x

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.

Last Post!

Fl4m3Ph03n1x

Fl4m3Ph03n1x OP

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

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
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
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
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
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews