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
I’m Russian, I know it better than anybody else ![]()
3
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
dimitarvp
I’d change that to:
get_in(~w<data repository issues edges>)
2
Popular in Questions
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
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
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
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
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
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
I would like to know what is the best IDE for elixir development?
New
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
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
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
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
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
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
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
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
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
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
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
by Lance Halvorsen
Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
New
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







