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

:waving_hand:

  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:

Last Post!

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!

Where Next?

Popular in Questions Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
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

Other popular topics Top

Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
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
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
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement