elixir Elixir:系统暂停

Elixir:系统暂停

halt.ex
defmodule H do
  def lt, do: System.halt(0)
end

elixir Elixir:退出当前流程

Elixir:退出当前流程

shutdown.ex
defmodule Sh do
  def tdown, do: exit(:shutdown)
end

elixir Elixir:捕获操作员:&

Elixir:捕获操作员:&

capture_oper.ex
defmodule Hof do
  def tripler(value, function) do
    3 * function.(value)
  end
end

elixir Elixir:|>运营商

Elixir:|>运营商

flatten.ex
defmodule Flat do
    def flatten do
        [1, [2], 3]
        |> List.flatten
        |> Enum.map(fn x -> x * 2 end)
    end
end

elixir Elixir:模块:Count Up,包括Accumulator

Elixir:模块:Count Up,包括Accumulator

count.ex
defmodule Count do
    def countup(limit) do
        countup(1, limit)
    end
    defp countup(count, limit) when count <= limit do
        IO.puts(count)
        countup(count + 1, limit)
    end
    defp countup(count, limit) do
        IO.puts("finished!")
    end
end

elixir Elixir:模块:cond,case,when,atom,:math.sqrt()

Elixir:模块:cond,case,when,atom,:math.sqrt()

drop.ex
defmodule Drop do

  def fall_velocity(planemo, distance) when distance >= 0 do
    # iex(1)> Drop.fall_velocity(:earth, 20)
    # :fast
    gravity = case planemo do
     :earth -> 9.8
     :moon  -> 1.6
     :mars  -> 3.71
    end

    velocity = :math.sqrt(2 * gravity * distance)

    cond do
      velocity == 0 -> :stable
      velocity < 5 -> :slow
      velocity >= 5 and velocity < 10 -> :moving
      velocity >= 10 and velocity < 20 -> :fast
      velocity > 20 -> :speedy
    end
  end

end

elixir Elixir:Module:case,atom,math.sqrt()

Elixir:Module:case,atom,math.sqrt()

drop.ex
defmodule Drop do

  def fall_velocity(planemo, distance) when distance >= 0 do
    case planemo do
     :earth -> :math.sqrt(2 * 9.8 * distance)
     :moon  -> :math.sqrt(2 * 1.6 * distance)
     :mars  -> :math.sqrt(2 * 3.71 * distance)
    end
  end
  
end

elixir Elixir:模块:Guard

Elixir:模块:Guard

drop.ex
defmodule Drop do
  def fall_velocity(:earth, distance) when distance >= 0 do
    :math.sqrt(2 * 9.8 * distance)
  end
  def fall_velocity(:moon, distance) when distance >= 0 do
    :math.sqrt(2 * 1.6 * distance)
  end
  def fall_velocity(:mars, distance) when distance >= 0 do
    :math.sqrt(2 * 3.71 * distance)
  end
end

elixir Elixir:模块:导入:数学

Elixir:模块:导入:数学

drop.ex
defmodule Drop do
  def fall_velocity(distance) do
    import :math, only: [sqrt: 1]  
    sqrt(2 * 9.8 * distance)
  end
end

elixir Elixir:模块:将mps转换为mph或kph

Elixir:模块:将mps转换为mph或kph

convert.ex
defmodule Convert do
  def mps_to_mph(mps) do
   2.23693629 * mps
  end
  def mps_to_kph(mps) do
   3.6 * mps
  end
end