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:
- How can I avoid using the temp var edges (i.e. use a continuous pipe)
- 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
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
More often than not the solution to a problem is “functions”.
5
idi527
![]()
- 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
4
mudasobwa
Creator of Cure
Last Post!
zerogvt
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!
3
Popular in Questions
Hello, how can I check the Phoenix version ?
Thanks !
New
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
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
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
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
i’m a new one to elixir
which editor can i use
vs code? or atom?
Thanks! :smiley:
New
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
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum.
...
New
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
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
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
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
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
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









