[英]Elixir - Cannot invoke remote function inside match
I'm working through a practice exercise on exercism and can't figure out why I'm getting the following error: 我正在通过练习进行练习,无法弄清楚为什么会出现以下错误:
(CompileError) anagram.exs:19: cannot invoke remote function String.codepoints/1 inside match
(stdlib) lists.erl:1353: :lists.mapfoldl/3
(stdlib) lists.erl:1353: :lists.mapfoldl/3
I guess I'm not understanding pattern matching as well as I thought because I don't quite understand how I'm trying to call a remote function inside a match. 我猜我不太了解模式匹配,因为我不太了解如何在匹配中调用远程函数。 Here are a couple examples of the test suite for context: 这是上下文测试套件的几个示例:
defmodule AnagramTest do
use ExUnit.Case
test "no matches" do
matches = Anagram.match "diaper", ["hello", "world", "zombies", "pants"]
assert matches == []
end
test "detect simple anagram" do
matches = Anagram.match "ant", ["tan", "stand", "at"]
assert matches == ["tan"]
end
Here's my code: 这是我的代码:
defmodule Anagram do
@doc """
Returns all candidates that are anagrams of, but not equal to, 'base'.
"""
@spec match(String.t, [String.t]) :: [String.t]
def match(base, candidates) do
base
|> String.codepoints
|> Enum.sort
|> scan_for_matches(candidates)
end
defp scan_for_matches(base, candidates) do
Enum.scan candidates, [], &(if analyze(&1, base), do: &2 ++ &1)
end
defp analyze(candidate, base) do
candidate
|> String.codepoints
|> Enum.sort
|> match?(base)
end
defp match?(candidate, base) do
candidate == base
end
end
Am I not just passing variables through to the analyze/2
function so that it ultimately returns a boolean
? 我是否只是将变量传递给analyze/2
函数,以便最终返回boolean
? I appreciate any insight. 我感谢任何见识。
This does need an answer, so I guess I'll add it in. match?/2
is a function exported from Kernel
by default. 这确实需要一个答案,所以我想我会添加它match?/2
是默认情况下从Kernel
导出的函数。 You can override default imports via import Kernel, except: [match?: 2]
. 您可以通过import Kernel, except: [match?: 2]
覆盖默认import Kernel, except: [match?: 2]
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.