有没有办法在换行符上拆分从文件加载的位串?我有这样的事情:
A line of text
Additional line of text
And another line
Run Code Online (Sandbox Code Playgroud)
我想要一个像这样的数组:
["A line of text",
"Additional line of text",
"And another line"]
Run Code Online (Sandbox Code Playgroud)
是否有一个函数来分割换行符上的文本以产生类似这个数组的东西?
提前致谢.
我想在Elixir写一个字谜检查器.它需要2个单词,第一个是参考,第二个是作为第一个可能的anagram测试.
我试图用递归和模式匹配来编写它.我in在一个保护条款中使用运算符时收到错误:
(ArgumentError)运算符输入无效的args,它在保护表达式中使用时需要右侧的编译时列表或范围
我不知道该怎么做才能解决它.这是代码(错误在第4个定义中):
defmodule MyAnagram do
def anagram?([], []), do: true
def anagram?([], word) do
IO.puts 'Not an anagram, the reference word does not contain enough letters'
false
end
def anagram?(reference, []) do
IO.puts 'Not an anagram, some letters remain in the reference word'
false
end
def anagram?(reference, [head | tail]) when head in reference do
anagram?(reference - head, tail)
end
def anagram?(_, [head | _]) do
IO.puts 'Not an anagram, #{head} is not in the reference …Run Code Online (Sandbox Code Playgroud) 我试图在Elixir项目中使用Erlang库时遇到一个小问题.有问题的库是erl8583ISO-8583消息打包和解包.
我找到了一个github存储库erl8583,并将我调整mix.exs为以下内容:
defmodule Iso.Mixfile do
use Mix.Project
def project do
[app: :iso,
version: "0.0.1",
elixir: "~> 1.0",
build_embedded: Mix.env == :prod,
start_permanent: Mix.env == :prod,
deps: deps]
end
def application do
[applications: [:logger]]
end
defp deps do
[{:erl8583, github: "mgwidmann/erl8583"}]
end
end
Run Code Online (Sandbox Code Playgroud)
当我运行mix deps.get和mix deps.compile,它运行平稳.
然后,我尝试启动IEx会话iex -S mix,并收到以下错误:
Unchecked dependencies for environment dev:
* erl8583 (git://github.com/mgwidmann/erl8583.git)
could not find an app file at _build/dev/lib/erl8583/ebin/erl8583.app. This may happen if …Run Code Online (Sandbox Code Playgroud) 在我的凤凰应用程序中,我的用户模型如下:
defmodule MyApp.User do
use MyApp.Web, :model
schema "users" do
field :username, :string, unique: true
field :email, :string, unique: true
field :crypted_password, :string
field :password, :string, virtual: true
timestamps
end
@required_fields ~w(email password username)
@optional_fields ~w()
@doc """
Creates a changeset based on the `model` and `params`.
If no params are provided, an invalid changeset is returned
with no validation performed.
"""
def changeset(model, params \\ :empty) do
model
|> cast(params, @required_fields, @optional_fields)
|> unique_constraint(:email)
|> unique_constraint(:username)
|> validate_format(:email, ~r/@/) …Run Code Online (Sandbox Code Playgroud) 我使用Phoenix内置的gen.HTML生成一个简单的视图,但它不起作用
<%= link "Delete", to: event_path(@conn, :delete, event), method: :delete, data: [confirm: "Are you sure?"], class: "btn btn-danger btn-xs" %>
Run Code Online (Sandbox Code Playgroud)
在页面上它看起来像是假设,但它只是附加#到地址
生成结构:
<form action="/event/1" class="link" method="post">
<input name="_method" type="hidden" value="delete">
<input name="_csrf_token" type="hidden" value="BwUSGQcDO1MwPzw0HBgqLnshHn8HNgAAnCTjuMt0viFshobX4XM/dQ==">
<a class="btn btn-danger btn-xs" data-confirm="Are you sure?" data-submit="parent" href="#">Delete</a>
</form>
Run Code Online (Sandbox Code Playgroud)
我错过了js导入的排序吗?我也可以通过浏览器下载:
//This is being downloaded as phoenix_html.js
// Although ^=parent is not technically correct,
// we need to use it in order to get IE8 support.
var elements = document.querySelectorAll('[data-submit^=parent]')
var len = elements.length
for (var …Run Code Online (Sandbox Code Playgroud) 我正在尝试将jquery添加到Phoenix项目中.
当我在头标记的app.html.eex中链接到jquery,如下所示:
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
Run Code Online (Sandbox Code Playgroud)
...有用.
但是,我不想要Web依赖.我想在应用程序中使用jquery.
我已将jquery.min.js复制到web/static/js目录.并在app.html.eex中引用它,如下所示:
<script src="<%= static_path(@conn, "/js/jquery.min.js") %>"></script>
Run Code Online (Sandbox Code Playgroud)
它不起作用.
将jquery.min.js复制到app.js也不起作用.
有趣的是,当我直接在app.html.eex中将脚本标记放在JS之间时,它可以工作.
只有来自云的直接链接和/或将JS放在app.html.eex中的脚本标记之间才能正常工作?
更新:
早午餐正在将JS复制到priv目录中的app.js. 但是应用程序似乎无法访问它.
我究竟做错了什么.
为什么在Ecto changeset方法中将params设置为默认:empty原子?例如
def changeset(user, params \\ :empty) do
...
Run Code Online (Sandbox Code Playgroud)
这是否允许您使用nil为变量调用changeset方法?
我有一个身份验证插件,我想测试我的控制器.问题是这个插头中的线路有
user_id = get_session(conn, :user_id)
Run Code Online (Sandbox Code Playgroud)
当我使用这种方法时,它总是零(我以前使用过脏黑客,但我不想再这样做了):
@session Plug.Session.init([
store: :cookie,
key: "_app",
encryption_salt: "secret",
signing_salt: "secret",
encrypt: false
])
user = MyApp.Factory.create(:user)
conn()
|> put_req_header("accept", "application/vnd.api+json")
|> put_req_header("content-type", "application/vnd.api+json")
|> Map.put(:secret_key_base, String.duplicate("abcdefgh", 8))
|> Plug.Session.call(@session)
|> fetch_session
|> put_session(:user_id, user.id)
Run Code Online (Sandbox Code Playgroud)
我正在使用此conn发送补丁请求,其会话user_id为nil.IO.puts conn在我的插件中的结果:
%Plug.Conn{adapter: {Plug.Adapters.Test.Conn, :...}, assigns: %{},
before_send: [#Function<0.111117999/1 in Plug.Session.before_send/2>,
#Function<0.110103833/1 in JaSerializer.ContentTypeNegotiation.set_content_type/2>,
#Function<1.55011211/1 in Plug.Logger.call/2>,
#Function<0.111117999/1 in Plug.Session.before_send/2>], body_params: %{},
cookies: %{}, halted: false, host: "www.example.com", method: "PATCH",
owner: #PID<0.349.0>,
params: %{"data" => %{"attributes" => %{"action" …Run Code Online (Sandbox Code Playgroud) 我有一个elixir/OTP应用程序由于内存不足问题而在生产中崩溃.导致崩溃的功能在专用进程中每6小时调用一次.运行需要几分钟(~30),看起来像这样:
def entry_point do
get_jobs_to_scrape()
|> Task.async_stream(&scrape/1)
|> Stream.map(&persist/1)
|> Stream.run()
end
Run Code Online (Sandbox Code Playgroud)
在我的本地机器上,当函数运行时,我看到大型二进制文件内存消耗不断增长:
请注意,当我在运行该函数的进程上手动触发垃圾收集时,内存消耗会显着下降,因此对于无法使用GC的几个不同进程来说肯定不是问题,而只有一个不能正常运行GC.此外,它说,每隔几分钟的过程中是非常重要的并设法GC,但有时还不够.生产服务器只有1GB内存,在GC启动之前就崩溃了.
试图解决我在愤怒中遇到Erlang的问题(参见第66-67页).一个建议是将所有大型二进制文件操作放在一次性过程中.scrape函数的返回值是包含大二进制文件的映射.因此,它们在Task.async_stream"worker"和运行该函数的进程之间共享.因此,从理论上讲,我可以把persist与一起scrape内Task.async_stream.我不想这样做,并persist通过这个过程保持呼叫同步.
另一个建议是:erlang.garbage_collect定期打电话.看起来它解决了这个问题,但感觉太过于hacky.作者也不建议这样做.这是我目前的解决方案:
def entry_point do
my_pid = self()
Task.async(fn -> periodically_gc(my_pid) end)
# The rest of the function as before...
end
defp periodically_gc(pid) do
Process.sleep(30_000)
if Process.alive?(pid) do
:erlang.garbage_collect(pid)
periodically_gc(pid)
end
end
Run Code Online (Sandbox Code Playgroud)
结果内存负载:
我不太明白书中的其他建议如何适应这个问题.
那个案子你会推荐什么?保持hacky解决方案或有更好的选择.
我Mix.Task在/lib/mix/tasks/start.ex中为我的项目创建了新的
defmodule Mix.Tasks.Start do
use Mix.Task
def run(_), do: IO.puts("Hello, World!")
end
Run Code Online (Sandbox Code Playgroud)
现在,它可以从控制台运行,如下所示:
mix start
但是我得到了Dialyzer的错误Callback info about the 'Elixir.Mix.Task' behaviour is not available.它是什么意思以及如何解决这个问题?