标签: elixir

Process.whereis(:user) 返回一个 pid。这个pid是什么?

在 Elixir 的 repl 中,iex当我输入Process.whereis(:user)它时会返回一个pid. 这个:user过程是什么?它有什么作用?它是什么状态?

它似乎被永久阻止或休眠,因为我无法通过:user |> Process.whereis() |> :sys.get_state().

erlang elixir erlang-otp

1
推荐指数
1
解决办法
125
查看次数

如何将十进制转换为整数?

我正在使用库十进制。

如果我有一个浮点十进制:

pry(4)> a1
#Decimal<179.4>
Run Code Online (Sandbox Code Playgroud)

如何转换为十进制整数或 Elixir 整数?我想我应该使用圆形。

pry(5)>  Decimal.round a1, 2
#Decimal<179.40>  # <--- still float
Run Code Online (Sandbox Code Playgroud)

但这不起作用。

我希望能够选择:向上或向下舍入。即 179 或 180。

我知道

Decimal.set_context(%Decimal.Context{Decimal.get_context | rounding: :floor})
Run Code Online (Sandbox Code Playgroud)

但我不知道如何在这里应用它。

elixir

1
推荐指数
1
解决办法
2025
查看次数

如何在phoenix中配置重新编译

我正在使用 phoenix 创建服务器并使用编辑器 VSCode。

当我启动 server:mix phx.server并且我更改了代码时,它不会重新编译,我必须关闭并再次运行。

应该在可以自动重新编译的地方设置扩展或配置吗?

文件dev.exs

config :jwtuser, Jwtuser.Endpoint,
  http: [port: 5000],
  debug_errors: true,
  code_reloader: true,
  check_origin: false,
  watchers: [node: ["node_modules/brunch/bin/brunch", "watch", "--stdin",
                    cd: Path.expand("../assets", __DIR__)]]
Run Code Online (Sandbox Code Playgroud)

在mix.exs 中

def project do
    [
      app: :jwtuser,
      version: "0.0.1",
      elixir: "~> 1.4",
      elixirc_paths: elixirc_paths(Mix.env),
      compilers: [:phoenix, :gettext] ++ Mix.compilers,
      start_permanent: Mix.env == :prod,
      aliases: aliases(),
      deps: deps(),
      erlc_options: erlc_options()
    ]
  end
Run Code Online (Sandbox Code Playgroud)

elixir-mix elixir phoenix-framework

1
推荐指数
1
解决办法
1115
查看次数

"Dialyzer is usually never wrong", but I can't figure out how my @spec is incorrect

I have some code that is failing dialyzer and I cannot understand why. No matter what I put into the @spec at the top of the function, calls to that function return a puzzling dialyzer error. Here is a simplification of the function. As far as I can tell, I have spec'd the function correctly.

@spec balances(uuid :: String.t(), retries :: non_neg_integer) ::
        {:ok, list()}
        | {:internal_server_error, String.t(), String.t()}
        | {:internal_server_error, map | list, String.t()}
def balances(uuid, retries \\ 0) …
Run Code Online (Sandbox Code Playgroud)

elixir dialyzer

1
推荐指数
1
解决办法
638
查看次数

Plug 路由器是否需要匹配/调度管道?

我有一个路由器模块,可以将请求转发到其他路由器。在这个路由器中,我有一个由plug(:match)和组成的管道plug(:dispatch)。

defmodule Example.Router do
  use Plug.Router

  plug(:match)
  plug(:dispatch)

  forward("/check", to: Example.Route.Check)

  get("/", do: send_resp(conn, 200, "router"))
end
Run Code Online (Sandbox Code Playgroud)

在第二个模块中,我有相同的管道:

defmodule Example.Route.Check do
  use Plug.Router

  plug(:match)
  plug(:dispatch)

  get "/", do: send_resp(conn, 200, "ok")
end
Run Code Online (Sandbox Code Playgroud)

我在这里看到的问题是我似乎总是需要plug(:match)并且plug(:dispatch)在所有Plug路由器中。所以我有以下问题:

  1. 这真的有必要吗?
  2. 是否所有路由器都需要在具有路由的同一文件中具有管道?

elixir plug

1
推荐指数
1
解决办法
412
查看次数

如何在 elixir 中使用 tesla 发出帖子请求

我正在尝试使用 tesla 发出帖子请求,但出现错误:

