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

Android with Functional Programming

takasfz
September 20, 2019

Android with Functional Programming

takasfz

September 20, 2019
Tweet

More Decks by takasfz

Other Decks in Programming

Transcript

  1. ؔ਺ܕϓϩάϥϛϯάͱ͸ w ෭࡞༻͕ͳ͍͜ͱ w ݱࡏͷؔ਺ͷ֎ʹଘࡏ͢ΔσʔλʹཔΒͣ ɺ
 ݱࡏͷؔ਺ͷ֎ʹଘࡏ͢ΔσʔλΛมߋ͠ͳ͍ w ͦͷଞͷʮؔ਺ܕʯͳ΋ͷ͸͢΂ͯɺ͜ͷಛੑͷ೿ੜͰ͋Δ w

    ෆมσʔλɺୈҰڃؔ਺ɺΧϦʔԽɺߴ֊ؔ਺ɺͳͲ "OJOUSPEVDUJPOUPGVODUJPOBMQSPHSBNNJOH IUUQTDPEFXPSETSFDVSTFDPNJTTVFTPOFBOJOUSPEVDUJPOUPGVODUJPOBMQSPHSBNNJOH
  2. var num: Int = 0 fun incrementNum() { num +=

    1 } ෭࡞༻ͷ͋Δؔ਺ ؔ਺ͷ֎ʹଘࡏ͢ΔσʔλΛ ؔ਺ͷத͔Βࢀর͍ͯ͠Δ
  3. var num: Int = 0 fun incrementNum() { num +=

    1 } ෭࡞༻ͷ͋Δؔ਺ ؔ਺ͷ֎ʹଘࡏ͢ΔσʔλΛ ؔ਺ͷத͔Βมߋ͍ͯ͠Δ
  4. fun incrementNum(num: Int): Int { return num + 1 }

    ෭࡞༻ͷͳ͍ؔ਺ ؔ਺ͷ֎ʹଘࡏ͢ΔσʔλΛ ؔ਺ͷத͔Β௚઀ࢀর͠ͳ͍
  5. fun incrementNum(num: Int): Int { return num + 1 }

    ෭࡞༻ͷͳ͍ؔ਺ ؔ਺ͷ֎ʹଘࡏ͢ΔσʔλΛ ؔ਺ͷத͔Β௚઀มߋ͠ͳ͍
  6. fun divide(x: Double, y: Double): Double { if (y ==

    0.0) { throw IllegalArgumentException() } return x / y } ෭࡞༻ͷ͋Δؔ਺ʁ
  7. fun divide(x: Double, y: Double): Double { if (y ==

    0.0) { throw IllegalArgumentException() } return x / y } ෭࡞༻ͷ͋Δؔ਺ʁ ྫ֎͕ൃੜ͢Δͱɺؔ਺ͷ֎ͷঢ়ଶ͕มԽ͢Δ ෭࡞༻͕͋Δ
  8. fun divide(x: Double, y: Double): Double { if (y ==

    0.0) { return Double.NaN } return x / y } ෭࡞༻ͷͳ͍ؔ਺
  9. w બख৘ใϦετ w બख*% લճग़৔ͨ͠Ϩʔεͷ*% લ ճग़৔ͨ͠Ϩʔεͷ݁ՌϦετ  w ϨʔεΧοϓϦετ

    w ϨʔεΧοϓ*% ϨʔεΧοϓ໊শ  άϨʔυ ڝྠ৔*%  w ڝྠ৔Ϧετ w ڝྠ৔*% ڝྠ৔໊শ  αʔό͔Β໯͏σʔλ
  10. var response: KeirinRaceResponse? = null var entries = mutableListOf<EntryTable>() fun

    parse() { for (record in response!!.recordsList) { var previousCup: KeirinCup? = null for (cup in response!!.cupsList) { if (cup.id == record.previousCupId) { previousCup = cup .. αʔό͔Β໯ͬͨσʔλΛύʔε͢Δ
  11. var response: KeirinRaceResponse? = null var entries = mutableListOf<EntryTable>() fun

    parse() { for (record in response!!.recordsList) { var previousCup: KeirinCup? = null for (cup in response!!.cupsList) { if (cup.id == record.previousCupId) { previousCup = cup .. αʔό͔Β໯ͬͨσʔλΛύʔε͢Δ ؔ਺ͷ֎ʹଘࡏ͢ΔσʔλΛ ࢀর͍ͯ͠Δ
  12. var response: KeirinRaceResponse? = null var entries = mutableListOf<EntryTable>() fun

    parse() { for (record in response!!.recordsList) { var previousCup: KeirinCup? = null for (cup in response!!.cupsList) { if (cup.id == record.previousCupId) { previousCup = cup .. αʔό͔Β໯ͬͨσʔλΛύʔε͢Δ ؔ਺ͷ֎ʹଘࡏ͢ΔσʔλΛ มߋ͍ͯ͠Δ
  13. var response: KeirinRaceResponse? = null var entries = mutableListOf<EntryTable>() fun

    parse() { for (record in response!!.recordsList) { var previousCup: KeirinCup? = null for (cup in response!!.cupsList) { if (cup.id == record.previousCupId) { previousCup = cup .. αʔό͔Β໯ͬͨσʔλΛύʔε͢Δ
  14. fun parse(response: KeirinRaceResponse): List<EntryTable> { val entries = mutableListOf<EntryTable>() for

    (record in response.recordsList) { var previousCup: KeirinCup? = null for (cup in response.cupsList) { if (cup.id == record.previousCupId) { previousCup = cup break } .. ؔ਺ͷ֎΁ͷ෭࡞༻Λͳ͘͢
  15. fun parse(response: KeirinRaceResponse): List<EntryTable> { val entries = mutableListOf<EntryTable>() for

    (record in response.recordsList) { var previousCup: KeirinCup? = null for (cup in response.cupsList) { if (cup.id == record.previousCupId) { previousCup = cup break } .. ؔ਺ͷ֎΁ͷ෭࡞༻Λͳ͘͢ ؔ਺ͷ֎ʹଘࡏ͢ΔσʔλΛ ؔ਺ͷத͔Β௚઀ࢀর͠ͳ͍
  16. fun parse(response: KeirinRaceResponse): List<EntryTable> { val entries = mutableListOf<EntryTable>() for

    (record in response.recordsList) { var previousCup: KeirinCup? = null for (cup in response.cupsList) { if (cup.id == record.previousCupId) { previousCup = cup break } .. ؔ਺ͷ֎΁ͷ෭࡞༻Λͳ͘͢ ؔ਺ͷ֎ʹଘࡏ͢ΔσʔλΛ ؔ਺ͷத͔Β௚઀มߋ͠ͳ͍
  17. fun parse(response: KeirinRaceResponse): List<EntryTable> { val entries = mutableListOf<EntryTable>() for

    (record in response.recordsList) { var previousCup: KeirinCup? = null for (cup in response.cupsList) { if (cup.id == record.previousCupId) { previousCup = cup break } .. ؔ਺ͷ֎΁ͷ෭࡞༻Λͳ͘͢
  18. fun parse(response: KeirinRaceResponse): List<EntryTable> { val entries = mutableListOf<EntryTable>() for

    (record in response.recordsList) { var previousCup: KeirinCup? = null for (cup in response.cupsList) { if (cup.id == record.previousCupId) { previousCup = cup break } .. ؔ਺ͷ֎΁ͷ෭࡞༻Λͳ͘͢
  19. fun parse(response: KeirinRaceResponse): List<EntryTable> { val entries = mutableListOf<EntryTable>() for

    (record in response.recordsList) { var previousCup: KeirinCup? = null for (cup in response.cupsList) { if (cup.id == record.previousCupId) { previousCup = cup break } .. ؔ਺ͷ֎΁ͷ෭࡞༻Λͳ͘͢ GPSϒϩοΫͷ֎ʹଘࡏ͢ΔσʔλΛ GPSϒϩοΫͷத͔Βมߋ͍ͯ͠Δ
  20. fun parse(response: KeirinRaceResponse): List<EntryTable> { val entries = mutableListOf<EntryTable>() for

    (record in response.recordsList) { var previousCup: KeirinCup? = null for (cup in response.cupsList) { if (cup.id == record.previousCupId) { previousCup = cup break } .. ؔ਺ͷ֎΁ͷ෭࡞༻Λͳ͘͢
  21. fun parse(response: KeirinRaceResponse): List<EntryTable> { val entries = mutableListOf<EntryTable>() for

    (record in response.recordsList) { val previousCup: KeirinCup = response.cupsList.first { it.id == record.previousCupId } val cupVenue: KeirinVenue = response.venuesList.first { it.id == previousCup.venueId } .. entries.add(EntryTable(record, previousCup, cupVenue)) GPSϒϩοΫͷ֎΁ͷ෭࡞༻Λͳ͘͢
  22. fun parse(response: KeirinRaceResponse): List<EntryTable> { val entries = mutableListOf<EntryTable>() for

    (record in response.recordsList) { val previousCup: KeirinCup = response.cupsList.first { it.id == record.previousCupId } val cupVenue: KeirinVenue = response.venuesList.first { it.id == previousCup.venueId } .. entries.add(EntryTable(record, previousCup, cupVenue)) GPSϒϩοΫͷ֎΁ͷ෭࡞༻Λͳ͘͢ pSTU৚݅ʹ౰ͯ͸·Δ࠷ॳͷཁૉΛฦ͢
  23. fun parse(response: KeirinRaceResponse): List<EntryTable> { val entries = mutableListOf<EntryTable>() for

    (record in response.recordsList) { val previousCup: KeirinCup = response.cupsList.first { it.id == record.previousCupId } val cupVenue: KeirinVenue = response.venuesList.first { it.id == previousCup.venueId } .. entries.add(EntryTable(record, previousCup, cupVenue)) GPSϒϩοΫͷ֎΁ͷ෭࡞༻Λͳ͘͢
  24. fun parse(response: KeirinRaceResponse): List<EntryTable> { val entries = mutableListOf<EntryTable>() for

    (record in response.recordsList) { val previousCup: KeirinCup = response.cupsList.first { it.id == record.previousCupId } val cupVenue: KeirinVenue = response.venuesList.first { it.id == previousCup.venueId } .. entries.add(EntryTable(record, previousCup, cupVenue)) GPSϒϩοΫͷ֎΁ͷ෭࡞༻Λͳ͘͢ GPSϒϩοΫͷ֎ʹଘࡏ͢ΔσʔλΛ GPSϒϩοΫͷத͔Βมߋ͍ͯ͠Δ
  25. fun parse(response: KeirinRaceResponse): List<EntryTable> { return response.recordsList.map { record ->

    val previousCup: KeirinCup = response.cupsList.first { it.id == record.previousCupId } val cupVenue: KeirinVenue = response.venuesList.first { it.id == previousCup.venueId } .. EntryTable(record, previousCup, cupVenue) GPSϒϩοΫͷ֎΁ͷ෭࡞༻Λͳ͘͢
  26. fun parse(response: KeirinRaceResponse): List<EntryTable> { return response.recordsList.map { record ->

    val previousCup: KeirinCup = response.cupsList.first { it.id == record.previousCupId } val cupVenue: KeirinVenue = response.venuesList.first { it.id == previousCup.venueId } .. EntryTable(record, previousCup, cupVenue) GPSϒϩοΫͷ֎΁ͷ෭࡞༻Λͳ͘͢ NBQ֤ཁૉʹϒϩοΫͷॲཧΛద༻ͨ͠ ݁ՌͷϦετΛฦ͢
  27. fun parse(response: KeirinRaceResponse): List<EntryTable> { return response.recordsList.map { record ->

    val previousCup: KeirinCup = response.cupsList.first { it.id == record.previousCupId } val cupVenue: KeirinVenue = response.venuesList.first { it.id == previousCup.venueId } .. EntryTable(record, previousCup, cupVenue) GPSϒϩοΫͷ֎΁ͷ෭࡞༻Λͳ͘͢
  28. fun parse(response: KeirinRaceResponse): List<EntryTable> { return response.recordsList.map { record ->

    val previousCup: KeirinCup = response.cupsList.first { it.id == record.previousCupId } val cupVenue: KeirinVenue = response.venuesList.first { it.id == previousCup.venueId } .. EntryTable(record, previousCup, cupVenue) GPSϒϩοΫͷ֎΁ͷ෭࡞༻Λͳ͘͢ Ұக͢Δσʔλ͕ͳ͍ͱ͖ ྫ֎͕ൃੜ͢Δ
  29. sealed class Try<out A> : TryOf<A> { data class Failure(val

    exception: Throwable) : Try<Nothing>() data class Success<out A>(val value: A) : Try<A>() inline operator fun <A> invoke(f: () -> A): Try<A> = try { Success(f()) } catch (e: Throwable) { Failure(e) } "SSPX5SZ
  30. val tried = Try { .. } if (tried.isFailure()) {

    .. } if (tried.isSuccess()) { .. } tried.fold( ifFailure = { .. }, ifSuccess = { .. } ) "SSPX5SZͷγϯϓϧͳ࢖͍ํ
  31. val tried = Try { .. } if (tried.isFailure()) {

    .. } if (tried.isSuccess()) { .. } tried.fold( ifFailure = { .. }, ifSuccess = { .. } ) "SSPX5SZͷγϯϓϧͳ࢖͍ํ 5SZ\^ϒϩοΫ಺Ͱ ྫ֎͕ൃੜ͢ΔՄೳੑͷ͋ΔॲཧΛߦ͏
  32. val tried = Try { .. } if (tried.isFailure()) {

    .. } if (tried.isSuccess()) { .. } tried.fold( ifFailure = { .. }, ifSuccess = { .. } ) "SSPX5SZͷγϯϓϧͳ࢖͍ํ ྫ֎͕ൃੜͨ͠৔߹ͷॲཧ
  33. val tried = Try { .. } if (tried.isFailure()) {

    .. } if (tried.isSuccess()) { .. } tried.fold( ifFailure = { .. }, ifSuccess = { .. } ) "SSPX5SZͷγϯϓϧͳ࢖͍ํ ॲཧ͕੒ޭͨ͠৔߹ͷॲཧ
  34. val tried = Try { .. } if (tried.isFailure()) {

    .. } if (tried.isSuccess()) { .. } tried.fold( ifFailure = { .. }, ifSuccess = { .. } ) "SSPX5SZͷγϯϓϧͳ࢖͍ํ GPMEͰ·ͱΊͯهड़͢Δ͜ͱ΋Ͱ͖Δ
  35. fun parse(response: KeirinRaceResponse): List<EntryTable> { return response.recordsList.map { record ->

    val previousCup: KeirinCup = response.cupsList.first { it.id == record.previousCupId } val cupVenue: KeirinVenue = response.venuesList.first { it.id == previousCup.venueId } .. EntryTable(record, previousCup, cupVenue) ॲཧͷࣦഊΛ໭Γ஋Ͱදݱ͢Δ Ұக͢Δσʔλ͕ͳ͍ͱ͖ ྫ֎͕ൃੜ͢Δ
  36. fun parse(response: KeirinRaceResponse): Try<List<EntryTable>> { return Try { response.recordsList.map {

    record -> val previousCup: KeirinCup = response.cupsList.first { it.id == record.previousCupId } val cupVenue: KeirinVenue = response.venuesList.first { it.id == previousCup.venueId } .. EntryTable(record, previousCup, cupVenue) ॲཧͷࣦഊΛ໭Γ஋Ͱදݱ͢Δ
  37. fun parse(response: KeirinRaceResponse): Try<List<EntryTable>> { return Try { response.recordsList.map {

    record -> val previousCup: KeirinCup = response.cupsList.first { it.id == record.previousCupId } val cupVenue: KeirinVenue = response.venuesList.first { it.id == previousCup.venueId } .. EntryTable(record, previousCup, cupVenue) ॲཧͷࣦഊΛ໭Γ஋Ͱදݱ͢Δ ෭࡞༻͕ͳ͘ɺ҆શʹ ύʔεͰ͖ΔΑ͏ʹͳͬͨʂ