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
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
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
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible.
total = 10
while total != 0
...
New
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
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
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
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
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...
New
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
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. 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
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
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









