标签: elixir

如何在Elixir控制台中定义命名函数而不得**(ArgumentError)无法调用def/2外部模块?

我可以很好地在模块中定义命名函数,但我还没有把它用在iex>或ex>控制台中.

当我尝试运行def命令时,我不断收到以下错误:

(ArgumentError) cannot invoke def/2 outside module
Run Code Online (Sandbox Code Playgroud)

pprime.exs

IO.puts "initial division test"

defmodule Expand do
    def transform(myvar) do
        8 * myvar + 3;
    end
end

div2 = fn inputnum ->
  [:a, inputnum/2.0, inputnum/3, inputnum/5.0, inputnum/7]
end

output = div2.(20.1)
Run Code Online (Sandbox Code Playgroud)

我可以用elixir运行这个,如下:

$ elixir pprime.exs
Run Code Online (Sandbox Code Playgroud)

但是,在控制台中,我似乎无法做任何这样的事情:

Erlang/OTP 17 [erts-6.3] [source] [64-bit] [async-threads:10] [kernel-poll:false]

Interactive Elixir (1.0.2) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> def transform(myvar) do 8 * myvar + 3; end
** (ArgumentError) cannot invoke def/2 …
Run Code Online (Sandbox Code Playgroud)

elixir named

22
推荐指数
2
解决办法
7085
查看次数

elixir Logger用于列表,元组等

我可以使用elixir记录器来检查字符串

 > str = "string"
 > Logger.info "Here is a #{str}"
 [info] Here is a string
Run Code Online (Sandbox Code Playgroud)

但是当我记录一个列表时,它看起来并不漂亮

 > list = [1,2,3,4,5]
 > Logger.info "Here is a list: #{list}"
 [info] Here is a list: ^A^B^C^D^E^F
Run Code Online (Sandbox Code Playgroud)

当我记录关键字列表时,它会出错

 > kwl = [a: "apple", b: "banana"]
 > Logger.info "Here is a keyword list: #{kwl}"
   ** (ArgumentError) argument error
   (stdlib) :unicode.characters_to_binary([a: "apple", b: "banana"])
   (elixir) lib/list.ex:555: List.to_string/1
Run Code Online (Sandbox Code Playgroud)

如何在Elixir中记录除字符串以外的列表,元组和数据类型?

logging elixir

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

Elixir从列表中删除重复项

有人愿意使用Functional Programming和Elixir Constructs为List(X)中的重复值提供一些替代解决方案吗?

X = [1,26,3,40,5,6,6,7] # the 6 being the duplicate
Run Code Online (Sandbox Code Playgroud)

我想到的用于解决此问题的库存解决方案是迭代列表(X),并添加到新的列表(Y),其中密钥尚不存在.

谢谢

elixir

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

在Elixir中安装依赖项

有没有办法直接通过命令行使用mix或安装Elixir项目的依赖项mix hex

我知道hex通过搜索注册表的选项

$ mix hex.search httpoison

Package    Version  URL
httpoison  0.11.0   https://hex.pm/packages/httpoison
Run Code Online (Sandbox Code Playgroud)

但是,我正在寻找类似的东西

$ mix hex.install httpoison
Run Code Online (Sandbox Code Playgroud)

这将修改我的mix.exs文件,将依赖项的名称和最新版本添加depsapplications列表中,然后运行该应用程序名称

$ mix deps.get
Run Code Online (Sandbox Code Playgroud)

拉取和编译依赖项.

elixir-mix elixir

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

VM正在使用latin1的本机名编码运行,这可能会导致Elixir出现故障,因为它需要utf8

每次执行Elixir代码或输入时,如何解决此警告iex

警告:VM正在使用latin1的本机名编码运行,这可能会导致Elixir出现故障,因为它需要utf8.请确保您的语言环境设置为UTF-8(可以通过在shell中运行"locale"来验证)

$ locale
locale: Cannot set LC_CTYPE to default locale: No such file or directory
locale: Cannot set LC_ALL to default locale: No such file or directory
LANG=en_US.utf8
LANGUAGE=en_US:
LC_CTYPE=UTF-8
LC_NUMERIC="en_US.utf8"
LC_TIME="en_US.utf8"
LC_COLLATE="en_US.utf8"
LC_MONETARY="en_US.utf8"
LC_MESSAGES="en_US.utf8"
LC_PAPER="en_US.utf8"
LC_NAME="en_US.utf8"
LC_ADDRESS="en_US.utf8"
LC_TELEPHONE="en_US.utf8"
LC_MEASUREMENT="en_US.utf8"
LC_IDENTIFICATION="en_US.utf8"
LC_ALL=

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 14.04 LTS
Release:        14.04
Codename:       trusty
Run Code Online (Sandbox Code Playgroud)

