Elixir - ไม่สามารถเรียกใช้ฟังก์ชันระยะไกลภายในการแข่งขันได้

ฉันกำลังฝึกปฏิบัติเกี่ยวกับการออกกำลังกาย และไม่เข้าใจว่าทำไมฉันจึงได้รับข้อผิดพลาดต่อไปนี้:

(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

ฉันเดาว่าฉันไม่เข้าใจการจับคู่รูปแบบเช่นเดียวกับที่ฉันคิดเพราะฉันไม่ค่อยเข้าใจว่าฉันพยายามเรียกใช้ฟังก์ชันระยะไกลในการแข่งขันอย่างไร ต่อไปนี้เป็นตัวอย่างชุดทดสอบสำหรับบริบท:

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

นี่คือรหัสของฉัน:

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

ฉันไม่ได้แค่ส่งตัวแปรผ่านไปยังฟังก์ชัน analyze/2 เพื่อให้ส่งกลับ boolean ในท้ายที่สุดใช่หรือไม่ ฉันขอขอบคุณข้อมูลเชิงลึกใด ๆ


person jsonkenl    schedule 21.02.2016    source แหล่งที่มา
comment
ฉันคิดว่าปัญหาอยู่ในฟังก์ชัน match?/2 ฉันไม่ได้พยายามค้นหาข้อผิดพลาดอื่น ๆ ในโค้ด แต่จะได้รับการคอมไพล์ถ้าฉันเปลี่ยนฟังก์ชันนั้นเป็นอย่างอื่น คงจะเป็นสิ่งที่สงวนไว้   -  person NoDisplayName    schedule 21.02.2016
comment
@JustMichael แค่นั้นแหละ ขอบคุณสำหรับความช่วยเหลือ หากคุณต้องการโพสต์เป็นคำตอบฉันจะให้เครดิตคุณ   -  person jsonkenl    schedule 21.02.2016


คำตอบ (1)


สิ่งนี้จำเป็นต้องมีคำตอบ ดังนั้นฉันคิดว่าฉันจะเพิ่มเข้าไป match?/2 เป็นฟังก์ชันที่ส่งออกจาก Kernel ตามค่าเริ่มต้น คุณสามารถแทนที่การนำเข้าเริ่มต้นได้ทาง import Kernel, except: [match?: 2]

person asonge    schedule 22.02.2016