(CaseClauseError) no case clause matching: %{name: "ecdebit"}
(hackney) /deps/hackney/src/hackney_request.erl:312::hackney_request.handle_body/4
(hackney) /deps/hackney/src/hackney_request.erl:81::hackney_request.perform/2
(hackney) /deps/hackney/src/hackney.erl:372::hackney.send_request/2
(tesla) lib/tesla/adapter/hackney.ex:69: Tesla.Adapter.Hackney.request/5
(tesla) lib/tesla/adapter/hackney.ex:31: Tesla.Adapter.Hackney.call/2
Run Code Online (Sandbox Code Playgroud)

我的请求代码是

request_body = %{
  name: "ecdebit",
}
Tesla.post(client, "/contactdb/lists", request_body)
Run Code Online (Sandbox Code Playgroud)

在特斯拉基本网址是:https://api.sendgrid.com/v3并设置授权key。我们如何为 post 请求传递数据?

在特斯拉文档中,将发布请求定义为:

Tesla.post("http://httpbin.org/post", "data", headers: [{"content-type", "application/json"}])
Run Code Online (Sandbox Code Playgroud)

这个星球上有没有人可以帮助摆脱这个小故障:(。

elixir tesla phoenix-framework httpoison

1
推荐指数
1
解决办法
3281
查看次数

了解带有多个子句的 Elixir 函数

我最近开始学习 Elixir。来自面向对象的编程背景,我无法理解 Elixir 函数。

我正在关注 Dave Thomas 的书Programming Elixir >= 1.6,但我不太明白函数是如何工作的。

在书中,他举了以下例子:

handle_open = fn
  {:ok, file} -> "Read data: #{IO.read(file, :line)}"
  {_,  error} -> "Error: #{:file.format_error(error)}"
end

handle_open.(File.open(?"??code/intro/hello.exs"?))   ?# this file exists?
-> "Read data: IO.puts \"Hello, World!\"\n"

 handle_open.(File.open(?"??nonexistent"?))           ?# this one doesn't?
 -> Error: no such file or directory"
Run Code Online (Sandbox Code Playgroud)

我不明白参数是如何工作的。是否有隐含的 if, else 语句隐藏在某处?

elixir pattern-matching

1
推荐指数
1
解决办法
1786
查看次数

Phoenix/Elixir:带有前端应用程序的 CSRF

我在前端使用,API 服务器使用phoenix/elixir,反向代理使用 nginx。

当我尝试制作用于上传文件的表单时,我遇到了麻烦。我如何与csrf?

ajax file-upload elixir phoenix-framework nuxt.js

1
推荐指数
1
解决办法
942
查看次数

Elixir:使用 Dynamic Supervisor 启动孩子的正确方法

我正在尝试创建一个动态主管来启动子进程。我使用三个模块来做到这一点,一个是主模块,第二个是动态主管,第三个是 genserver。我没有收到任何错误,但我看不到任何正在运行的子进程。

这是我的代码。第一个是具有主要方法的模块

defmodule Simulator do
    def main(args) do
        {:ok, super_pid} = PersonManager.start_link(args)
        num_of_persons = 1
        start_persons(num_of_persons)
        IO.inspect PersonManager.count_children, label: "The Persons started in main method are"
    end

    def start_persons(num_of_persons) when num_of_persons >=1 do
        PersonManager.add_node(private_key, public_key, 0)
        start_persons(num_of_persons-1)    
    end
    # num_of_persons == 0 case handled
end   
Run Code Online (Sandbox Code Playgroud)

以下是动态Supervisor

defmodule PersonManager do
  use DynamicSupervisor

  def start_link(args) do
    DynamicSupervisor.start_link(__MODULE__, :ok, name: __MODULE__)
  end

  def add_node(private_key, public_key, num_of_coins) do
    # The code flow does come here, used inspect to check
    child_spec = {Person, {private_key, …
Run Code Online (Sandbox Code Playgroud)

elixir gen-server erlang-supervisor

1
推荐指数
1
解决办法
1646
查看次数

Elixir 案例说明

我写了一个声明如下:

case length(clicks) do
      0 ->
        date_list = []
      1 ->
        date_list = start_date
      _ ->
        date_list = Interval.new(from: start_date, until: end_date) |> Enum.to_list
    end
Run Code Online (Sandbox Code Playgroud)

似乎date_list从未设置过。我究竟做错了什么?

elixir

1
推荐指数
1
解决办法
645
查看次数