ubuntu encoding utf-8 virtual-machine elixir

22
推荐指数
5
解决办法
7968
查看次数

如何在Ecto中更改字段类型?

我有一个架构:

schema "editables" do
    field :title, :string
    field :content, :string

    timestamps
  end
Run Code Online (Sandbox Code Playgroud)

现在我想将一个字段表单的类型更改:integer:binary.编写迁移的正确方法是什么,因为使用add不起作用......?

def change do
    alter table(:editables) do
      add :title, :binary
      add :content, :binary

      timestamps
    end
  end
Run Code Online (Sandbox Code Playgroud)

elixir ecto phoenix-framework

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

Elixir中的函数参数中双反斜杠的含义是什么?

我最近遇到了一个代码段,例如:

def loop(ring_pid \\ self, nil, true) do
  #some code 
end
Run Code Online (Sandbox Code Playgroud)

双反斜杠是什么意思?我用Google搜索并找到http://elixir-lang.org/getting-started/sigils.html,但这适用于正常表达式而不是函数params.

elixir

22
推荐指数
2
解决办法
3910
查看次数

在Elixir中用计数器列出理解

有没有办法在理解中添加循环计数器?

例如,没有计数器的理解:

for c <- ["a", "b"], do: c            # => ["a", "b"]
Run Code Online (Sandbox Code Playgroud)

我怎样才能加入反击呢?像这样的东西:

for c <- ["a", "b"], do: {counter, c} # => [{0, "a"}, {1, "b"}]
Run Code Online (Sandbox Code Playgroud)

elixir

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

适配器Ecto.Adapters.Postgres未编译

我无法创建我的凤凰项目.会喜欢一些关于如何修复它的建议.

设置细节:

  • Ubuntu 16.04.4 LTS
  • Erlang/OTP 21 [erts-10.1] [source] [64-bit] [smp:1:1] [ds:1:1:10] [async-threads:1] [hipe]
  • Elixir 1.7.3(使用Erlang/OTP 20编译)
  • 混合1.7.3(用Erlang/OTP 20编译)
  • Ecto v3.0.0

我正在关注Phoenix Up and Running制作应用程序.

mix phx.new hello
cd hello
mix ecto.create
Run Code Online (Sandbox Code Playgroud)

最后一个命令给了我:

 == Compilation error in file lib/hello/repo.ex ==
 ** (ArgumentError) adapter Ecto.Adapters.Postgres was not compiled, ensure it is correct and it is included as a project dependency
     lib/ecto/repo/supervisor.ex:71: Ecto.Repo.Supervisor.compile_config/2
     lib/hello/repo.ex:2: (module)
     (stdlib) erl_eval.erl:680: :erl_eval.do_apply/6
     (elixir) lib/kernel/parallel_compiler.ex:206: anonymous fn/4 in Kernel.ParallelCompiler.spawn_workers/6
Run Code Online (Sandbox Code Playgroud)

我安装了postgres.我有postgres超级用户.

postgresql elixir ecto phoenix-framework

22
推荐指数
2
解决办法
2991
查看次数

获取 github 操作中特定步骤的输出

我有这个运行测试的 GitHub 操作文件,但现在我正在其中集成松弛通知。我想获取该Run tests步骤的输出并将其作为消息发送到 slack 步骤

  - name: Run tests
    run: |
      mix compile --warnings-as-errors
      mix format --check-formatted
      mix ecto.create
      mix ecto.migrate
      mix test
    env:
      MIX_ENV: test
      PGHOST: localhost
      PGUSER: postgres

  - name: Slack Notification
    uses: rtCamp/action-slack-notify@master
    env:
      SLACK_MESSAGE: Run tests output
      SLACK_TITLE: CI Test Suite
      SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }}
Run Code Online (Sandbox Code Playgroud)

任何帮助都感激不尽。谢谢

github elixir github-actions

22
推荐指数
3
解决办法
2万
查看次数