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
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible.
total = 10
while total != 0
...
New
I will often find my self writing things similar to:
case some_value do
nil -> something()
"" -> something()
_ -> somethi...
New
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
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
Hello everybody,
usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
In templates/appointment/index.html.eex:
<%= for appointment <- @appointments do %>
<tr>
<td><%= appoi...
New
Using vs code and installed ElixirLS: support and debugger.
And I got an error popped up on start up says
Failed to run ‘elixir’ comma...
New
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors:
[WARN] - (starship::utils): Executing command ...
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
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
Other popular topics
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch.
This project took far...
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
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
Hi all,
I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage.
I’m trying to use Postgres...
New
What learn first? Rust or Elixir
Hi Elixir community!
I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
I have VueJS GUIs with the project generated using Webpack.
I have Elixir modules that will need to be used by the VueJS GUIs.
I forese...
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
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
I would like to know what is the best IDE for elixir development?
New
Hi everyone!
I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
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
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









