Archive for: 𔃴月 2020’

Dart の非同期処理/同期処理

2020年4月26日 Posted by PURGE

thenを使用 (非同期処理)

void main(){
  print("A");
  futurePrint(Duration(milliseconds: 2000), "B")
    .then((status) => print(status));
  print("C");
  futurePrint(Duration(milliseconds: 1000), "D")
    .then((status) => print(status));
  print("E");
}

Future<String> futurePrint(Duration dr, String msg){
  return Future.delayed(dr).then((onValue) => msg);
}

//A
//C
//E
//D
//B

async / await を使用 (同期処理)

void main() async {
  print("A");
  await futurePrint(Duration(milliseconds: 2000), "B")
    .then((status) => print(status));
  print("C");
  await futurePrint(Duration(milliseconds: 1000), "D")
    .then((status) => print(status));
  print("E");
}

Future<String> futurePrint(Duration dr, String msg){
  return Future.delayed(dr).then((onValue) => msg);
}

//A
//B
//C
//D
//E

Elixir 値取得方法

2020年4月22日 Posted by PURGE

# List からの値取り出し
list = [1,2,3,4]

IO.inspect(Enum.at(list, 0))
IO.inspect(Enum.at(list, 1))

# Map からの値取り出し
map = %{a: 1, b: 2, c: 3, d: 4}

IO.inspect(Map.get(map, :a))
IO.inspect(Map.get(map, :b))

# Tuple からの値取り出し
tuple = {1, 2, 3, 4}

IO.inspect(elem(tuple, 0))
IO.inspect(elem(tuple, 1))

Phoenix Server をバックグラウンドにて動作させる方法

2020年4月20日 Posted by PURGE

$ elixir --erl "-detached" -S mix phx.server