Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Vavrのすすめ

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
Avatar for kawagoo kawagoo
October 27, 2024
540

 Vavrのすすめ

Avatar for kawagoo

kawagoo

October 27, 2024
Tweet

Transcript

  1. 2 @Copyright BIGLOBE inc. 2024. All rights reserved  Vavrとは? •

    Javaの関数型プログラミングライブラリ ◦ https://vavr.io/ ◦ https://github.com/vavr-io/vavr ◦ JAVAをひっくり返すとVAVr • Java標準にないイミュータブルなデータ構造を利用できる • 所属しているチームでJavaを使用する際はマストで採用
  2. 3 @Copyright BIGLOBE inc. 2024. All rights reserved  Vavrが用意しているデータ構造(一部) •

    Tuple ◦ いくつかの値(異なる型でも良い)の組を保持する型 • Functions ◦ Function0〜Function8 といった標準にはない3引数以上のものが ある • Values ◦ 1つの型の値を0個以上持つようなオブジェクトを抽象化した インターフェース ▪ Option • ある型の値を持つか持たないかどちらかを表現できる ▪ Try • 例外を戻り値のように扱うことができる ▪ Either • 2つの型のうちどちらか一方だけを持つ型 • 2つの型は Left, Right と呼ばれ、成功時の値とエラー値を保持できる
  3. 4 @Copyright BIGLOBE inc. 2024. All rights reserved  例 files

    = sftp.getFiles("test.csv"); // sftpでファイル取得 if (files.isEmpty()) { throw new RuntimeException("No file found"); } String csvContent; try { csvContent = CSVParser.parse(files.get(0)); // ファイルをCSVにパース } catch (IOException e) { throw new RuntimeException("CSV parse failed", e); } return new XXXReader(csvContent);
  4. 5 @Copyright BIGLOBE inc. 2024. All rights reserved  例 files

    = sftp.getFiles("test.csv"); if (files.isEmpty()) { throw new RuntimeException("No file found"); } String csvContent; try { csvContent = CSVParser.parse(files.get(0)); } catch (IOException e) { throw new RuntimeException("CSV parse failed", e); } return new XXXReader(csvContent); return Try.of(() -> sftp.getFiles("test.csv") ) .filter( f -> !f.isEmpty(), f -> new RuntimeException("No file found") ) .mapTry( f -> CSVParser.parse(f.get(0)) ) .recoverWith( IOException.class, e -> Try.failure(        new RuntimeException("CSV parse failed", e)) ) .map(XXXReader::new) .get();
  5. 6 @Copyright BIGLOBE inc. 2024. All rights reserved  ちゃんとメンテされてるん? •

    2021年7月に0.10.4がリリースされて以来、アップデートがなかったが、 今月0.10.5にアップデート ◦ 新しい maintainer に変わったとのこと