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

Scalaでつくる ちょっとしたCLIツール

Scalaでつくる ちょっとしたCLIツール

Shunsuke Tadokoro

April 18, 2019
Tweet

More Decks by Shunsuke Tadokoro

Other Decks in Technology

Transcript

  1. 4DBMBͰͭ͘Δ

    ͪΐͬͱͨ͠$-*πʔϧ
    !UPEPLS
    ָ͘͠

    View full-size slide

  2. ୭ͩ
    ాॴॣ༎!UPEPLS
    ٻਓݕࡧΤϯδϯ8FCΫϩʔϥɺ޿ࠂ഑৴γεςϜɺ

    ޿ࠂϩάج൫ɺ0QFO*%$POOFDUೝূɺ3%#.4͓Ҿӽ͠

    View full-size slide

  3. 4DBMB
    w +BWBͱͷ૬ޓӡ༻ੑ
    w γʔϜϨεͳݺͼग़͠ɺ+BWBඪ४ϥΠϒϥϦͷ࠶ར༻
    w ؆ܿੑ
    w লུՄೳͳߏจɺܕਪ࿦ɺڧྗͳඪ४ϥΠϒϥϦ
    w ந৅౓ͷߴ͍ίʔυɺ৽੍͍͠ޚߏจΛఆٛͰ͖Δදݱྗ
    w 8IBUͷڧௐɺ)PXͷӅณ
    w ੩తܕ෇͚
    w ݕূՄೳੑɺϦϑΝΫλͷ͠΍͢͞ɺυΩϡϝϯτੑ

    View full-size slide


  4. 4DBMBΛձࣾͰ࢖͍͍͚ͨͲ

    Ͳ͏ͨ͠Β͍͍ͩΖ͏ʁ
    ձࣾͷٕज़తํ਑ͱ͔ɺϝϯόʔͷख़࿅౓ͱ͔ɺϓϩμΫτॻ͖௚͠ͱ͔

    View full-size slide


  5. 4DBMBΛձࣾͰ࢖͍͍͚ͨͲ

    Ͳ͏ͨ͠Β͍͍ͩΖ͏ʁ
    ձࣾͷٕज़తํ਑ͱ͔ɺϝϯόʔͷख़࿅౓ͱ͔ɺϓϩμΫτॻ͖௚͠ͱ͔

    ·ͣ࢖͍·͠ΐ͏ʂ

    ͍͖ͳΓϓϩμΫτʹಋೖ͢Δͷ͸େมɻ

    ͳͷͰɺ·ͣ͸ݸਓͰ׬݁͢Δۀ຿ʹ࢖ͬͯΈͯɺ
    ʮΑ͔ͬͨΑʂʯʮ໘ന͍Αʂʯͱ෍ڭ͍ͯ͘͠ͷ͕Α͍͔΋͠Ε·ͤΜɻ

    View full-size slide


  6. 4DBMBͬͯɺͦͦ͜͜େ͖ͳ

    ϓϩμΫτ޲͚ͳΜ͡Όͳ͍ͷʁ
    5XJUUFSͱ͔"QBDIF4QBSLͱ͔ΞυςΫͱ͔

    View full-size slide


  7. 4DBMBͬͯɺͦͦ͜͜େ͖ͳ

    ϓϩμΫτ޲͚ͳΜ͡Όͳ͍ͷʁ
    5XJUUFSͱ͔"QBDIF4QBSLͱ͔ΞυςΫͱ͔

    ͪΐͬͱͨ͠εΫϦϓτΛ

    ॻ͘ͷʹ΋͍͍ײ͡Ͱ͢
    (*U)VCͷτϨϯυΛݟΔͱɺ͔֬ʹ෼ࢄγεςϜ΍Ϗοάσʔλɺ

    ػցֶशܥͷϓϩμΫτ͕ଟ͍Ͱ͢ɻ
    ͕ɺͪΐͬͱͨ͠ίʔυΛॻ͍ͯαοͱಈ͔͢͜ͱ΋࣮͸ಘҙͳΜͰ͢ɻ

    View full-size slide

  8. r.BSUJO0EFSTLZ -FY4QPPO BOE#JMM7FOOFST
    5IFOBNF4DBMBTUBOETGPSlTDBMBCMFMBOHVBHFz
    ʜ

    :PVDBOBQQMZ4DBMBUP

    BXJEFSBOHFPGQSPHSBNNJOHUBTLT 

    GSPNXSJUJOHTNBMMTDSJQUTUPCVJMEJOHMBSHFTZTUFNT

    View full-size slide


  9. 4DBMB͸େֻ͔ΓͳϓϩμΫτΛ࡞Δͷʹద͍ͯ͠ΔݴޠͰ͕͢ɺ
    )BDLZͳ਺ߦd਺ेߦͷ

    εΫϦϓςΟϯά΋े෼

    շదʹͰ͖·͢

    View full-size slide


  10. 4DBMB͸େֻ͔ΓͳϓϩμΫτΛ࡞Δͷʹద͍ͯ͠ΔݴޠͰ͕͢ɺ
    )BDLZͳ਺ߦd਺ेߦͷ

    εΫϦϓςΟϯά΋े෼

    շదʹͰ͖·͢

    View full-size slide

  11. #

    %FNPOTUSBUJPO5JNF
    .BSLEPXOΛࣼΊಡΈ͢Δ

    View full-size slide

  12. TDBMBJP4PVSDF
    w ϩʔΧϧϦϞʔτͷϑΝΠϧಡΈॻ͖ʹศར
    w ςΩετϑΝΠϧΛߦ୯ҐDIBS୯ҐͰJUBSBUJWFʹ

    ॲཧͰ͖ΔͷͰɺϑΝΠϧαΠζ͕େ͖ͯ͘΋҆৺
    w HFU-JOFTͯ͠ίϨΫγϣϯϝιουͰΩϝΔͳͲ

    View full-size slide

  13. #

    %FNPOTUSBUJPO5JNF
    εΫϨΠϐϯάΛ΍ͬͯΈΔ

    View full-size slide

  14. 4USJOH*OUFSQPMBUJPO
    w จࣈྻϦςϥϧͷલʹTΛ෇͚Δͱɺม਺Λల։Ͱ͖Δ
    val price = 1000
    s"Price is $price"
    // => Price is 1000

    View full-size slide

  15. 5SJQMF2VPUF
    "Hello triple quote!\nHello stripMargin!"
    """Hello triple quote!
    Hello stripMargin!"""
    """|Hello triple quote!
    |Hello stripMargin!""".stripMargin
    w վߦΛؚΉจࣈྻΛຒΊࠐΉʹ͸ɺΛ࢖͏
    w ΠϯσϯτΛଗ͑Δʹ͸Πϯσϯτจࣈ c
    ͱTUSJQ.BSHJOΛ࢖͏

    View full-size slide

  16. 4USJOH*OUFSQPMBUJPOͱ5SJQMF2VPUF
    val response =
    s"""HTTP/1.1 ${status.value}
    |Date: ${rfc1123Formatter.format(now)}
    |Server: SimpleScalaHttpServer
    |Content-Type: $contentType
    |Content-Length: ${body.length.toString}
    |Connection: Close
    |
    |""".stripMargin
    ͪΐͬͱͨ͠ςϯϓϨʔτΤϯδϯΈ͍ͨʹ࢖͏͜ͱ΋

    View full-size slide


  17. $-*πʔϧʹͯ͠͸

    ΋ͬ͞Γ͗͢͠͡Όͳ͍ʁ
    ύΠϓͱ͔ͰΨϯΨϯͭͳ͍ͰαΫαΫಈ͔͍ͨ͠ΜͰ͚͢Ͳɻ

    View full-size slide


  18. $-*πʔϧʹͯ͠͸

    ΋ͬ͞Γ͗͢͠͡Όͳ͍ʁ
    ύΠϓͱ͔ͰΨϯΨϯͭͳ͍ͰαΫαΫಈ͔͍ͨ͠ΜͰ͚͢Ͳɻ

    ࣮ߦՄೳͳόΠφϦΛੜ੒͠

    αΫαΫಈ͔͢͜ͱ΋Ͱ͖·͢
    4DBMB/BUJWF΍(SBBM7.ͷ4VCTUSBJU7.Λ࢖͍ɺ

    (PͷΑ͏ʹ࣮ߦՄೳͳόΠφϦΛੜ੒͢Δ͜ͱ΋ՄೳͰ͢ɻ
    +7.ͷىಈΛ଴ͨͣʹαΫαΫಈ͔͢͜ͱ͕Ͱ͖·͢ʂ

    View full-size slide

  19. IUUQXXXTDBMBOBUJWFPSHFOW

    View full-size slide

  20. #

    %FNPOTUSBUJPO5JNF
    .BSLEPXOࣼΊಡΈΛ4DBMB/BUJWFͰߴ଎Խ

    View full-size slide

  21. ·ͱΊ
    w 4DBMBΛ࢓ࣄͰ࢖͓͏ʂ
    w ·ͣ͸ݸਓͷۀ຿Ͱ׬݁͢ΔൣғͰ΍ͬͯΈΑ͏ʂ
    w 4DBMB͸εέʔϥϒϧͳݴޠʂ
    w େ͖͍ϓϩμΫτ΋ɺখ͞ͳεΫϦϓτ΋͍͍ײ͡ʂ
    w εΫϦϓςΟϯάʹศརͳػೳΛ஌͓ͬͯ͜͏ʂ
    w TDBMBJP4PVSDF
    w 4USJOH*OUFSQPMBUJPO
    w 5SJQMF2VPUF
    w ࣮ߦՄೳͳόΠφϦΛੜ੒͢Δ͜ͱ΋Ͱ͖ΔΑʂ
    w αΫαΫಈ͍ͯɺͦͷ··νʔϜʹڞ༗΋Ͱ͖Δʂ
    w མͱ݀͠΋͋Δ͔ΒؾΛ͚ͭΑ͏ʂ

    View full-size slide


  22. 5IBOLZPV
    Some(question) match {
    case Twitter => "@todokr"
    case GitHub => "@todokr"
    case Facebook => "shunsuke.tadokoro"
    case _ => invokeDynamic
    }

    View full-size slide