Ordering and Ordered

F7d2aefb506d0ea0783c559df9865f25?s=47 Seiya IZUMI
November 29, 2018

Ordering and Ordered

How do you sort out your class instances in Scala? There are two useful traits for sorting: Ordered and Ordering. This presentation focuses on how to use these traits in the most efficient manner.

F7d2aefb506d0ea0783c559df9865f25?s=128

Seiya IZUMI

November 29, 2018
Tweet

Transcript

  1. Ordered & Ordering Seiya Izumi

  2. About Me • Seiya Izumi (@sy_izumi) • Go ͱ Scala

    ͕ϝΠϯ • ࠷ۙͷϚΠϒʔϜ͸ Vape Ͱ͢
  3. Ordered ͱ Ordering ʹ͍ͭͯݟ͚ͭͨ͜ͱΛ ࿩͠·͢

  4. Case Class Λιʔτ͍ͨ͠!

  5. Ordered τϨΠτ

  6. Ordered τϨΠτ • compareϝιουΛ࣮૷͢Δ͜ͱͰ “<“ ΍ “>” ͳͲͷൺֱͷΦϖϨʔλ͕࢖͑ΔΑ͏ʹ ͳΔ

  7. ͕ͩ… • ൺֱΛ͢ΔͨΊʹ೚ҙͷΫϥεʹຖճ Ordered τϨΠτΛϛΫεΠϯ͠ͳ͍ͱ͍͚ͳ͍ͷ ͔…? • ఆ͕ٛॻ͖׵͑ΒΕͳ͍ࣄ৘͕͋Δͱ͖͸໽հ ͩͳ •

    ଞʹ΋΍Γํ͸ͳ͍ͷ͔
  8. Ordering τϨΠτ

  9. Ordering τϨΠτ

  10. Ordering τϨΠτ • ೚ҙͷΫϥεͷू߹ʹର͢Δൺֱͷετϥς δΛ༩͑Δ͜ͱ͕Ͱ͖Δͧ

  11. SortingΛݟͯΈΔ ͜Ε͸ Context Bound ͷ༧ײ

  12. ͱ͍͏͜ͱ͸implicitʹͰ͖Δ

  13. ΋ͬͱ Ordering ΛݟͯΈΔ

  14. Optionͷൺֱ΋༻ҙ͞ΕͯΔ ͜Μͳײ͡ͰimplicitΛ࢖͍ͬͯΖΜͳιʔτ͕༧Ί༻ҙ͞Ε͍ͯΔ

  15. ோΊ͍ͯΔͱ͜ΜͳίʔυΛൃݟ Ͳ͏΍ΒIterableͷฒͼସ͑ϩδοΫͬΆ͍͜ͱ͸Θ͔Δ

  16. ͜͏͍͏ͷ͕ΠέΔΜͩΖ͏͔

  17. ͜͏͍͏ͷ͕ΠέΔΜͩΖ͏͔ ίϯύΠϧΤϥʔ

  18. ·ͨผͷఆٛΛൃݟ

  19. ·ͨผͷఆٛΛൃݟ Seq্͕ݶڥքʹͳͬͯΔ!

  20. ·ͨผͷఆٛΛൃݟ ͳΔ΄Ͳ Implicits ΛΠϯϙʔτ͢Ε͹͍͍ͷ͔

  21. ͜Ε͸Πέͨʂ

  22. ͡Ό͋͜Ε͸ͳΜͳͷ͔…

  23. ྺ࢙Λඥղ͘

  24. GithubΛݟͯΈΑ͏

  25. ͦΕͬΆ͍΋ͷΛൃݟ

  26. implicit def Iterable ͷ΄͏ͷ࣮૷͸ Iterable ʹର͚ͯͩ͠ Ordering Λద༻͍ͯͯ͠ɺαϒΫϥεʹ͸ద༻͍ͯ͠ͳ͍ͷ͕ ໰୊ͩɻ͔ͩΒ implicit

    ϝιου͕2ͭଘࡏ͍ͯ͠ΔΜͰ͸ͳ͍ ͔ͱࢥ͏ɻԿͰ2ͭඞཁͳͷ͔͸஌ΒΜ͚Ͳɻ
  27. ͪΐͬͱߟ͑ͯΈ͚ͨͲ Iterable ͸ཁૉॱংͷ Ұ؏ੑΛอূ͠ͳ͍͔Β implicit def Iterable ͷ ΄͏͸ফͯ͠͠·͏ͷ͕ଥ౰͡Όͳ͍͔ͳʁ

  28. ͦͷͱ͓Γͩʂ ফͦ͏ ͦ͏ͩͶɻͦͬͪ͸ফͯ͠ Seq ʹରԠ͢Δ΄͏͚ͩ࢒ͦ͏

  29. ͱ͍͏͜ͱͰ 2.13 ͔Β͸ Seq ͱ SortedSet ͷ ΈΛ্ݶڥքͱ͢ΔΑ͏ʹ

  30. Deprecated ʹͳͬͨ Iterable ͘Μ

  31. ୭͕ͳΜͷܦҢͰ ௥Ճͨ͠ͷ͔

  32. ෼͔Βͳ͍

  33. ίʔυίϝϯτͬͯ େࣄͰ͢Ͷ…