【问题标题】:Wrap a function and wrap it to a closure by passing modulename into a function通过将模块名传递给函数来包装函数并将其包装到闭包中
【发布时间】:2020-06-21 04:20:59
【问题描述】:

您好,我想让我的函数更加模块化,并最终将其包装成闭包。

我目前有这个功能:

  def get_all_perfume_by_name_con_sex(query, name, concentration, gender) do 
    from p in query,
      join: j in Fumigate.Fragrance.PerfumeCompanyJoin, where: p.id == j.perfume_id,
      where: [perfume_name: ^name, 
              concentration: ^concentration,
              gender: ^gender
      ],
      select: j.company_id 
  end

我很好奇如何将模块作为参数传递给函数,更具体地说是 Fumigate.Fragrance.PerfumeCompanyJoin 行。

这可能吗?

  def get_all_perfume_by_name_con_sex_modulename(query, name, concentration, gender, modulename) do 
    from p in query,
      join: j in modulename, where: p.id == j.perfume_id,
      where: [perfume_name: ^name, 
              concentration: ^concentration,
              gender: ^gender
      ],
      select: j.company_id 
  end

这样的事情可能吗?

我想最终将新函数包装到一个闭包中。

谢谢!

【问题讨论】:

    标签: elixir


    【解决方案1】:

    是的,它的工作方式与将任何其他值注入查询中的方式相同——使用^

    在查询中,执行以下操作:

    join: j in ^modulename, where: p.id == j.perfume_id
    

    并将Fumigate.Fragrance.PerfumeCompanyJoin 传递给函数调用:

    get_all_perfume_by_name_con_sex_modulename(_, _, _, _, Fumigate.Fragrance.PerfumeCompanyJoin)
    

    【讨论】:

      猜你喜欢
      • 2018-08-05
      • 1970-01-01
      • 2015-07-22
      • 2017-02-05
      • 1970-01-01
      • 2021-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多