elixir 混合药剂

mix.ex
# mix

+ mix phx.new hello
+ mix phoenix.start
+ mix ecto.create
+ mix phx.server
+ iex -S mix phx.server
+ mix etco.gen.migration add_topics
+ mix ecto.migrate
+ mix archive.uninstall phx_new

elixir 提醒灵药

reminder.ex
# to know type
i type
IO.inspect type

# range
players =
  1..12
  |> Enum.map(fn _ -> %Player{} end)

# Ecto // db

mix ecto.create

# Struct

defmodule User do
  defstruct email: nil
end
 
%User{email: "c@c.com"} = struct(%User{}, email: "c@c.com")

# Help
%User{email: "a@a.co"} = %{ %User{} | email: "a@a.co" }
%User{email: "b@b.com"} = Map.put(%User{}, :email, "b@b.com")

# Default

  def test(a \\ "aze") do
    IO.puts(a)
  end

# Help

h(Map.replace!)  

elixir university.exs

university.exs
# Run in shell with
# elixir university.exs

defmodule University do
  def average(p1, p2), do: (p1 + p2) / 2

  def average(p1, p2, p3), do: (p1 + p2 + p3) / 3

  defp professor_base_salary(:adjunct, "US$"), do: 20000
  defp professor_base_salary(:adjunct, "R$"), do: 30000

  defp professor_base_salary(:assistant, currency),
    do: 3 * professor_base_salary(:adjunct, currency)

  defp professor_base_salary(:associate, currency),
    do: 5 * professor_base_salary(:adjunct, currency)

  defp professor_base_salary(:full, currency), do: 10 * professor_base_salary(:adjunct, currency)

  def professor_annual_salary(name, position, currency) do
    {name, professor_base_salary(position, currency), currency}
  end

  def main() do
    IO.puts("Salaries at the University of Some Place (USP)\n")
    {name, salary, currency} = professor_annual_salary("John", :full, "US$")
    IO.puts("#{name} receives an annual salary of #{currency} #{salary}\n")
    {name, salary, currency} = professor_annual_salary("João", :associate, "R$")
    IO.puts("#{name} receives an annual salary of #{currency} #{salary}\n")
  end
end

University.main()

elixir 我的代码问题解决方案 - 第2天:库存管理系统https://adventofcode.com/2018/day/2

我的代码问题解决方案 - 第2天:库存管理系统https://adventofcode.com/2018/day/2

day2.ex
defmodule Day2 do
  def create_map([], map) do
    map
  end

  def create_map([head | tail], map) do
    create_map(tail, Map.update(map, head, 1, fn x -> x + 1 end))
  end

  def process_string(string) do
    values =
      String.codepoints(string)
      |> Enum.sort()
      |> create_map(Map.new())
      |> Map.values()

    {3 in values, 2 in values}
  end

  def process_stream(n_threes, n_twos, []) do
    {n_threes, n_twos}
  end

  def process_stream(o_threes, o_twos, stream) do
    [string] = Enum.take(stream, 1)
    {threes, twos} = process_string(string)
    n_threes = if threes, do: 1, else: 0
    n_twos = if twos, do: 1, else: 0
    process_stream(n_threes + o_threes, n_twos + o_twos, Enum.drop(stream, 1))
  end

  def process_file(stream) do
    process_stream(0, 0, stream)
  end

  ### PART 2

  def aux_differ(x, y, "", "") do
    {x, y}
  end

  def aux_differ(false, y, s1, s2) do
    if String.at(s1, 0) != String.at(s2, 0) do
      aux_differ(true, y, String.slice(s1, 1..-1), String.slice(s2, 1..-1))
    else
      aux_differ(false, y + 1, String.slice(s1, 1..-1), String.slice(s2, 1..-1))
    end
  end

  def aux_differ(true, y, s1, s2) do
    if String.at(s1, 0) != String.at(s2, 0) do
      {false, -1}
    else
      aux_differ(true, y, String.slice(s1, 1..-1), String.slice(s2, 1..-1))
    end
  end

  def differ_by_exactly_one_character(string1, string2) do
    aux_differ(false, 0, string1, string2)
  end

  def new_string(s1, y) do
    String.slice(s1, 0..(y - 1)) <> String.slice(s1, (y + 1)..-1)
  end

  def process_file_differ(file_as_list) do
    for i <- file_as_list do
      for j <- file_as_list do
        {a, b} = differ_by_exactly_one_character(i, j)

        if a do
          IO.puts(new_string(i, b))
        end
      end
    end
  end
