Skip to content

Instantly share code, notes, and snippets.

@avdi
Last active December 20, 2015 08:19

Revisions

  1. Avdi Grimm revised this gist Jul 28, 2013. 1 changed file with 10 additions and 1 deletion.
    11 changes: 10 additions & 1 deletion def.exs
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,16 @@ def next_state
    (".", live_count) when live_count === 3 -> "o"
    (".", _) -> "."
    (_, x, y) when (x < 0 or y < 0) -> "."

    def just_one_arity(x,y) -> IO.puts "hello"


    # This would internally define both next_state/3 and next_state/2
    # Since these are distinctions that the user largely doesn't care about
    # Since these are distinctions that the user largely doesn't care about

    # Thoughts on referencing functions:
    # The syntax here is intentionally an ugly placeholder because it's not important
    # This is more about the semantics
    $next_state$ # returns an anonymous lambda which dyn-dispatches to all arities, because DWIM
    $next_state/2$ # returns a reference to the specific arity definition of next_state
    $just_one_arity$ # returns just_one_arity/1 since it's unambiguous
  2. Avdi Grimm created this gist Jul 28, 2013.
    15 changes: 15 additions & 0 deletions def.exs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    def next_state
    (board, x, y) ->
    cell = cell_at(board, x, y)
    live_count = live_neighbors(board, x, y)
    next_state(cell, live_count)
    end
    ("o", live_count) when live_count in 2..3 -> "o"
    ("o", _) -> "."
    (".", live_count) when live_count === 3 -> "o"
    (".", _) -> "."
    (_, x, y) when (x < 0 or y < 0) -> "."


    # This would internally define both next_state/3 and next_state/2
    # Since these are distinctions that the user largely doesn't care about