Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

Braga-Elixir Basics

braga-livebook.livemd

Braga-Elixir Basics

Mix.install([
  {:kino, "~> 0.15.3"}
])

import IEx.Helpers

SVG example

This is a CRC example.

  • C
  • R
  • C

This is a mental mnemonic that stands for

  • Construct
  • Reduce
  • Convert
input = "42"

reducer = fn acc, number -> acc + number end

converter = fn acc -> "The ants ARRR is #{acc}" end

input  # "42"
|> String.to_integer() # 42
|> reducer.(1) # 43
|> reducer.(2) # 45
|> reducer.(3) # 48
|> converter.()
defmodule Counter do
  def new(string) do
    String.to_integer(string)
  end

  def add(acc, int) do
    acc + int
  end

  def show(acc) do
    "The ants ARR is #{acc}"
  end
end
"42"
|> Counter.new()
|> Counter.add(1)
|> Counter.add(2)
|> Counter.add(3)
|> Counter.show()
list = [1, 2, 3]
input = "42"
accumulator = 0

list 
|> Enum.reduce(Counter.new(input), &Counter.add/2)
|> Counter.show()

"""

  

"""
|> Kino.Image.new(:svg)
defmodule Canvas do
  def new(point) when is_tuple(point) do
    """
    
     #{Point.show(point)}
    
    """
  end

  def new(points) when is_list(points) do
    points =
      Enum.map(points, fn point -> 
        Point.show(point)
        end)

    """
    
      #{points}
    
    """
  end

  def show(svg) do
    svg 
    |> Kino.Image.new(:svg)
  end
end
Point.new(4, 2)
|> Point.right()
|> Point.down()
|> Point.down()
|> Point.down()
|> Point.down()
|> Canvas.new()
|> Canvas.show()
[Point.new(4, 2), 
  Point.new(5, 2), 
  Point.new(4, 3), 
  Point.new(3, 3)]
|> Point.right()
|> Canvas.new()
|> Canvas.show()
# Don't execute this... bad idea

# 1..1_000_000_000_000 |> Enum.each(fn n -> "string#{n}" |> String.to_atom()  en)
# and you won't be home for Christmas

Points

defmodule Point do
  def new(x, y) do
    {x, y}
  end

  def right({x, y}) do
    {x + 1, y}
  end

  def right(points) when is_list(points) do
    Enum.map(points, &right/1)
  end

  def left({x, y}) do
    {x - 1, y}
  end

  def left(points) when is_list(points) do
    Enum.map(points, &left/1)
  end

  def up({x, y}) do
    {x, y-1}
  end

  def up(points) when is_list(points) do
    Enum.map(points, &up/1)
  end

  def down({x, y}) do
    {x, y + 1}
  end

  def down(points) when is_list(points) do
    Enum.map(points, &down/1)
  end



  def show({x, y}) do
    x = x * 20 
    y = y * 20
    """
    
    """
  end

end
Point.new(0, 0)
|> i()
h Enum.reduce/3

Conway’s game of life

Conway’s game of life is played by the computer on a grid. It has the rules, given a list of “cells”, to determine the next generation.

Rules:

  1. IF a cell has less than 2 neighbors, then in the next generation it will die.
  2. If a cell has more than 3 neighbors, in the next generation, it will die of overcrowding.
  3. If a cell has exactly 2 neighbors, in the next generation, it will stay the same.
  4. if a cell has exactly 3 neighbors, in the generation, it will spring to life.
defmodule Life.Core do
  def new() do
    for _i <- 1..500 do
      x = :rand.uniform(40)
      y = :rand.uniform(40)
      {x, y}
    end
  end
end
alias Life.Core

Core.new()
|> Canvas.new()
|> Canvas.show()