Permalink
Browse files

Use more idiomatic do_foo style for inner funcs.

  • Loading branch information...
1 parent ec5ce8a commit b7126eeee16b726df0b00234fd4aff03a5c3e1a9 @seven1m committed Feb 8, 2014
Showing with 36 additions and 36 deletions.
  1. +4 −4 09-ping.exs
  2. +4 −4 10-sudoku-board.exs
  3. +4 −4 12-sudoku-solver-norvig.exs
  4. +11 −11 29-vector.exs
  5. +13 −13 30-vector.exs
View
@@ -31,7 +31,7 @@ defmodule Subnet do
Enum.each all, fn ip ->
Process.spawn(Ping, :ping_async, [ip, self])
end
- _wait HashDict.new, Enum.count(all)
+ wait HashDict.new, Enum.count(all)
end
@doc """
@@ -42,12 +42,12 @@ defmodule Subnet do
Enum.to_list(1..254) |> Enum.map fn i -> "#{subnet}#{i}" end
end
- defp _wait(dict, 0), do: dict
- defp _wait(dict, remaining) do
+ defp wait(dict, 0), do: dict
+ defp wait(dict, remaining) do
receive do
{ip, exists} ->
dict = Dict.put(dict, ip, exists)
- _wait dict, remaining-1
+ wait dict, remaining-1
end
end
end
View
@@ -17,15 +17,15 @@ defmodule SudokuBoard do
@doc "Returns true if all columns are solved."
def cols_solved?(board) do
- _cols_solved?(board, count(board)-1)
+ do_cols_solved?(board, count(board)-1)
end
- defp _cols_solved?(board, index) when index >= 0 do
+ defp do_cols_solved?(board, index) when index >= 0 do
max = count(board)
col = map board, fn row -> at(row, index) end
- sort(col) == to_list(1..max) and _cols_solved?(board, index-1)
+ sort(col) == to_list(1..max) and do_cols_solved?(board, index-1)
end
- defp _cols_solved?(_, -1), do: true
+ defp do_cols_solved?(_, -1), do: true
end
ExUnit.start
@@ -72,18 +72,18 @@ defmodule SudokuSolver do
def parse_grid(grid, board) do
# To start, every square can be any digit; then assign values from the grid.
values = HashDict.new(lc s inlist board.squares, do: {s, @cols})
- _parse_grid(values, Dict.to_list(grid_values(grid)), board)
+ do_parse_grid(values, Dict.to_list(grid_values(grid)), board)
end
- defp _parse_grid(values, [{square, value} | rest], board) do
- values = _parse_grid(values, rest, board)
+ defp do_parse_grid(values, [{square, value} | rest], board) do
+ values = do_parse_grid(values, rest, board)
if value in '0.' do
values
else
assign(values, square, value, board)
end
end
- defp _parse_grid(values, [], _), do: values
+ defp do_parse_grid(values, [], _), do: values
@doc """
Convert grid into a Dict of {square: char} with '0' or '.' for empties.
View
@@ -73,7 +73,7 @@ defmodule Vector do
"tim"
"""
def get(vec(children: children), index) do
- _get(children, hash(index))
+ do_get(children, hash(index))
end
@doc """
@@ -85,7 +85,7 @@ defmodule Vector do
"tim"
"""
def put(v = vec(size: size, children: children), index, value) do
- children = _put(children, hash(index), value)
+ children = do_put(children, hash(index), value)
if index >= size do
vec(size: index + 1, children: children)
else
@@ -125,36 +125,36 @@ defmodule Vector do
def from_list([], v, _), do: v
# traversed down to a non-existent key
- defp _get(nil, _) do
+ defp do_get(nil, _) do
nil
end
# at the last node of our tree, so we should have a value!
- defp _get({val, _}, []) do
+ defp do_get({val, _}, []) do
val
end
# traverse down the tree to get the value
- defp _get({_, children}, [pos | hash_rest]) do
+ defp do_get({_, children}, [pos | hash_rest]) do
node = elem(children, pos)
- _get(node, hash_rest)
+ do_get(node, hash_rest)
end
# at the last leaf of our branch, so store the value
# FIXME our hash function will have collisions, so we really should
# store duplicates properly here, but we don't yet
- defp _put(_, [], value) do
+ defp do_put(_, [], value) do
{value, @template}
end
# build a new branch
- defp _put(nil, hash, value) do
- _put({nil, @template}, hash, value)
+ defp do_put(nil, hash, value) do
+ do_put({nil, @template}, hash, value)
end
# traverse down the tree to store the value
- defp _put({val, children}, [pos | hash_rest], value) do
- tree = _put(elem(children, pos), hash_rest, value)
+ defp do_put({val, children}, [pos | hash_rest], value) do
+ tree = do_put(elem(children, pos), hash_rest, value)
{val, set_elem(children, pos, tree)}
end
View
@@ -50,7 +50,7 @@ defmodule Vector do
"mac"
"""
def get(vec(depth: depth, tree: tree), index) do
- _get(tree, key(index, depth))
+ do_get(tree, key(index, depth))
end
@doc """
@@ -70,7 +70,7 @@ defmodule Vector do
tree = [tree]
end
# attach node
- tree = _put tree, key(index, depth), value
+ tree = do_put tree, key(index, depth), value
vec(size: size, depth: depth, tree: tree)
end
@@ -85,18 +85,18 @@ defmodule Vector do
6
"""
def reduce(vec(size: size, depth: depth, tree: tree), acc, fun) do
- _reduce tree, depth-1, size, 0, acc, fun
+ do_reduce tree, depth-1, size, 0, acc, fun
end
- defp _reduce(node, depth, size, index, acc, fun) when depth > 0 and is_list(node) do
+ defp do_reduce(node, depth, size, index, acc, fun) when depth > 0 and is_list(node) do
Enum.reduce Enum.with_index(node), acc, fn {n, i}, acc ->
- _reduce n, depth-1, size, index + (i * @width), acc, fun
+ do_reduce n, depth-1, size, index + (i * @width), acc, fun
end
end
- defp _reduce(node, _, size, index, acc, fun) when is_list(node) do
+ defp do_reduce(node, _, size, index, acc, fun) when is_list(node) do
Enum.reduce Enum.slice(node, 0..(size-index-1)), acc, fun
end
- defp _reduce(_, _, _, _, acc, _), do: acc
+ defp do_reduce(_, _, _, _, acc, _), do: acc
defp key(index, depth, indeces // []) when depth > 0 do
level = (depth - 1) * @bits
@@ -112,20 +112,20 @@ defmodule Vector do
end
defp tree_from_list(list, _), do: list
- defp _get(node, [idx | rest_key]) do
+ defp do_get(node, [idx | rest_key]) do
node = Enum.at(node, idx)
- _get(node, rest_key)
+ do_get(node, rest_key)
end
- defp _get(node, []), do: node
+ defp do_get(node, []), do: node
- defp _put(tree, [idx | rest_key], value) do
- rest = _put(Enum.at(tree, idx) || [], rest_key, value)
+ defp do_put(tree, [idx | rest_key], value) do
+ rest = do_put(Enum.at(tree, idx) || [], rest_key, value)
if length(tree) <= idx do # expand this node
tree = tree ++ List.duplicate(nil, idx - length(tree) + 1)
end
List.replace_at tree, idx, rest
end
- defp _put(_, [], value), do: value
+ defp do_put(_, [], value), do: value
end
ExUnit.start

0 comments on commit b7126ee

Please sign in to comment.