我正在使用C#构建的一些代码在Elixir中重建一些东西.
这是非常黑客攻击,但完美的工作(虽然不是在Linux上,因此重建).
基本上它做的是检查一些RSS提要并查看是否有任何新内容.这是代码:
Map historic (URL as key, post title as value).
List<string> blogfeeds
while true
for each blog in blogfeeds
List<RssPost> posts = getposts(blog)
for each post in posts
if post.url is not in historic
dothing(post)
historic.add(post)
Run Code Online (Sandbox Code Playgroud)
我想知道如何在Elixir中有效地进行Enumeration.而且,似乎我在"历史性"中添加东西的过程就是反功能编程.
显然,第一步是声明我的URL列表,但除此之外,枚举的想法正在弄乱我的脑袋.有人可以帮帮我吗?谢谢.
我想知道elixir中的方法名称究竟是什么:
array = [1,2,3]
module_name = :lists
method_name = :nth # this not working
module_name.method_name(1, array) # error, undef function lists.method_name/2
module_name.nth(1, array) # returns 1, module_name is OK. It's an atom
Run Code Online (Sandbox Code Playgroud)
但我可以在erlang中做同样的事情:
A = [1,2,3].
X = lists.
Y = nth.
X:Y(1,A). # returns 1
Run Code Online (Sandbox Code Playgroud)
我怎么能在灵药中做到这一点?
我正在尝试定义一个匿名函数来做一个点积,我可以将它编码为私有函数而没有任何问题,但我正在努力使用匿名函数语法.我知道我可以以不同的方式实现它,但我试图了解如何使用模式匹配和递归来定义匿名函数.这是我目前的实施
dot = fn
[i|input],[w|weights], acc -> dot.(input,weights,i*w+acc)
[],[bias],acc -> acc + bias
end
Run Code Online (Sandbox Code Playgroud)
我在编译时遇到这个错误:
function dot/0 undefined
Run Code Online (Sandbox Code Playgroud)
任何提示?这不可能吗?
我想动态创建一个函数名.我写了这个宏
defmacro generate_dynamic(name) do
quote do
def add_(unquote(name)) do
end
end
end
Run Code Online (Sandbox Code Playgroud)
我用它是这样的:
defmodule AnotherModule do
generate_dynamic :animal
end
Run Code Online (Sandbox Code Playgroud)
现在,我只得到AnotherModule.add_函数定义,而我期望AnotherModule.add_animal函数.
作为Elixir和Web领域的新手(没有Web框架经验),我想知道什么是Plug?据我所知,Cowboy是一个Web服务器(虽然在Erlang,而不是Elixir),而Phoenix是一个用于构建Web应用程序的框架,但是插件是从哪里进来的?它是两者之间的抽象层,还是与Phoenix相同的抽象层中的插件系统?
我有一个Ecto变更集错误的关键字列表我想转换为地图,以便Poison JSON解析器可以正确输出JSON格式的验证错误列表.
所以我得到一个错误列表如下:
[:topic_id, "can't be blank", :created_by, "can't be blank"]
Run Code Online (Sandbox Code Playgroud)
...我想得到一个错误的地图,如下所示:
%{topic_id: "can't be blank", created_by: "can't be blank"}
Run Code Online (Sandbox Code Playgroud)
或者,如果我可以将其转换为字符串列表,我也可以使用它.
完成这两项任务的最佳方法是什么?
我有一个用户模型和一个聊天模型.直观地,多个人将在任何时间属于同一个聊天组,每个人可以拥有许多聊天组.因此聊天组必须属于多个人user_id的.
我的聊天组和用户的架构是:
schema "chatGroups" do
field :name, :string
has_many :messages, Message
belongs_to :user, User
timestamps
end
schema "users" do
field :name, :string
has_many :chatGroups, ChatGroup
timestamps
end
Run Code Online (Sandbox Code Playgroud)
任何建议如何处理这个?
你可以case在Elixir中使用正则表达式吗?
所以有类似的东西:
case some_string do
"string" -> # do something
~r/string[\d]+/ -> # do something
_ -> # do something
end
Run Code Online (Sandbox Code Playgroud) 我正在研究价格格式函数,它采用浮点数并正确表示.
恩.190.5,应该是190,50
这就是我提出的
def format_price(price) do
price
|> to_string
|> String.replace ".", ","
|> String.replace ~r/,(\d)$/, ",\\1 0"
|> String.replace " ", ""
end
Run Code Online (Sandbox Code Playgroud)
如果我运行以下.
format_price(299.0)
# -> 299,0
Run Code Online (Sandbox Code Playgroud)
看起来它只是通过第一次更换.现在,如果我将此更改为以下内容.
def format_price(price) do
formatted = price
|> to_string
|> String.replace ".", ","
formatted = formatted
|> String.replace ~r/,(\d)$/, ",\\1 0"
formatted = formatted
|> String.replace " ", ""
end
Run Code Online (Sandbox Code Playgroud)
然后一切似乎都很好.
format_price(299.0)
# -> 299,00
Run Code Online (Sandbox Code Playgroud)
为什么是这样?