包装一个函数并通过将模块名称传递给函数将其包装到闭包中

Wrap a function and wrap it to a closure by passing modulename into a function

嗨,我想让我的函数更加模块化,并最终将它包装成一个闭包。

我目前有这个功能:

  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

这样的事情可能吗?

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

谢谢!

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

在查询中,这样做:

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

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

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