owaisqayum

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

kokolegorille

The full pipe way :slight_smile:

a 
|> Kernel.+(b) 
|> add()
kokolegorille

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

Where Next?

Popular in Questions Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
ovidiubadita
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
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
romenigld
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
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New
dokuzbir
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
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Other popular topics Top

axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 48342 226
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
siddhant3030
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
albydarned
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
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
openscript
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
JorisKok
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

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement