标签: elixir

带可选键的模式匹配映射

我有一个函数接收带有许多键的映射,其中一些是可选的.如何编写功能签名,理解地图,同时允许可选键默认为某些东西?

def handle_my_map(%{text: text, 
                    print_times: print_times, # this I want to default to 2
                    color: color # this I want to default to "Blue"
  }) do
  Enum.each(1..print_times, fn (_) -> IO.puts ["(", color, "): ", text] end)
end

Test.handle_my_map(%{text: "here", print_times: 5, color: "Red"})
# (Red): here
# (Red): here
# (Red): here
# (Red): here
# (Red): here

handle_my_map(%{text: "there"})
# => MatchError!
Run Code Online (Sandbox Code Playgroud)

我希望它是:

handle_my_map(%{text: "where", print_times: 3})
# (Blue): where
# (Blue): where
# (Blue): where
handle_my_map(%{text: "there"}) …
Run Code Online (Sandbox Code Playgroud)

elixir

15
推荐指数
1
解决办法
6841
查看次数

有没有办法在Elixir中检查函数的arity?

如果我正在编写一个传递另一个函数的函数,有没有办法检查我传递的函数的arity,或者模式匹配不同的arities?我可以使用is_function/2来检查特定的arities,但这将是一个获取数字的尴尬方式.

elixir

15
推荐指数
2
解决办法
1255
查看次数

如何撤消mix phoenix.gen.html?

mix phoenix.gen.html生成一堆文件.如何撤消这一代?还是我必须手工完成?

elixir phoenix-framework

15
推荐指数
1
解决办法
3162
查看次数

如何在elixir中的函数头中的地图键上进行模式匹配

我似乎找不到在函数头中的地图键上进行模式匹配的方法.有没有办法做到这一点?我正在尝试做的是根据某个键是否已存在于地图中而运行不同的代码(并且还想避免if/else等)

这就是我的代码

def my_func(key, %{key => _} = map), do: ...
Run Code Online (Sandbox Code Playgroud)

这给了我这个错误

**(CompileError)在映射键匹配内非法使用变量键,映射只能使用^键匹配现有变量

当然我也试过用它 ^

def my_func(key, %{^key => _} = map), do: ...
Run Code Online (Sandbox Code Playgroud)

然后给出

**(CompileError)未绑定变量^键

我在Windows 8.1机器上使用elixir 1.3.1/erlang 19.0 x64.谢谢阅读!

elixir pattern-matching

15
推荐指数
1
解决办法
8089
查看次数

Elixir和Erlang记录模式匹配

如何在Elixir中进行这种记录模式匹配?

[ #xmlText{value=Rank} ]  = xmerl_xpath:string("//SalesRank/text()", Xml),
Run Code Online (Sandbox Code Playgroud)

奖励:从Dave Thomas在Elixir的博客中重写这个例子.


更新:

发现了我的问题.你必须使用

defrecord :xmlText, Record.extract(:xmlText, from_lib: 'xmerl/include/xmerl.hrl')
Run Code Online (Sandbox Code Playgroud)

从中提取XMerL lib中记录到你的程序的规定位置.然后.value语法工作,行可以写如下:

rank = Enum.first(xmerl_xpath.string('//SalesRank/text()', xml)).value
Run Code Online (Sandbox Code Playgroud)

xml erlang elixir

14
推荐指数
1
解决办法
1611
查看次数

检查elixir中的URL是否有效

我想检查给定的URL是否有效,理想情况下,如果URL也解析了.

首先,我将如何检查字符串的有效性(即正则表达式)

其次,有没有办法让我看到网址是否真的解析为互联网上的资源?

谢谢

elixir

14
推荐指数
1
解决办法
2859
查看次数

Elixir中"|>"符号的用途是什么?

我搜索了Elixir和Phoenix文档,以及其他一些网站,比如Learn Elixir,没有运气.这是它的样子:

defp update_positions(item_ids) do
  item_ids = String.split(item_ids, ",")
                    |> Enum.map fn item_id -> String.to_integer(item_id) end

  items = Repo.all(Item |> where([item], item.id in array(^item_ids, :integer)))
  item_hash = Enum.reduce items, %{}, fn item, map -> Map.put(map, item.id, item) end

  item_ids
    |> Stream.with_index
    |> Enum.each fn {item_id, index} ->
      item = item_hash[item_id]
      Repo.update(%{item | position: index + 1})
    end
end
Run Code Online (Sandbox Code Playgroud)

起初我认为它只是一个行继续符号来保持代码可读,但Item |> where上面的行表示不然.它是列表理解还是指定输入类型的东西?

elixir

14
推荐指数
1
解决办法
798
查看次数

你如何在Elixir中获得文件的大小?

给定磁盘上文件的路径,以字节为单位检索文件大小的最惯用方法是什么?

path = "/tmp/some_file.txt"
Run Code Online (Sandbox Code Playgroud)

elixir

14
推荐指数
1
解决办法
1907
查看次数

无法混合ecto.create,角色'postgres'不存在

我尝试创建一个名为postgres的用户.我重新安装了postgres brew.我能用它来运行它

postgres -D /usr/local/var/postgres

当我跑步时mix ecto.create,我仍然得到错误:

~/code/blog_phoenix:.mix ecto.create
** (Mix) The database for BlogPhoenix.Repo couldn't be created, reason given: psql: FATAL:  role "postgres" does not exist.
~/code/blog_phoenix:.
Run Code Online (Sandbox Code Playgroud)

postgresql elixir ecto phoenix-framework

14
推荐指数
1
解决办法
2720
查看次数

Elixir Ecto:如何在变更集中设置belongs_to关联

我有点陷入如何实际设置与变更集的关联.我在我的模型中有这个代码:

defmodule MyApp.MemberApplication do
  use MyApp.Web, :model
  use Ecto.Schema
  use Arc.Ecto.Schema

  alias MyApp.Repo
  alias MyApp.MemberApplication

  schema "applications" do
    field :name, :string
    field :email, :string
    field :time_accepted, Ecto.DateTime
    field :time_declined, Ecto.DateTime
    belongs_to :accepted_by, MyApp.Admin
    belongs_to :declined_by, MyApp.Admin

    timestamps()
  end

  def set_accepted_changeset(struct, params \\ %{}) do
    struct
    |> cast(params, [:time_accepted, :accepted_by_id])
    |> cast_assoc(params, :accepted_by)
    |> set_time_accepted
  end

  defp set_time_accepted(changeset) do
    datetime = :calendar.universal_time() |> Ecto.DateTime.from_erl
    put_change(changeset, :time_accepted, datetime)
  end
end
Run Code Online (Sandbox Code Playgroud)

我想保存一个关联Admin执行某个操作(接受或拒绝member_application)和时间戳.时间戳的生成有效,但是当我尝试保存关联时,我总是得到错误

** (FunctionClauseError) no function clause matching in Ecto.Changeset.cast_assoc/3
Run Code Online (Sandbox Code Playgroud)

这就是我想要设置关联的方式: …

elixir ecto

14
推荐指数
1
解决办法
5662
查看次数