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

dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
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
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
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
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
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 130579 1222
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
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
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44167 214
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