假设我们有 2 个结构:
defmodule Algo.A do
defstruct id: nil, foo: nil
end
defmodule Algo.B do
defstruct id: nil, bar: nil
end
Run Code Online (Sandbox Code Playgroud)
以及以下按结构类型过滤的 API:
defmodule Algo do
alias Algo.{A, B}
def filter_by_type(collection, %A{}) do
for %A{} = el <- collection, do: el
end
def filter_by_type(collection, %B{}) do
for %B{} = el <- collection, do: el
end
end
Run Code Online (Sandbox Code Playgroud)
预期的行为只是根据理解中的匹配返回一个集合:
test "filters by struct type" do
collection = [%A{id: 1}, %A{id: 2}, %B{id: 3}]
assert [%A{id: 1}, %A{id: 2}] = Algo.filter_by_type(collection, %A{})
assert …Run Code Online (Sandbox Code Playgroud) 凤凰城的 Web 应用程序。Nginx 用作反向代理。
目前,与往常一样,Web 应用程序处理单个域的请求。如果我将其他域的 DNS 指向同一服务器,我需要更改什么才能让我的 Phoenix 应用程序处理这些域的请求?
服务器上的 Web 应用程序将保留在单个实例中。并且它将可以通过多个域进行访问。不会重定向到主域 - 每个域都会像主域一样处理请求。
mix release和 systemd)全部运行在 VPS (Ubuntu) 上
使用以下命令运行 livebook
LIVEBOOK_IP=<IP> LIVEBOOK_PORT=8089 LIVEBOOK_PASSWORD=<PASS> LIVEBOOK_COOKIE=<COOKIE> livebook server --name livebook@127.0.0.1
可以通过 IP 和端口在浏览器中访问。
mix release使用环境变量运行phoenix应用程序
RELEASE_COOKIE: ${{ secrets.RELEASE_COOKIE }}
RELEASE_DISTRIBUTION: name (literally)
RELEASE_NAME: <appname>
RELEASE_NODE: <appname>@<PUBLIC_IP>
Run Code Online (Sandbox Code Playgroud)
我尝试在同一台机器上运行没有 docker 的 livebook,以确保它们能够连接,但到目前为止还没有运气。
从 Livebook 连接到 phoenix 应用程序既不能从本地安装的 Livebook 上也不能从部署在服务器上的 Livebook 上工作。我正在关注的屏幕截图教程在这里:fly.io 链接
它说该节点无法访问。

== Compilation error in file lib/ecto/query.ex ==
** (Kernel.TypespecError) lib/ecto/query.ex:428: type dynamic/0 is a built-in type and it cannot be redefined
(elixir 1.15.0) lib/kernel/typespec.ex:961: Kernel.Typespec.compile_error/2
(stdlib 5.0.1) lists.erl:1599: :lists.foldl_1/3
(elixir 1.15.0) lib/kernel/typespec.ex:226: Kernel.Typespec.translate_typespecs_for_module/2
could not compile dependency :ecto, "mix compile" failed. Errors may have been logged above. You can recompile this dependency with "mix deps.compile ecto --force", update it with "mix deps.update ecto" or clean it with "mix deps.clean ecto"
ERROR: Service 'cense_live_chat' failed to build: The command '/bin/sh -c …Run Code Online (Sandbox Code Playgroud) 我尝试在 Elixir 中做以下可以在 Python 中实现的事情:
sum = 0
for elem in list:
if elem >= 5:
sum += 1
Run Code Online (Sandbox Code Playgroud)
例如:我要返回号码3给定列表的数字[0, 5, 8, 3, 10]
这就是我在 Elixir 中所做的(这还不起作用):
cond do
Enum.empty?(list) -> 0
length(list) == 1 -> if List.first(list) >= 5 do 1 else 0 end
true ->
Enum.reduce(list, 0, fn x, acc -> if x >= 5 do acc + 1 end end)
end
end
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:** (ArithmeticError) bad argument in arithmetic expression: nil + 1
编辑: …
我正在尝试测试一个构建HashDict的算法,但我不能让我的"预期" 实际上等于我的"实际".例如,
iex> expected = HashDict.new([ key: 12 ])
#HashDict<[foo: 12]>
iex> actual = Dict.put(HashDict.new, "key", 12)
#HashDict<[{"foo", 12}]>
Run Code Online (Sandbox Code Playgroud)
HashDict通过以更加钝的方式声明它,我可以获得文字中的第二个"风格" :
iex> HashDict.new([ { "key", 12 } ])
#HashDict<[{"foo", 12}]>
Run Code Online (Sandbox Code Playgroud)
我非常喜欢我的测试用例的简单Dict文字语法,但是我的实现以递归方式构建了Dict.我怎样才能使这些相等?
我如何在Elixir中查找子字符串是否与另一个位串一起出现?我需要验证位串X是否存在于位串Y中的某个位置以进行测试.使用现有功能有一种简单的方法吗?
x = "bar"
y = "foo bar baz"
some_substring_function(x, y)
Run Code Online (Sandbox Code Playgroud)
与some_substring_function返回truthy值.
提前致谢!
我:cowboy在Elixir中使用Erlang Web框架,我收到了一个错误:cowboy_http_req.reply,这是我的代码:
mix.exs是:
defmodule Example.Mixfile do
use Mix.Project
def project do
[ app: :example,
version: "0.0.1",
deps: deps ]
end
# Configuration for the OTP application
def application do
[
# you should start :inets and :crypto first, or it will not start cowboy
applications: [:inets, :crypto],
mod: {Example, []}
]
end
# Returns the list of dependencies in the format:
# { :foobar, "0.1", git: "https://github.com/elixir-lang/foobar.git" }
defp deps do
[
{ :cowboy, github: …Run Code Online (Sandbox Code Playgroud) 示例似乎由'number'和'integer'组成.他们应该使用哪些相应的情况?
编辑:这是每个的一个例子
@type bar :: %{atom => [{binary, integer}]}
def foo(x) when is_number(x), do: x
Run Code Online (Sandbox Code Playgroud) 我是Elixir/Erlang编程的新手.
如何为Elixir模块实现模块属性,因此我的模块用户可以在模块构造函数中设置.
例如,
defmodule Config do
some_property: nil
other_property: nil
def constructor(one, two) do
some_property = one
other_property = two
end
def get_property_one do
some_property
end
end
Run Code Online (Sandbox Code Playgroud)
谢谢
elixir ×10
erlang ×3
bitstring ×1
cowboy ×1
deployment ×1
ecto ×1
elixir-mix ×1
enums ×1
substring ×1
testing ×1