summaryrefslogblamecommitdiff
path: root/lib/util.ex
blob: 22d1034d4070ded5c6ddc3b46fff64fec6ebe51f (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12











                                                         

































                                                                          
   
defmodule Util do

  def plusminus(number) when number > 0, do: "+#{number}"
  def plusminus(0), do: "0"
  def plusminus(number) when number < 0, do: "#{number}"

  def float_paparse(string) do
    string
    |> String.replace(",", ".")
    |> Float.parse()
  end

  def ets_mutate_select_each(ets, table, spec, fun) do
    ets.safe_fixtable(table, true)
    first = ets.select(table, spec, 1)
    do_ets_mutate_select_each(ets, table, fun, first)
  after
    ets.safe_fixtable(table, false)
  end

  defp do_ets_mutate_select_each(_, _, _, :'$end_of_table') do
    :ok
  end

  defp do_ets_mutate_select_each(ets, table, fun, {objs, continuation}) do
    for obj <- objs, do: fun.(table, obj)
    do_ets_mutate_select_each(ets, table, fun, ets.select(continuation))
  end


  def ets_mutate_each(ets, table, fun) do
    ets.safe_fixtable(table, true)
    first = ets.first(table)
    do_ets_mutate_each(ets, table, fun, first)
  after
    ets.safe_fixtable(table, false)
  end

  defp do_ets_mutate_each(ets, table, fun, key) do
    case ets.lookup(table, key) do
      [elem] -> fun.(table, elem)
      _ -> nil
    end
    do_ets_mutate_each(ets, table, fun, ets.next(table, key))
  end

end