Pass function as variable in pipe

Hi, is it possible to insert a variable function in the pipe operartor?. Something like this:

def list_long_trade_fe(schema, function) do    
    long_trades_fe(schema)
      |> ^function()
end

You need to invoke the anonymous function

def list_long_trade_fe(schema, function) do 
  long_trades_fe(schema) 
  |> function.()
end
5 Likes

@gregvaughn. It is working, however the function needs 1 parameter, and this parameter will be inserted during the pipe. But elixir is asking for the parameter during compilation.

undefined function retest_long_select_fields/0

What would be the solution?

The snippet you have shown does not reference that name, perhaps its a problem on the callsite?

3 Likes

The complete functions are like these:

In this case, I plan to execute this:

list_long_trade_fe(RetestLong, :retest_long_category, retest_long_select_fields)

 def long_trades_fe(schema, category_atom) do  
    schema      
      |> join(:inner, [lt], er in assoc(lt, :exit_reason))
      |> join(:inner, [lt], ltc in assoc(lt, ^category_atom))
      |> join(:inner, [lt], f in assoc(lt, :fact))
      |> join(:inner, [_, _, _, f], s in assoc(f, :stock))      
  end

  def retest_long_select_fields(query) do
    query
      |> select([lt, er, ltc, f, s], %ShowRetestLongFE{
        atr: f.atr,
        best_entry_price: lt.best_entry_price,
        best_exit_price: lt.best_exit_price
        })
  end

  def list_long_trade_fe(schema, schema_category, fields_function) do    
    long_trades_fe(schema, schema_category)
      |> fields_function.()
end
list_long_trade_fe(RetestLong, :retest_long_category, &retest_long_select_fields/1)
4 Likes

Thanks @gregvaughn @NobbZ @peerreynders !

1 Like