我正在阅读 Elixir 文档,并且遇到了 Elixir List 模块的“foldr”函数。我真的很难理解它。这是医生所说的:
Folds (reduces) the given list from the right with a function. Requires an accumulator.
iex> List.foldr([1, 2, 3, 4], 0, fn(x, acc) -> x - acc end)
-2
Run Code Online (Sandbox Code Playgroud)
所以这应该返回-2。但是当我读到它时,我似乎认为它每次都试图将一个数字减0,如果是这样的话我们如何得到-2?我显然不明白累加器,有人可以帮我分解它吗?
我正在尝试使用Postgrex提供的 tsvector 扩展。但我如何在我的 Phoeinx 项目中实际使用它呢?这是我在 dev.exs 中的设置
config :project, Project.Repo,
adapter: Ecto.Adapters.Postgres,
username: "postgres",
password: "postgres",
database: "project_dev",
hostname: "localhost",
pool_size: 10,
extensions: [{Postgrex.Extensions.TSVector}]
Run Code Online (Sandbox Code Playgroud)
这是架构
schema "branches" do
field :address, :string
field :tags, {:array, :string}
field :document, Postgrex.Extensions.TSVector
belongs_to :bank, Bank
timestamps()
end
Run Code Online (Sandbox Code Playgroud)
这不应该给我一个新类型吗TSVector?我收到以下错误
** (ArgumentError) invalid or unknown type Postgrex.Extensions.TSVector for field :document
Run Code Online (Sandbox Code Playgroud)
更新
我尝试将架构设置为以下
field :document, {:array, Postgrex.Lexeme}
Run Code Online (Sandbox Code Playgroud)
从数据库检索值时出现以下错误
** (UndefinedFunctionError) function Postgrex.Lexeme.type/0 is undefined or private
我试图找出如何使用苦艾酒在查询中的嵌套项目上使用参数。
我试图完成的是:
{
users(order: ASC) {
id
email
clients(order: DESC) {
id
email
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的架构和类型:
query do
@desc "Get all users"
field :users, list_of(:user) do
arg :order, type: :sort_order, default_value: :asc
resolve &Resolvers.users/2
end
end
@desc "A user"
object :user do
field :id, :id
field :email, :string
field :clients, list_of(:user)
end
Run Code Online (Sandbox Code Playgroud)
和解析器:
def users(_, args, _) do
args
|> Enum.reduce(User, fn
{:order, order}, query ->
query |> order_by({^order, :email})
end)
|> Repo.all |> Repo.preload([:clients])
end
Run Code Online (Sandbox Code Playgroud)
所以我的问题是我应该如何以及在哪里为客户放置排序参数?通过上面的例子我得到一个错误:
"message": "Unknown …Run Code Online (Sandbox Code Playgroud) 我在获取在我从事的项目之一中使用的依赖项版本时遇到问题。我想在我的代码中获取此依赖项的版本。有什么简单的方法可以做到这一点吗?
我尝试了以下方法:
{:ok, dependency_version} = Mix.Dep.cached
|> Enum.filter(fn(d) -> d.app == :dependency_name end)
|> hd
|> Map.fetch!(:status)
Run Code Online (Sandbox Code Playgroud)
但正如我在文档中读到的那样,如果设置了,Mix.dep.cached可能会返回一个空数组。MIX_NO_DEPS除了创建一个返回自身版本的依赖函数之外,还有其他可靠的方法可以做到这一点吗?
我dialyxir在phoenix项目下的mix.exs文件中有:
{:dialyxir, "~> 0.5", only: [:dev, :test], runtime: false}
Run Code Online (Sandbox Code Playgroud)
但是在mix dialyzer项目路径下运行时出现此错误:
$ mix dialyzer
Checking PLT...
[:asn1, :certifi, :compiler, :connection, :cowboy, :cowlib, :crypto,
:db_connection, :decimal, :eex, :elixir, :ex_json_schema, :eye_drops,
:fastglobal, :forge, :fs, :gettext, :hackney, :httpoison, :idna, :kernel,
:logger, :logger_file_backend, :mariaex, :metrics, :mime, :mimerl, :mnemonics,
:parse_trans, :phoenix, :phoenix_html, :phoenix_pubsub, :plug, :poison,
:pqueue, :pqueue2, :public_key, :ranch, :relax_yaml, :runtime_tools, :sentry,
:ssl, :ssl_verify_fun, :stdlib, :stream_gzip, :syntax_tools,
:unicode_util_compat, :uuid, :yamerl]
Finding suitable PLTs
Looking up modules in dialyxir_erlang-19.3_elixir-1.5.1_deps-dev.plt
Looking up modules …Run Code Online (Sandbox Code Playgroud) 我在 Elixir 中有一个函数,可以在列表中生成三个随机 RGB 元组。
defmodule Color do
@doc """
Create three random r,g,b colors as a list of three tuples
## Examples
iex> colors = Color.pick_color()
iex> colors
[{207, 127, 117}, {219, 121, 237}, {109, 101, 206}]
"""
def pick_color() do
color = Enum.map((0..2), fn(x)->
r = Enum.random(0..255)
g = Enum.random(0..255)
b = Enum.random(0..255)
{r, g, b}
end)
end
Run Code Online (Sandbox Code Playgroud)
当我运行测试时,我的文档测试失败了。生成的元组列表与我的文档测试中定义的不同。如何为返回随机值的函数编写文档测试?
我列出了记录(包)列表。
这些包通过多对多关系链接到项目:
当我列出包时,我想预加载它们的活动项目
def list_packs(params) do
packs = Pack
|> preload_active_items()
|> Repo.all()
|> Repo.preload([:classroom, [packlanguages: :language]])
end
defp preload_active_items(query) do
query
|> join(:left, [pack], _ in assoc(pack, :packitems))
|> join(:left, [_, pi], _ in assoc(pi, :item))
|> where([..., i], i.active == true)
|> preload([_, pi, i], [packitems: {pi, item: i}])
# |> preload([_, pi, i], [packitems: {pi, item: ^from(i, where: i.active == true)}])
end
Run Code Online (Sandbox Code Playgroud)
一切工作正常(我检索了包及其预加载的项目),但我对那些具有活动项目的包进行了过滤。 …
为什么这个守卫不工作?
def method(stuff, %{}), do: stuff
Run Code Online (Sandbox Code Playgroud)
我必须检查地图大小才能使其正常工作?
def method(stuff, data) when map_size(data) == 0, do: stuff
Run Code Online (Sandbox Code Playgroud)
第一个守卫不应该匹配一张空地图吗?
I had to create a SignalR client because I wanted to try Bittrex websockets API, everything went great until I receive responses from the API (Subscription data, simple calls, etc...) they state in the docs:
\n\n\n\n\nAll responses are compressed by the server using GZip (via a \xe2\x80\x98deflate\xe2\x80\x99 API - there are no headers) and base64 encoded prior to transmission. Users must reverse this process to retrieve the JSON payload.
\n
I\'ve tried using :zlib.(gunzip, gzip, uncompress) …
我正在使用 Elixir Bamboo发送电子邮件
我有一些二进制内容,想附加到电子邮件中
简单的方法是:
content = <<binary-content>>
File.write("/tmp/myfile.pdf", content )
data = new_email()
|> to(email)
|> from( "email.com")
|> subject("subject")
|> put_attachment("/tmp/myfile.pdf")
|> put_header("Reply-To", "email.com")
|> html_body(html_body)
File.rm("/tmp/myfile.pdf", content )
Run Code Online (Sandbox Code Playgroud)
有没有办法附加文件而不将其保存在本地?就像是:
content = <<binary-content>>
data = new_email()
|> to(email)
|> from( "email.com")
|> subject("subject")
|> put_attachment(content)
|> put_header("Reply-To", "email.com")
|> html_body(html_body)
Run Code Online (Sandbox Code Playgroud) elixir ×10
ecto ×2
absinthe ×1
dependencies ×1
elixir-mix ×1
email ×1
ex-unit ×1
graphql ×1
preload ×1
version ×1