在进行一些练习时,我提出了一个(可能)不是惯用的解决方案,它可以完美地编译但是没有按预期运行?
def span(xs, min, max), do: _span(xs,min,max)
defp _span([], _, _), do: []
# The following guard does not match
defp _span([head | tail],min,max) when min <= head <= max, do: [head | _span(tail,min,max)]
defp _span([head | tail],min,max), do: _span(tail,min,max)
Run Code Online (Sandbox Code Playgroud)
问题是为什么编译但不起作用?
顺便说一句.我知道'更多'类似于灵药的解决方案可能就是这个(至少它可以完成这项工作):
defp _span([head | tail],min,max) when head in min..max, do: [head | _span(tail,min,max)]
Run Code Online (Sandbox Code Playgroud)
谢谢.
通过"归纳图",我的意思是作为一个纯粹的功能数据结构,例如本文所述:web.engr.oregonstate.edu/~erwig/papers/InductiveGraphs_JFP01.pdf
更具体地说,我只对以这种方式实现的DAG感兴趣,尽管我认为一般方法是类似的.
我正在学习Elixir,我很好奇为什么会发生以下情况:
iex(4)> case {:one, :two} do
...(4)> {:four, :five} ->
...(4)> "This won't match"
...(4)> {:one, x} ->
...(4)> "This will match and bind `x` to `:two`"
...(4)> _ ->
...(4)> "This will match any value"
...(4)> end
"This will match and bind `x` to `:two`"
Run Code Online (Sandbox Code Playgroud)
因此,如果在"模式匹配"示例中,为什么空变量会x自动绑定到原子:two并提供正匹配?这是第一次运行时x不相等.:twocase
我只是没有理解究竟发生了什么.
谢谢.
我是新的Elixir,非常享受它.当我尝试使用同名函数时,我碰壁了.这是一个例子
defmodule ChangeName do
def convert(:captilize, name), do: String.capitalize(name)
def convert(:lower, name), do: String.downcase(name)
end
Run Code Online (Sandbox Code Playgroud)
我正在使用iex和ChangeName.convert.captilize的基本调用但是如何运行这些函数?
谢谢
我有一个嵌套的模型,并希望做一些基本的管道.我的最终目标是删除任何嵌套映射,其中id == ""包含以下参数:
params = %{"name" => "OuterModelName", "InnerModel" => %{"0" => %{"id" => "2"}, "1" => %{"id" => "3"}, "2" => %{"id" => ""}}}
要删除id ==""以下管道工程:
blanksRemoved =
params
|> Map.update! "InnerModel", fn(innerMap) ->
Enum.filter(innerMap,fn{k,v} -> byte_size(v["id"]) !=0 end) end
Run Code Online (Sandbox Code Playgroud)
blanksRemoved现在是:
%{"name" => "OuterModelName", "InnerModel" => [{"0", %{"id" => "2"}}, {"1", %{"id" => "3"}}]}
注意innerModel变成了一个数组,所以我需要将该数组转换回结构.
asStruct =
blanksRemoved |> Map.update! "InnerModel", fn(innerMap) ->
Enum.into(innerMap,%{}) end
Run Code Online (Sandbox Code Playgroud)
这符合预期,我得到:
%{"name" => "OuterModelName", "InnerModel" => %{"0" => %{"id" => …
Sup,首先,我想让你知道我对Phoenix Framework和Elixir很新鲜.我正在通过教程工作,但我遇到了奇怪的问题.我配置了连接,一切正常.但是迁移文件似乎没有被执行.我写了以下文件:
defmodule Hangman.Repo.Migrations.CreateWords do
use Ecto.Migration
def change do
create table(:words) do
add :content, :string
timestamps
end
end
end
Run Code Online (Sandbox Code Playgroud)
这只是一个包含一个字段的简单表.但是Table没有在数据库中创建,但Ecto认为一切正常:
18:22:28.669 [info] Already up
Run Code Online (Sandbox Code Playgroud) 关于凤凰城框架:我只是想知道写作之间有什么区别
defmodule MyApp.User do
# some code
end
Run Code Online (Sandbox Code Playgroud)
只是
defmodule User do
# some code
end
Run Code Online (Sandbox Code Playgroud)
稍后,只需编写User.function比MyApp.User.function更容易
每当我发布到/ api/subastas时,我都会收到此错误
Phoenix.ActionClauseError at POST /api/subastas/ bad request to IascSubastas.SubastaController.create, no matching action clause to process request
Run Code Online (Sandbox Code Playgroud)
如果我运行mix phoenix.routes,我看到:create正确路由到POST/api/subastas
这是router.ex
defmodule IascSubastas.Router do
use IascSubastas.Web, :router
pipeline :browser do
plug :accepts, ["html"]
plug :fetch_session
plug :fetch_flash
plug :protect_from_forgery
plug :put_secure_browser_headers
end
pipeline :api do
plug :accepts, ["json"]
end
scope "/", IascSubastas do
pipe_through :browser # Use the default browser stack
get "/", PageController, :index
end
# Other scopes may use custom stacks.
scope "/api", IascSubastas do
pipe_through :api
resources "/subastas", …Run Code Online (Sandbox Code Playgroud) 我发现渲染模板使用link函数和生成的路径辅助函数很慢.
例如,我跑 mix phoenix.gen.html Author authors name:string
里面index.html.eex有
<%= for author <- @authors do %>
# ...
<%= link "Show", to: author_path(@conn, :show, author) %>
<% end %>
1,500位作者记录的响应时间超过2秒.但是,如果我用上述link函数注释掉author_path,响应时间仅为9毫秒.在任何一种情况下,数据库查询时间都是相同的(4ms).
我link只是尝试删除和打印author_path(@conn, :show, author).这有帮助,但响应时间仍然超过500毫秒.
感谢您提供有关如何找到这种缓慢性能的来源的任何建议.
我需要以与以下相同的格式生成当前时间的字符串: 20130524T000000Z
该示例是一个时间戳Fri, 24 May 2013 00:00:00 GMT.
我该怎么办?有没有办法在没有外部包装的情况下做到这一点?