简体   繁体   English

Elixir-无法在匹配中调用远程功能

[英]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.

相关问题 Elixir:为什么会出现“未定义函数”错误? - Elixir: Why do I get “undefined function” error? 无法将Trip类型的Value调用为Json类型 - Cannot Invoke Value of type Trip into Type Json AWS Lambda 函数的问题,在 cloudwatch 上调用错误 - Issues with AWS Lambda function, invoke error on cloudwatch 在本地类中定义lambda时,C2326函数无法访问“ bar” - C2326 function cannot access 'bar' when defining a lambda inside a local class 在传递给另一个函数的匿名方法中使用开放数组参数出现“无法捕获符号”错误 - 'Cannot capture symbol' error using an open array param inside anonymous method which is passed to another function 是否可以通过 Haskell 中的 function 进行模式匹配? - Is it possible to pattern match by function in Haskell? 数组内的php函数 - php function inside array 如何使用函数对象作为回调来调用对象中的成员函数(处理程序)? - How to use function object as callback to invoke member functions (handlers) in an object? namespace :: function不能用作函数 - namespace::function cannot be used as a function Parse.com入门项目在Xcode 6.3上给出了无法调用'subscribeToChannelInBackground'编译器错误 - Parse.com starter project gives Cannot invoke 'subscribeToChannelInBackground' compiler error on xcode 6.3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM