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?

Showing Posts 1 to 2

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.

Fl4m3Ph03n1x

Fl4m3Ph03n1x OP

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

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
water
I’m aware of 2 such projects, are there others? git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
itzmidinesh
Hi all, I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
peck
Hi everyone. My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
leoschweizer
Hi, we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
csmr
Hi all, I’m new here and would like to hear peoples views on my Elixir/Phoenix based game-engine! :smiley: Mutonex gamedesign was finish...
New
perzanko
Do we have maintainer of absinthe-graphql/dataloader here? We have a PR that we’d like to merge to the library. Relatively small additive...
New

Other Trending Topics Top

dominicletz
Hey there, I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first. ...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
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
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews