Lock in $30 Savings on PRO—Offer Ends Soon! ⏳

今こそ、ラムダ式を考える - なぜあなたはラムダ式を苦手と感じるのか

今こそ、ラムダ式を考える - なぜあなたはラムダ式を苦手と感じるのか

Burikaigi ブリ会議 2024
発表資料

Java, Java SE, Project Lambda, ラムダ式, Lambda Expression
関数型プログラミング

Yuichi.Sakuraba

January 19, 2024
Tweet

More Decks by Yuichi.Sakuraba

Other Decks in Technology

Transcript

  1. ϥϜμࣜಋೖ Java 8ϦϦʔε 2014: ؔ਺ܕϓϩάϥϛϯάػೳͷ୺ॹ Records Sealed Class Pattern Matching

    ݴޠ࢓༷ ୅਺తσʔλܕ ^ ඪ४ϥΠϒϥϦ Stream API CompletableFuture Flow (Reactive Stream) HTTP Client ϑϨʔϜϫʔΫ Spring WebFlux Oracle Helidon Red Hat Quarkus et al.
  2. ϥϜμࣜಋೖ Java 8ϦϦʔε 2014: ؔ਺ܕϓϩάϥϛϯάػೳͷ୺ॹ Records Sealed Class Pattern Matching

    ݴޠ࢓༷ ୅਺తσʔλܕ ^ ඪ४ϥΠϒϥϦ Stream API CompletableFuture Flow (Reactive Stream) HTTP Client ϑϨʔϜϫʔΫ Spring WebFlux Oracle Helidon Red Hat Quarkus et al. ΋ؔ਺ܕϓϩάϥϛϯά͔ΒಀΕΒΕͳ͍ Java खଓ͖ܕ͔Βએݴܕ΁ จ͔Βࣜ΁ ϥϜμࣜಋೖ͔Β೥ ͋ΒͨΊͯϥϜμࣜΛݟ௚ͯ͠ΈΔ ϥϜμࣜΛ࢖͏ ϥϜμࣜͷಈ࡞Λཧղ͢Δ
  3. ϥϜμࣜ ແ໊ ؔ਺ ࢖͏্Ͱ͸ ϝιου͕ॻ͚ΔϥϜμࣜ΋ॻ͚Δ JLS 15.27 A lambda expression

    is like a method ॻ͖ํͷ໰୊Ͱ͸ͳ͘ɺߟ͑ํͷ໰୊ खଓ͖తهड़ͷढറ ͍ʹ͑͠ͷ Java ͷهड़ελΠϧ΁ͷݻࣥ
  4. record StudentScore(String name, String className, int score) {} List<StudentScore> scores

    = ...; ྫ"૊ͷੜెͷฏۉࢉग़ record StudentScore(String name, String className, int score) {} List<StudentScore> scores = ...; double sum = 0.0; int count = 0; for (int i = 0; i < scores.size(); i++) { var ss = scores.get(i); if (ss.className().equals("A")) { sum += ss.score(); count++; } } sum /= count; தؒঢ়ଶอ࣋ͷͨΊϛϡʔλϒϧ είʔϓ͕޿͍ ϧʔϓ੍ޚ ஋औग़ɺ ৚݅ Ճࢉॲཧ͕ҰମԽ ҙຯͷҟͳΔ஋ͷ୅ೖ
  5. record StudentScore(String name, String className, int score) {} List<StudentScore> scores

    = ...; final double ave = scores.stream() .filter(ss -> ss.className().equals("A")) .collect(Collectors.averagingDouble( ss -> ss.scores() )); ஋औग़ ৚݅ Πϛϡʔλϒϧม਺ ฏۉ஋ࢉग़
  6. ϥϜμࣜͷܕ͸ؾʹ͠ͳ͍ List<Integer> nums = ...; var sortedNums = nums.stream() .sorted(

    (x1, x2) -> x1-x2 ) .toList(); ྫϦετͷιʔτ ͜ͷϥϜμࣜͷܕ͕ Comparatorͳͷ͔ BiFunction ͳͷ͔ ߟ͑Δඞཁ͸ͳ͍ ॏཁͳͷ͸  Ҿ਺Ͱൺֱ݁ՌΛ JOU Ͱฦ͢ͱ͍͏͜ͱ
  7. ϥϜμࣜͰॏཁͳͷ͸γάωνϟʔ Ҿ਺ͷݸ਺ܕ ໭Γ஋ͷ༗ແܕ List<Integer> nums = ...; var sortedNums =

    nums.stream() .sorted( (x1, x2) -> x1-x2 ) .toList(); Ҿ਺ݸ਺  Ҿ਺ܕ Integer, Integer ໭Γ஋ ͋Γ ໭Γ஋ܕ Integer