feviskus

feviskus

Improving my code using idiomatic elixir and pipelines

Hello there,
I am currently working on my first elixir-project which has a bit more business-logic and is not only CRUD.
I want to apply OTP/concurrency in a useful way, and a book-search aggregator seems to be a good use-case.
Currently I have a prototype implementation for a book-store which I scrape to extract the results (btw, Floki is surprisingly fast! At least it feels faster than python).
The pipe looks like this:

 @behaviour Book4Less.Searches.SearchBookBehaviour

  def search_books(query = %Query{} \\ %Query{title: "Elixir"}) do
    query
    |> build_url() # returns a string
    |> execute_request() # returns a HTTPoison.request
    |> validate_response!() # returns a html-string
    |> extract_books() # returns a list of %Book{}
  end

Questions

  • These functions are temporally-coupled, e.g. build_url/1 and execute_request/1 - because I need to set the url before I send a HTTP-request…
    How to express this coupling best? Just making execute_request/1 call build_url/1?

  • So this pipeline deals with string, HTTP, HTML and the domain-object Book - all levels of abstractions & different topics. Would u unify the return values to e.g. a %Search{} struct?

Most Liked

kip

kip

ex_cldr Core Team

I think this would be nicely expressed in a with clause since you are subject to errors at each stage of the pipeline and therefore would want to exit the pipeline if an error is detected. So as an example:

def search_books(query = %Query{} \\ %Query{title: "Elixir"}) do
  with {:ok, url} <- build_url(query),
       {:ok, response} <- execute_request(url),
       {:ok, response} <- validate_response(response) do
    extract_books(response)
  end
end

Noting that if at any stage the patterns do not match, the with clause will exit with the non-matching return.

With this approach I would likely delegate validate_response/1 to be called within execute_request/1 since with is best used to describe the happy path. So this time with explicit error management:

def search_books(query = %Query{} \\ %Query{title: "Elixir"}) do
  with {:ok, url} <- build_url(query),
       {:ok, response} <- execute_request(url) do
    extract_books(response)
  else
    {:error, reason} -> raise "whoops, we got error #{inspect reason}"
    other -> raise "unexpected error return #{inspect other}"
  end
end

Then for execute_request/1:

def execute_request(url) do
  with {:ok, response} -> http_call(url),
       {:ok, valid_response} -> validate_response(response) do
    {:ok, valid_response}
  end
end
kip

kip

ex_cldr Core Team

At some point your expressive pipeline will have to deal with errors since you have functions with side affects (like external requests) so I’m not sure its the right measure of success.

However perhaps the OK library by @Crowdhailer would reflect your tastes more?

peerreynders

peerreynders

FYI: exceptional

Where Next?

Popular in Questions Top

9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
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
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
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
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
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
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

Other popular topics Top

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
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
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
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
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42158 114
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
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
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 Postgres...
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
New

We're in Beta

About us Mission Statement