zerogvt

zerogvt

Funny pipe code

Hi all, first post here (also a newbie in elixir/FP).
So, I have next pipe code which works but also looks funny:

case {status_code, body} do
      {200, _} ->
        edges = Poison.Parser.parse!(body)
        |> Map.get("data")
        |> Map.get("repository")
        |> Map.get("issues")
        |> Map.get("edges")
        if Enum.count(edges) == 0 do
          :ok
        else
          edges
          |> Enum.at(-1)
          # get last edge cursor
          |> Map.get("cursor")
          |> IO.inspect
          # recurse to get next page
          |> get(pagenum + 1)
          :ok
        end
      _ ->
        IO.inspect(status_code)
        IO.inspect("[ERROR]")
        :error
    end

2 questions here:

  1. How can I avoid using the temp var edges (i.e. use a continuous pipe)
  2. Is there some sort of tee operator that I could use to siphon out data at a specific pipe point whilst the pipe would continue on?

Thanks!

Most Liked

peerreynders

peerreynders

def f_1(data) do
  data
  |> transformation_1()
  |> transformation_2()
  |> f_2()
end

# still purely sequential - more like a (sequential) fork/join
# because everything has to be an expression.
#
def f_2(snapshot_data) do
  {transformation_3(snapshot_data),transformation_4(snapshot_data)} # return both results as a tuple
end

functional-programming-patterns-ndc-london-2014-15-638

More often than not the solution to a problem is “functions”.

pie

idi527

idi527

:wave:

  1. How can I avoid using the temp var edges (i.e. use a continuous pipe)
# ...
|> Map.get("edges")
|> case do
  [] -> :ok
  edges ->
    edges
    |> Enum.at(-1)
    # get last edge cursor
    |> Map.get("cursor")
    |> IO.inspect
    # recurse to get next page
    |> get(pagenum + 1)
    :ok
  end
# ...

But I’d probably want to refactor what you have to something like

def handle_resp(status_code, raw_body)

def handle_resp(200, raw_body) do
  raw_body
  |> Posion.decode!()
  |> extract_edges!()
  |> handle_edges()
end

defp extract_edges!(%{"data" => %{"repository" => %{"issues" => %{"edges" => edges}}}}) do
  edges
end

defp extract_edges!(invalid_body) do
  raise("invalid body: #{inspect(invalid_body)}")
end

defp handle_edges([]), do: :ok
defp handle_edges(edges) when is_list(edges) do
  # not sure what this is supposed to do
  # ...
  :ok
end

# etc
mudasobwa

mudasobwa

Creator of Cure

I’m Russian, I know it better than anybody else :sunglasses:

zerogvt

zerogvt

:sweat_smile: it’s gonna take me a while to adjust. Coming from classic OO world (and landing to elixir by accident). FP paradigm feels quite alien to me in a good way though. I feel like I’m unlocking doors in my mind. Thanks a mil!

dimitarvp

dimitarvp

I’d change that to:

get_in(~w<data repository issues edges>)

Where Next?

Popular in Questions Top

srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
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
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
hpopp
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
New

Other popular topics Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
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
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 Postg...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
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

We're in Beta

About us Mission Statement