Snippets

Gerard Gauthier 7o8oq: Untitled snippet

Created by Gerard Gauthier
defmodule Main do

  def aspellClearHeader do
    receive do
      _ -> :ok
    end
  end

  def aspellGetCorrections(lst \\ []) do
    receive do
      {_, {:data, {:eol, msg}}} ->
        case msg do
          "" -> Enum.reverse(lst)
          _ -> aspellGetCorrections([msg|lst])
        end
    end
  end

  def aspellLoop(pt) do
    receive do
      {pid, {:correct, msg}} ->
        Port.command(pt, msg)
        send(pid, {:correct, aspellGetCorrections})
        aspellLoop(pt)
    end
  end

  def aspellStartApp do
    #This is the part of the code I'm interested in
    pt = Port.open({:spawn_executable, System.find_executable("aspell")}, [:binary, {:args, ["-a"]}, {:line, 1024}])

    Port.connect(pt, self())

    aspellLoop(pt)
    #This is the part of the code I'm interested in

  end

  def aspellFilterErrors(lst) do
    Stream.filter(lst, fn e -> String.first(e) != "@"  end)
    |> Enum.filter(fn e -> String.first(e) != "*" end)
  end

  def main() do

    pid = spawn(Main, :aspellStartApp, [])

    send(pid, {self(), {:correct, "Thwis is the message\n"}})

    receive do
      {:correct, lst} ->
        Enum.each(aspellFilterErrors(lst) ,fn e -> IO.puts e end)
    end

    IO.puts "\n\n"

    send(pid, {self(), {:correct, "This the enpd, my only frieend the end.\n"}})

    receive do
      {:correct, lst} ->
        Enum.each(aspellFilterErrors(lst) ,fn e -> IO.puts e end)
    end

  end
end

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.