end

# Part 1 
# IO.inspect Day2.process_string("ababab")

{x, y} =
  File.stream!("input", [], :line)
  |> Day2.process_file()
  |> IO.inspect()

IO.puts("Value: #{x * y}")

# Part 2

# IO.inspect Day2.differ_by_exactly_one_character("abc", "abd")
# IO.inspect Day2.differ_by_exactly_one_character("abcd", "abce")
# IO.inspect Day2.differ_by_exactly_one_character("abcdfgh", "abcefgh")
# IO.inspect Day2.differ_by_exactly_one_character("bcdfgh", "bcefgh")
# IO.inspect Day2.differ_by_exactly_one_character("cdfgh", "cefgh")
# IO.inspect Day2.differ_by_exactly_one_character("dfgh", "efgh")
# IO.inspect Day2.differ_by_exactly_one_character("abcc", "abdd")

# x1 = "abcdfgh"
# x2 = "abcefgh"
# {true, y} = Day2.differ_by_exactly_one_character(x1, x2)

# new_string = Day2.new_string(x1,y) 

# IO.puts(new_string)

{:ok, file} = File.read("input")
Day2.process_file_differ(String.split(file, "\n"))

elixir 第2部分,refactored.exs

part2-refactored.exs
defmodule Day1 do
  # This is a refactored version of José Valim's code available at https://gist.github.com/josevalim/ea4bf4fb5a009d33ff37f406e25c4749#file-part2-exs

  def make_file_stream(filename) do
    File.stream!(filename, [], :line)
  end

  def map_stream_to_integers(file_stream) do
    Stream.map(file_stream, fn line ->
      {integer, _leftover} = Integer.parse(line)
      integer
    end)
  end

  def halt_if_repeated_frequency_found(x, {current_frequency, seen_frequencies}) do
    new_frequency = current_frequency + x

    if new_frequency in seen_frequencies do
      {:halt, new_frequency}
    else
      {:cont, {new_frequency, MapSet.put(seen_frequencies, new_frequency)}}
    end
  end

  def reduce_stream(stream) do
    Enum.reduce_while(stream, {0, MapSet.new([0])}, &Day1.halt_if_repeated_frequency_found/2)
  end

  def repeated_frequency(file_stream) do
    file_stream
    |> map_stream_to_integers()
    |> Stream.cycle()
    |> reduce_stream()
  end
end

defmodule Main do
  def run_tests() do
    ExUnit.start()

    defmodule Day1Test do
      use ExUnit.Case

      import Day1

      test "final_frequency" do
        assert repeated_frequency([
                 "+1\n",
                 "-2\n",
                 "+3\n",
                 "+1\n"
               ]) == 2
      end
    end
  end

  def process(filename) do
    filename
    |> Day1.make_file_stream()
    |> Day1.repeated_frequency()
    |> IO.puts()
  end

  def error() do
    IO.puts(:stderr, "we expected --test or an input file")
    System.halt(1)
  end

  def main() do
    case System.argv() do
      ["--test"] ->
        run_tests()

      [filename] ->
        process(filename)

      _ ->
        error()
    end
  end
end

Main.main()

elixir part1.exs

part1.exs
defmodule Advent1 do
  defp process(value, []), do: value

  defp process(value, [head | tail]) do
    process(value + String.to_integer(head), tail)
  end

  def do_it(filename) do
    {:ok, file} = File.read(filename)
    process(0, String.split(file, "\n"))
  end
end

IO.puts(Advent1.do_it("input"))
part2.exs
defmodule Advent1 do
  defp process(value, already, [], original) do
    process(value, already, original, original)
  end

  defp process(value, already, [head | tail], original) do
    if value in already do
      value
    else
      process(value + String.to_integer(head), [value | already], tail, original)
    end
  end

  def do_it(filename) do
    {:ok, file} = File.read(filename)
    list = String.split(file, "\n")
    process(0, [], list, list)
  end
end

IO.puts(Advent1.do_it("input"))

elixir fizz.exs

