2017-04-17 52 views
1

我以纯粹的OO方式实现了Ruby中的二叉树迷宫代。我试图在Elixir中将其重写为学习练习,但我遇到了OO与FP范例的一些问题。Elixir中的二叉树迷宫代

我渲染一个包含单元格的网格。当使用二叉树算法在网格中行走时,对于我决定与其旁边的北部或东部单元格连接的每个单元格。这种链接在Ruby实现中是双向的:

def link(cell, bidirectional=true) 
    @links[cell] = true 
    cell.link(self, false) if bidirectional 
    self 
end 

def unlink(cell, bidirectional=true) 
    @links.delete cell 
    cell.unlink(self, false) if bidirectional 
    self 
end 

因此,它将单元连接到邻居和单元的邻居。我不知道如何在Elixir中做到这一点。我有功能的第一部分下来:

def link(cell, neighbour, bidirectional) do 
    %{ cell | links: cell.links ++ [neighbour]} 
end 



test "it links cells in a bidirectional way" do 
    cell = Cell.create(1, 1) 
    neighbour = Cell.create(2, 2) 

    %{ row: _, column: _, links: cell_links } = Cell.link(cell, neighbour, true) 
    assert Enum.member? cell_links, neighbour 
    # ?? check if neighbour links includes cell, but cannot get a reference to "new" neighbour 
end 

但后来双向通话给我麻烦。我可以在没有问题的情况下进行调用,但由于我正在处理不可变数据,因此我将永远无法使用正确的链接数组获取“新”邻近单元的引用。

为每个单元实现GenServer似乎有点像我的反模式。肯定必须有一种方法来以纯粹的功能方式来实现这种行为;我是新来的FP,但会喜欢一些帮助。

回答

1

在将OO映射到顺序Elixir(一般的函数式语言)时,您可以使用On模式,您可以创建一个数据对象(不是OO对象)并将其作为第一个参数传递给函数。这样,您就可以在每次通话中转换数据。

所以,你的api将形状像def link(maze, cell, bidirectional \\ true)。使用地图来表示迷宫,将{x,y}元组作为关键字,并使用地图作为值,以访问单个单元格并更新它们。

这里有一些未经测试的代码作为例子。

def Maze do 
    def new, do: %{cells: %{], links: %{}, start: {0,0}}} 

    def link(maze, cell1, cell2, bidirectional \\ true) do 
    maze 
    |> put_in([:links, cell2], true) 
    |> link_bidirectional(cell1, bidirectional) 
    end 

    defp link_bidirectional(maze, _, _, false), do: maze 
    defp link_bidirectional(maze, cell1, cell2, _) do 
    link(maze, cell2, cell1, false) 
    end 
end 

编辑:这里是用于连接

defmodule Maze do 
    def new do 
    %{cells: %{{0, 0} => Cell.create(0,0)}, tree: {{0, 0}, nil, nil}} 
    end 

    def new_cell(maze, row, column) do 
    # ignoring the tree for now 
    put_in(maze, [:cells, {row, column}], Cell.create(row, column)) 
    end 

    def link(maze, cell1, cell2, bidirectional \\ true) 
    def link(maze, %{} = cell1, %{} = cell2, bidirectional) do 
    maze 
    |> update_in([:cells, cell1[:origin]], &(Cell.link(&1, cell2))) 
    |> do_bidirectional(cell1, cell2, bidirectional, &link/4) 
    end 
    def link(maze, {_, _} = pt1, {_, _} = pt2, bidirectional) do 
    link(maze, maze[:cells][pt1], maze[:cells][pt2], bidirectional) 
    end 

    def unlink(maze, %{} = cell1, %{} = cell2, bidirectional \\ true) do 
    maze 
    |> update_in([:cells, cell1[:origin]], &(Cell.unlink(&1, cell2))) 
    |> do_bidirectional(cell1, cell2, bidirectional, &unlink/4) 
    end 

    defp do_bidirectional(maze, _, _, false, _), do: maze 
    defp do_bidirectional(maze, cell1, cell2, _, fun) do 
    fun.(maze, cell2, cell1, false) 
    end 
end 

defmodule Cell do 
    def create(row,column), do: %{origin: {row, column}, links: %{}} 
    def link(self, cell) do 
    update_in(self, [:links, cell[:origin]], fn _ -> true end) 
    end 
    def unlink(self, cell) do 
    update_in(self, [:links], &Map.delete(&1, cell[:origin])) 
    end 
end 

iex(26)> Maze.new() |> 
...(26)> Maze.new_cell(0,1) |> 
...(26)> Maze.new_cell(1,0) |> 
...(26)> Maze.link({0,0}, {0,1}) |> 
...(26)> Maze.link({0,0}, {1,0}) 
%{cells: %{{0, 
    0} => %{links: %{{0, 1} => true, {1, 0} => true}, origin: {0, 0}}, 
    {0, 1} => %{links: %{{0, 0} => true}, origin: {0, 1}}, 
    {1, 0} => %{links: %{{0, 0} => true}, origin: {1, 0}}}, 
    tree: {{0, 0}, nil, nil}} 
iex(27)> 
官能溶液