我正在编写一个模块来查询在线天气API.我决定将它作为一个受监督的应用程序来实现GenServer
.
这是代码:
defmodule Weather do
use GenServer
def start_link() do
GenServer.start_link(__MODULE__, :ok, name: __MODULE__)
end
def weather_in(city, country) do
GenServer.call(__MODULE__, {:weather_in, city, country_code})
end
def handle_call({:weather_in, city, country}) do
# response = call remote api
{:reply, response, nil}
end
end
Run Code Online (Sandbox Code Playgroud)
在我的测试中,我决定使用setup
回调来启动服务器:
defmodule WeatherTest do
use ExUnit.Case
setup do
{:ok, genserver_pid} = Weather.start_link
{:ok, process: genserver_pid}
end
test "something" do
# assert something using Weather.weather_in
end
test "something else" do
# assert something else using Weather.weather_in
end …
Run Code Online (Sandbox Code Playgroud) 我想在Elixir写一个字谜检查器.它需要2个单词,第一个是参考,第二个是作为第一个可能的anagram测试.
我试图用递归和模式匹配来编写它.我in
在一个保护条款中使用运算符时收到错误:
(ArgumentError)运算符输入无效的args,它在保护表达式中使用时需要右侧的编译时列表或范围
我不知道该怎么做才能解决它.这是代码(错误在第4个定义中):
defmodule MyAnagram do
def anagram?([], []), do: true
def anagram?([], word) do
IO.puts 'Not an anagram, the reference word does not contain enough letters'
false
end
def anagram?(reference, []) do
IO.puts 'Not an anagram, some letters remain in the reference word'
false
end
def anagram?(reference, [head | tail]) when head in reference do
anagram?(reference - head, tail)
end
def anagram?(_, [head | _]) do
IO.puts 'Not an anagram, #{head} is not in the reference …
Run Code Online (Sandbox Code Playgroud) 我在elixir中试验Macros.因此,我即将展示的代码当然应该用简单的函数完成,但是......我正在试验!
我想定义2个宏(A和B)并使A使用B来试验宏扩展.当我使用A时,我收到一个编译错误,说明函数 B 未定义.
这是代码:
defmodule MyMacros do
defmacro print_expr(expr) do
quote do
IO.puts(unquote(expr))
end
end
defmacro print_hashes_around(expr) do
quote do
IO.puts "###"
print_expr(unquote(expr))
IO.puts "###"
end
end
end
defmodule MyModule do
require MyMacros
def my_print(expr) do
MyMacros.print_hashes_around(expr)
end
end
MyModule.my_print("hello world")
Run Code Online (Sandbox Code Playgroud)
这是编译错误:
macro_test.exs:17: warning: redefining module MyModule
** (CompileError) macro_test.exs:21: function print_expr/1 undefined
(stdlib) lists.erl:1336: :lists.foreach/2
macro_test.exs:17: (file)
(elixir) lib/code.ex:307: Code.require_file/2
Run Code Online (Sandbox Code Playgroud)
我(错)理解事物的方式:
我对吗 ?
至于建议在农闲,前缀print_expr
与MyMacros.
修复它.我还是不明白为什么.MyModule
需要MyMacros
这样两个宏都应该是已知的并且可以扩展......当我看到unless …
我一直试图用elixir连接到远程ssh服务器.
这就是我在IEX所做的事情:
[Macintosh] elixir/logglycious (master|…)> iex 15-07-20 0:11
Erlang/OTP 17 [erts-6.4] [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false]
Interactive Elixir (1.0.4) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> :application.start(:crypto)
{:error, {:already_started, :crypto}}
iex(2)> :application.start(:public_key)
{:error, {:not_started, :asn1}}
iex(3)> :application.start(:asn1)
:ok
iex(4)> :application.start(:public_key)
:ok
iex(5)> :application.start(:ssl)
:ok
iex(6)> :application.start(:ssh)
:ok
iex(7)> :ssh.connect("my.server.co.uk", 22, [ { :user, 'my_username' } ])
{:error, {:options, {:socket_options, [:inet]}}}
iex(8)>
Run Code Online (Sandbox Code Playgroud)
首先,我必须说这个错误信息根本没有帮助.不过,我得到了社区对Slack的大力支持.有人建议启动inets应用程序.我做了并重新尝试连接,但我又遇到了同样的错误.
我究竟做错了什么 ?更重要的是,我怎样才能在下次找到解决这个问题的方法?
[固定]有多个问题.首先,必须在单引号之间提供服务器.然后确保您的公钥不需要密码.如果是,则可以将其作为选项传递给connect函数.此外,没有必要启动我开始的所有应用程序.:ssh.start
是我唯一需要的人.