fizz.exs
defmodule Fizz do

  def fizz(x) when rem(x,3)==0 do
    "fizz"
  end
  def fizz(x) do
    x
  end

  def buzz(x) when rem(x,5)==0, do: "buzz"
  def buzz(x), do: x

  def fizzbuzz(x) when rem(x,15)==0, do: "fizzbuzz"
  def fizzbuzz(x) when rem(x,3)==0, do: "fizz"
  def fizzbuzz(x) when rem(x,5)==0, do: "buzz"
  def fizzbuzz(x), do: x

end
livraria.exs
defmodule LivrariaHarryPotter do

  @precovolume 42

  def compra(x,y) when x==1 and y==1 do
     (x*@precovolume + y*@precovolume)*0.95
  end
  def compra(x,0) do
     x*@precovolume
  end
  def compra(0,x) do
     x*@precovolume
  end
  def compra(x,y) when x>=1 and y>=1 do
     (2*@precovolume)*0.95 + compra(x-1,y-1)
  end

  def compra(x,y,z) when x>=1 and y>=1 and x>=1 do
    (3*0.9*@precovolume) + compra(x-1,y-1,z-1)
  end
  def compra(0,y,z), do: compra(y,z)
  def compra(x,0,z), do: compra(x,z)
  def compra(x,y,0), do: compra(x,y)


end

elixir UTFPR的Elixir Coding Dojo - 2018年10月24日

UTFPR的Elixir Coding Dojo - 2018年10月24日

look_and_say.ex
defmodule LookAndSay do
  def lookandsay(numero) do
    numero
    |> Integer.digits()
    |> lookandsay_inicia()
    |> Integer.undigits()
  end

  defp lookandsay_inicia([cabeca | cauda]) do
    lookandsayauxaux(cabeca, cauda, 1)
  end

  defp lookandsayauxaux(estado, [], i) do
    [i, estado]
  end

  defp lookandsayauxaux(estado, [estado | cauda], i) do
    lookandsayauxaux(estado, cauda, i + 1)
  end

  defp lookandsayauxaux(estado, [novoestado | cauda], i) do
    [i, estado] ++ lookandsayauxaux(novoestado, cauda, 1)
  end
end
look_and_say_test.exs
defmodule LookAndSayTest do
  use ExUnit.Case

  test "Look and Say de 1 é 11" do
    assert LookAndSay.lookandsay(1) == 11
  end

  test "Look and Say de 555 é 35" do
    assert LookAndSay.lookandsay(555) == 35
  end

  test "Look and Say de 55 é 25" do
    assert LookAndSay.lookandsay(55) == 25
  end

  test "Look and Say de 11 é 21" do
    assert LookAndSay.lookandsay(11) == 21
  end

  test "Look and Say de 5 é 15" do
    assert LookAndSay.lookandsay(5) == 15
  end

  test "Look and Say de 123 é 111213" do
    assert LookAndSay.lookandsay(123) == 111_213
  end

  test "Look and Say de 551 é 2511" do
    assert LookAndSay.lookandsay(551) == 2511
  end

  test "Look and Say de 555555 é 65" do
    assert LookAndSay.lookandsay(555_555) == 65
  end
end

elixir try_kazan

try_kazan.ex
defmodule Replicator do
  @moduledoc """
  Documentation for Replicator.
  """

  def try_kazan do
    server = Kazan.Server.from_kubeconfig("/home/rdesousa/.kube/config")

    {:ok, res} =
      Kazan.Apis.Core.V1.list_namespaced_pod!("test")
      |> Kazan.run(server: server)

    version = List.last(res.items).metadata.resource_version

    Kazan.Apis.Core.V1.watch_namespaced_pod_list!("test")
    |> Kazan.Watcher.start_link(server: server, send_to: self(), resource_version: version)
  end

  def handle_info(object, state) do
    {:noreply, state}
  end
end

elixir generic_transform.exs

generic_transform.exs
defmodule GenericTransform do

  # Example: transform(list, &list_to_set/1)

  @spec transform(map | list | term, (term -> term)) :: map | list | term
  def transform(map, fun) when is_map(map) do
    Enum.reduce(map, %{}, fn {k, v}, acc ->
      acc
      |> Map.put(k, transform(v, fun))
      |> fun.()
    end)
  end

  def transform(list, fun) when is_list(list) do
    list
    |> Enum.map(&transform(&1, fun))
    |> fun.()
  end

  def transform(term, fun), do: fun.(term)

  def list_to_set(list) when is_list(list) do
    MapSet.new(list)
  end

  def list_to_set(term), do: term
end