owaisqayum
Calling a function inside other function
Hi,
I am having three functions and the data inputs to only one of them. Lets say i have a function A having two parameters a and b. The sum operation is going inside.
def sum(a, b), do: a + b
Then in the second function, I want to add ten to the result of sum(a, b). So i can simply do like
def sum(a, b) do
a + b
|> add()
end
and then define add.
def add(sum) do
sum + 10
end
Now, my question is that how can I use this sum function and add functions separately. let’s say in a third function I want to take the product of sum and add function. Right now I can’t use them as the sum function is always calling the add function.
def product() do
# code here
add * sum
end
Most Liked
kokolegorille
The full pipe way ![]()
a
|> Kernel.+(b)
|> add()
2
kokolegorille
I was writing one last night…
Maybe it can help You.
defmodule EventStore.Core.ListenersProvider do
use GenServer
require Logger
@name __MODULE__
def start_link(_) do
GenServer.start_link(@name, nil, name: @name)
end
def register(pid, filter_fun \\ fn _ -> true end)
def register(pid, filter_fun) when is_function(filter_fun) do
GenServer.call(@name, {:register, pid, filter_fun})
end
def unregister(pid) do
GenServer.cast(@name, {:unregister, pid})
end
def get_listeners do
@name
|> :ets.match_object({:"$1", :"$2", :_})
|> Enum.map(fn {pid, filter_fun, _ref} -> {pid, filter_fun} end)
end
def stop, do: GenServer.cast(@name, :stop)
@impl GenServer
def init(_) do
Logger.debug(fn -> "#{@name} is starting}" end)
#
Process.flag :trap_exit, true
:ets.new(@name, [:set, :protected, :named_table])
{:ok, nil}
end
@impl GenServer
def handle_call({:register, pid, filter_fun}, _from, _state) do
ref = Process.monitor(pid)
:ets.insert_new(@name, {pid, filter_fun, ref})
{:reply, :ok, nil}
end
@impl GenServer
def handle_cast({:unregister, pid}, _state) do
case :ets.match(@name, {pid, :_, :"$1"}) do
[[ref]] when is_reference(ref) -> Process.demonitor(ref)
_ -> nil
end
:ets.delete(@name, pid)
{:noreply, nil}
end
@impl GenServer
def handle_cast(:stop, _state), do: {:stop, :normal, nil}
@impl GenServer
def handle_info({:DOWN, _ref, :process, pid, _status}, _state) do
:ets.delete(@name, pid)
{:noreply, nil}
end
@impl GenServer
def terminate(reason, _state) do
Logger.debug(fn -> "#{@name} is stopping : #{inspect reason}" end)
#
:ets.delete(@name)
:ok
end
end
1
Popular in Questions
Hi! May someone helps me, please!
I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
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
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
i’m a new one to elixir
which editor can i use
vs code? or atom?
Thanks! :smiley:
New
I have a list say
x = ["23gh", "56kh", "97mh"]
I would like to pass each element to Val in each iteration.
Say, in iteration 1 -------...
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’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
Other popular topics
Phoenix 1.4.0 released
Phoenix 1.4 is out! This release ships with exciting new features, most notably
with HTTP2 support, improved deve...
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
Hi all,
Trying to get some more clarity over utc_datetime and naive_datetime for Ecto:
https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this:
...
New
Hi folks,
Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
New
What is most correct way to open, read and parse JSON file with poison?
For example if we have example.json file in root of some projec...
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
Hey all,
I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
I will often find my self writing things similar to:
case some_value do
nil -> something()
"" -> something()
_ -> someth...
New
I am trying to run a deploy with docker and I successfully runned with this command:
docker build -t romenigld/blog-prod .
but when I t...
New








