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

JDanielMartinez
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
srinivasu
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
dotdotdotPaul
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
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
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
quazar
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
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
mathew4509
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
vonH
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
LegitStack
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 Top

chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
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
itssasanka
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
aalberti333
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
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
lk-geimfari
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
fayddelight
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
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
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
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

We're in Beta

About us Mission Statement