エンタープライズ Scala

223977120819323905c8e4e55a2365ed?s=47 Yohei TSUJI
November 10, 2018

エンタープライズ Scala

「2018/11/10 Scala関西Summit 2018」でお話したエンタープライズScalaの発表スライドです。

223977120819323905c8e4e55a2365ed?s=128

Yohei TSUJI

November 10, 2018
Tweet

Transcript

  1. 2.

     • #&"%$"  • Java 17Scala 3 • !%

      $&  Yohei TSUJI @crossroad0201 2
  2. 3.

    E8IC • 5D[]WbeE)ScalaDQ!6O?.N)ScalaQ. I;Q %C + @ScalaQ/CE)5D-;MQ0?14F.D@E, A /aSe^GAI;TIPS(@9* •

    Ue\f`cSZD `dXTV^@J +Scala=?HL/2B, A8?.; <4OF.@9* • ScalaD #KAPIC>.?C$7?.NP4@E-MG:R* • YcS_D CE)P2MK96Q7?'@EB."&3-MG9* 3
  3. 4.

      1. # !" 2.  3.  4.

    case class #" 5.  6. # 7. Java 8.  4
  4. 6.

    $ val  ) ) .* .* 0) * .

    .* 1 // % ) .* ) .* 1* ()8 0) * . .* 1 • * val  !) !"()-,*% • & var *)$ #/0+. " %' ! 6
  5. 7.

    )67=159.:+/8<( 16 65=> - 2 ( 2 ( 16 65=>

    - 65=> ," ) 2 ( ) 16 65=>( - 65=> "" 2 + 2 ( ) + 16 1: - /1: 2 > >4 22 ( /1: 2 > >4 22 ( 16 1: - 1: " 3 ) /1: 2 > >4 22 ( 3 ) 16 1:( - 1: " 35 2 =5 + /1: 2 > >4 22 ( 3 ) 35 2 =5 + • @)67=159.:+/8<(  %' 3,(" • 67=159.:+/8<>scala.collection.mutable42-=0?#&" !  ;=*9 "$ 7
  6. 8.

      (. 4 34 , 4 4 , 34

    " 1 - 1 4 , 2 $ ($ )" 4 4( , = + " = / ($ )$ " 4 4) , 4 , 4 ( , < ( G < • Scala' if + for-yieldmatch %$'  ( #-'.) • var #! &!-56;248%09/17:&." ',%0;3.  &*) 8
  7. 9.

    8A79@B( ) ,2 >L>= L.L4: L( L) L L+ L

    L.L L @L- >= L L .L@ =@ >@ LL L + L L.L>= > 2: L :L L@ L./L L L@ LL L + 0 => : : L > : ,L2= 51@ L ,L1@ L.L ,L1@ L.L > : L <L :L3=>L./L :LI,,I L./L I L L LI >L (L.L >= 2= LL L + • 8A79@B'0 ) #map() , #fold()0)2* • %'64B=3;?!/0.& + var 21 ' * • 2 ) '&0.'*- • @tailrec 3><C9@B20%$ '&"#0%2:5;7$* ! 9
  8. 10.
  9. 11.

     ),$2 0 D 5I 0 ( 3 I I

    0 5 D I D I 5 5I 8 0 L ( 0 1 2 : B ( ) 0 1 2 : B ) ( " ) 1 2 : B = 2 I 6 B / 5I / 4D: 2 I 6 B 0 L 4D: 2 I 6 B I 0 2 I 6 B ) (. (( ( 6()/ +/,- 4D: 2 I 6 B • ) ( ),(!.$ • %&# + 1,#)-  +*,#($ •  + 13 #%-  +13'"0$6745/1.$ 11
  10. 12.

      ==  ( " / ) ( .

    / ( / ) ( . / • Scala !#"     •   #equals()  ==     ( ( . / ! 12
  11. 13.

    -)/*)0(, &$ 8 . ) . ( 8 8 .

    *+: ( 8 8 . *+:( .) . ( 8 . + *+: . ) . *+: +80 . . 8 ( 8 = . + *+:( .) . . ) . *+:( .) . . + *+: . = • *)0(,23&$" • *)0(,&$+1.  &$" •  $%!#"' 13
  12. 14.

    ACF0&- 1 = > , 1> ) / 31 .1

    =1> ) ( $(($( "" =>8 = > 0(" 1> =>8 = > 0)" ) =>8 = > 0 " ) ( $(($( 1 1 1 0" , = > = > 1 3 31 1 0 28> 1 " ,- 1 , 1 28> 1 , 28> 1 D • (1, 2, … 22) +<E?:7-$*5/:1,3,4. 3' • =G<>DH,-9 /:)9%,"+#3' • ._1 6 ._2 + .;<@?+#92!val 6 case +/: 8(3' •  +'"/.&=IB/ 5"9/+0+' 14
  13. 15.

     2$6-71 •  2$6- 78* .53.&5 *& • 45.%)"0&

    • >9<&5#+2 *&@(+/Try[Option[String]] -,A • 8*57 4&?:;= #get() !40&!0'8    Option[ ] - 2$6- Some() None Try[ ]  2$6- Success() Failure(Throwable) Either[ ,  ]  2$6- Right() Left() 15
  14. 16.

    implicit class , 0@NB: (8 . = = A :

    = )= (: : = $ )= " = A : = = : = " // "Hello World" • implicit class :!%-, 0@NB:(8%-.$MCEH:,#3( SEnrich my libraryIDRQ-293(T • ;HKE@.0)40MCEH:'+ $%-,@NB"(8%-:,#3 ( • 67 .1DDD,>QF<F<0:PRO?JA=@G,BLRG/ ! https://qiita.com/crossroad0201/items/bd1c66819e47e95a8754 5$*&  16
  15. 17.
  16. 18.

    !9<;5(6:3=72 F FF B ECD EG? F( FF C= GE?B="

    AC= GE?B=" CC GE?B= ) " E GE?B= ) B ECD EG? F( FF C= , " AC= , " CC : A B E A G EF HB G?CB C= GE?B=" AC= GE?B=" CC GE?B= ) " E GE?B= ) A B E A G EF HB G?CB C= , " AC= , " CC • 2 .$(!+9<;5 =9<;5 ,'9<;52? '@'!/$ +9<;5,  *)+4>8, /0/$ • 9<;5+-6:3=72&%1"(' .#'2$1"( '!/$ 18
  17. 19.

    ' ".*,% 10 1/0 1 1 1 ( = (

    = ) 10 1/0 =0 .0= ( * + 1 80 ) 1 =0 .0= =: = B 1 10 1/0 B 0 0 ) 10 1/0 1 1 • ! (.*, # • 3& " private (/1+,  %!" .*, %!*)20()2-!  $# 19
  18. 20.

    -36>@'4 >:B * / A * / A * /

    A + : + >:B ( : >:B + >:B 4 / A +, / A : + >:B ( :==>2 .3 :B * * +, . * 1 2.3 + :B > : + :==> ( :B +, :B A ( ) • .0/;=<8>7:5)(4%,"+#1' A6>@B • *-2.  Haskell Curry / .'43&! •  / ,  />9=? 5$45 &)!,# • implicit parameter +;=<85.&)!,# AB 20
  19. 21.

    163'JRNF6 implicit parameter 1!4- 2 ?0 = >? ) >?

    " 1 ) - 1 C 1 =20 .0= 0: = >? 0: " 0 = >? 0 0 1 ) - 1 ,,, ?0 >? 0> " " 1 ?0 >? 0 08 " ( " 1 2 ?0 = >? ) >? := 1 1 ) - 1 C 1 =20 .0= 0: = >? 0: " 0 = >? 0 := 1 0 1 ) - 1 ,,, ?0 >? 0> " ?0 >? 0 08 " ( • implicit parameter =(+21"JRNF=!4,0 =7-+2)1*8-# • UB?WPXDX 9DBCIAEQW5:(3"%5 163') 4 * -& 6"implicit parameter 1-:(4,8-# • ./,61-# 6$5)4(:;">X@HAGO9KTXMVX ATLS1<+2) '2'8-# ! 21
  20. 22.

    (178- • # %$"% implicit parameter !& 052,' ! 

    Scala$PureFunctional6)48/(8*.+-3 https://www.fringeneer-blog.com/entry/2018/11/01/100531 22
  21. 23.

    3A:A?A9.0 4>62(1/2) > >: > . >: A > F

    . A := >: > . => " >: A . : A )= . := >: > >: > . : A : : A A ( => F F • ;>=7'# *1 2 &1,3A:A?A9.04>6;7A@2 &1$)2 %-& • 5<A8&1;>=72 )/+&1$) (!  "*0-& 23
  22. 24.

         (2/2) FM [5ETL 9 SOE

    ST[ LO NL L [ EN[LP 9 SOE S1[9 SOE SB:P C[2 EN 1[:P "[23[ EN SLP LO NL L [ EN[N EN7E ALO 9 SOE S1[9 SOE SB EN7E ALO C[2 EN 1[ EN7E ALO "[23[7E ALO 9 SOE S : >D >64 D74A8DA: 8 I SOE EN " LO S [5ETL 9 SOE ST D T ("[[ [ ( T EN7E ALO P ""[[ [ ) (/$(($( A()1 1,- ./0 EN[T O :P 1[> L PB:P C 2[ O (" T T O :P "[[ [ ( EN[P P :P 1[> L PB:P C[2[= P T P P :P "[[ [ ET [ NETT[ ST P PEO 1[ SLP [E 1[:P " LO NL L [ EN[ ST P9 SOE S1[9 SOE SB ST PC[2[ EN 1[ ST P"[23[T PEO 2 EN PEO [E 2 EN E T ST P ES [) "" // "name=taro, age=20" 24
  23. 26.

    case class 0/ 2 0 / 2 / 2) .=

    / 2) , = " / =/ 2 =/ ( " : = =/ $ / 2" =/ : = =/ $/ 2" ( : = =/ " 2 =/ ( " : = =/ 2 =/ ( "" = >2 =/ /=0 0/ 2 2 / 2 / 2" : = / 2 / 2 2/ 1$ " =/ ( 2/ 1$ / =/ ( =/ $0 : / 2 ( " : = =/ ( $/ 2" ( • R[OJ@( 6 val 65=72# AUW]GQY5K]G ? 62):.$ • R[OJ@? .=7285*#  6!,>= #copy() VFIM2 -%?/A\EG\E?-*-:.$ • K]G 8 case class 2# B\L[]X];C]PE548 "7 class# 7<&5%+65=3%:.$ • equals()#hashCode()#toString() ( 6  ,>=72SAX]RZ]L?2):.$ • .917R[OJ@6 0%17# NIDWB]M(,>:.$ • OG]\TIH2R[OJ@6<=(  6':.$^_ 26
  24. 27.

    +&0/-(' ) A A"A > ) A A"A A )

    A ) A > A" A ) A AA" A ) ) A(AAAAA" A A A ) = A ) A // true • +&0/-('! !   • "+&0/-(' •   -('$0%1),#.*2 '_ => '  27
  25. 28.

    -)21/+*'% > 1 A ) 2 = " ) .

    > A 1 A A " > = 8 > A < 1 A " =: ( , , : > I > A ) 0 =A 2 = /A > = A < 2A , A. /A , I • case class ! !.0-,('%$ • ! .0-,(" '_'  $ • Option / Try / Either   ' '%$ • !#List !%%&&$ 28
  26. 29.

    sealed 2/65 ') . . : .: . . .

    .: . A . . . . .: . . . > . . .: . = " .: . .: . = .: A . .: . . . .: . > : / . • sealed 7.3' "!)&8! %(&! 2/65# '"!)-52*4 #0+1, $ • 2/65# '&!-52*4 # match may not be exhaustive.$ 29
  27. 30.
  28. 31.

    532 • 532MQ62\nvsgMDK @ Y27 • JavaL,<MA[Pvrh7 • Scala "API6a{qzf{ev_pvwLV&N532?$CZK;YPL6

    532\;ANFAM?4O1*LF7 • + E2LV6yd{m{POEKdclit?%NYPL!?*LF7 • =R #fold() 2Q6*'\HHT.]L POFY2LF?6 E8HHT.U9LVGPOWIKHHT.T?0;SF7 • OptionP #fold() Q6?8:Y9 QX>P#\)IK/D6 ?8N;9 QCZHjo`xk\/DSF7 • SeqP #fold() Q6=XZH\J^bury{g{ODK6 *'BMP#(\-DA]L/DSF7 31
  29. 32.

    flatten() / /. (. , , , , / /.

    /. (. / ,1 (. / 23 (. , 2 , )21, 2 , / /. 23 (. / ,1 (. •    • "!#… • None Some … 32
  30. 33.

    map() 3 => / 3 344 2 => 3 +

    / ( 3 344 2 => 3 2 / ( 3 : > ) . > : , >1 /: 3 : > 3 2 /: 3 : > ) . > : , >1 : 3 : > 3 2 : • -!)#  ($ ,& • /2.013!%$#   ,& • Option / Try / Either !%'*" # ,& $ % %+*$ $&&*& 33
  31. 34.

    flatMap() O O O CB O C " O >

    O O < C.> , L"OO O/ C O B O O O O C " O > 'O O A C.> , L" < BOO O/ C O B O O O O C " : <O C-CC B (O B="(O2 CB -CC B O OM OA >OM O L O O CA " O BC OO O CA < " O OOOOO O CB N N OC (O2 CB B= O O CA >C= " OC 'O OC < O O C-CC B ""O O CB • ,/+-.0"#map()  #flatten() (! • Option / Try / Either "%) %)  *$ • #flatMap() * &'for  !# %… 34 OC 'O O<C OM O) C O) C-CC B " NOL :O
  32. 35.

    fold() (1/2) I I,I1 I( I) I =I,I I 6

    I 66 I> I, I 66I"I> II I I = +I0 >21 >:3I,I1 = I I,I = .> II I I> > +I0 >21 >:3I,I/ > I (I,I> > .> II I •  (&&% • '2730*1! ## ( #fold()  '  • -8,.69 / Option $)+548:/:"  # (&% 35
  33. 36.

    fold() (2/2) • Try / Either    I

    S EL+S EL E > S,S/ E S02. : .3323 I S )S,S EL = S S/ E O R : F S +S02. : S,-S : F S SSSSSSSSSSSSSS,-S ( 0 SS S :: FF O R SS S I S E+S. E E > S0 S,S1 = .3323 I S S,S E = SSSSS,-S S1 = O R E > S,-SE > S"S(SS S :: FF O R SS S 36
  34. 37.

    foldLeft() / foldRight() 0 L =>L+L/3 L( L) 0 L=

    L+L => 2. 6> L 10=3L 011 L L+, L L )L ,L(L ,L L 011L L LL L 0 L =>L+L/3 L( L) 0 L= (L+L => 2-3 > L 10=3L L011 L+, L L L ,L(L ,L)L 011L L LL L • &)%'(*$  •  #fold() ! !#! "#! "  • &)%'(* #fold() #foldLeft()   37
  35. 38.
  36. 39.

    ,24$Either ? : ?? A A . " = ??C

    . , ??C C, ??C . " : , ??C . B : , ??C " A ? : ?? B : , ??C ? (A? ?? B B : ) B : , ??C A = ? E A . " A ? B A A " A • ,24$+.34*#$! )  Either # Left +& • Left   $! String #,2410/4-+(  !,24 +'"$ ",24+*% 39
  37. 40.

    &/;?.- D = EC E CC C = DE C

    CC .E C = ?E D = DD . DE CC C FD :C = E ? D CC C D = DD = E ? CC C E =D . B CC C E = E ? D CC C D = DD CC C E = AC A CE .EC ? " C D ? .EC ? EC E (FD ? DD CC C E ? D CC C = CC C) .EC ? = C D . B ? . B AE D = DD E E, F? CC C ? .EC ? " .EC ? E ? D (FD ? DD CC C CC = CC C) CC C E E, F? CC = C D . B ? " • case class #/;?.) • */;?#93?>:54#-&#/;?8>7<>1 *$,) • /;?&'/;?8>7<>1&93?>%" sealed #,) • /;?%'23506=?2. ! " . #-+%!(# 40
  38. 41.

    >JL")!95for 0/2& <A . ( E : <A , =<

    < <A , =< < C< <A - C C< - <: C< - ) ) < <A E < E <A <A <A , =< < <A = C< ( <A <A <A C< C C< <: C< <A <A , =< < = C< ( <A : <A • for 5 HCF)- #flatMap() 1 #map() 4BKDE@CBI?L0* • >JL328!9M=Either<*N</2$ ;+ .')- 1#3 6* 3291,:45 (:6+= • Scala0ALG<%1#4 0840786*! 41
  39. 42.

    #& %$%Either!&%" ? B A B A E E )))

    B A A EE B B * @C? B@ """"" """"" @ L P E = B A ( C =A ?A B A R P E / : , E=A EE. B ( C =A ?A R P E / : ?= =BA. B ( C =A ?A =?E @> =A STUVR P E / : E @. B ( E C =A > • #& %$%Either!&%" • #& sealed  #& %$%  42
  40. 44.

    Java '6G<(!. • Scala6G<'&Java6G<-)%,&Java& !.6G<()! • null • 6E47BF •

     • JavaC2>CD • ' JavaC2>CD 0!.#/!.'" 1?D5G7BF0E2AJavaC2>CD'( 'E2AH2F=C8;C4:@E 2A+2F9G=3G8E2A%$I& *.,&)! 44
  41. 45.

    null  Option =15 55/15 2) . :4 3 (

    =15 4 55/15 2 " 1 " . 2 ( =15 55/15 2) . :4 3 55 =15 4 55/15 2 " 1 " 2 • null Option( )  NULL   45
  42. 46.

    Scala%19027:$, : : :5 I. : :%JI %5 I 3

    I8 / : :%JI %1 :L %: 5 I ( ) * ," : : / : :5 I% I :C " %= I /0 ) // " %C: /0 )" I : :%JI %1 :L %I I > :%I 1 :L"" C I : :% I %4: :2 I % : :) / : :5 I%: : : %= I ) // " %C: )" I :)%CA I > "" • Java%19027:<List)Map#"=&JavaConverters#asScala . Scala%/56;34819027:$* ' •  $Scala19027:$-, JavaConversions !API(+'  &!#+ JavaConvertes#asScala $,!-'  46
  43. 47.

     )Try$3$Either' 0 (1/3) A > A ,> F >

    > A E AA > ) A > : A AA A > /A TL RJ OS O I = : A : A ( A = AA A I I I •  0Java7?9) Try{ } $+* Try{ } $,% Success( ) *!) Failure( ) ( 1'&/* • .' Try 2 Either ' 0%$4:=6?8;>$5<?2 Either $0- '*#&% for $ ".$ 47
  44. 48.

    Try Either (2/3) > F C F L = C

    = =F EE ,CE ( C = F ( ( JF A E A L : F ( ( , ( : < ( , (=F< ( , ( L ( F < L E EE E = <F E E ) = :F : : F EF ( (=F< EF ( ( F ( EF ( • Try -> Either $!%'#&'"     48
  45. 49.

     Try Either (3/3) A :A_ = A = :A=

    E ) _ A _ = A = :A= E_ : = A_) , = A = : _ _ A = A( _,A : _ :A . = _ = A/_ _ E_ _) T ST JR O [JI ] _A=,E A = 49
  46. 50.
  47. 51.

    H@=?><?,ScalaTest6 > DFi D F: F L > :A =:A

    =:A"i A > :"i. F =:D "i D : M i GAF : >F DK A.: DK : : F:A i D : iI>F=i > :A =:A =:A I>F=i. F =:D iI>F=i A > :iL :F iI=:AiL (U fS ebRTOda i = G iL NUT i>AiA:Ii >F=)> FGD:iL > :A ebRTO =:A cgS i FG i i GF :F , =:A N h W >A > : FG iL :i : F : : i i : :i = G i : GAFiA Fi GA M M M • ScalaTest: http://www.scalatest.org • Scala&, /CAGIM) ><>7L:BJMDKM9 • )><?+   ;CM? 4%-! NWordSpecFreeSpec8<<EO • GivenWhenThen &><? 6 !3'520! • ScalaMock: https://scalamock.org )('.5"%F=9><?/ • should be xxxx.#)DSL + *,$1! 51
  48. 52.

    Scalaz : Cats 45 >/"; Scala0GMCJHNC@ *=,84JAFJK#?>='1#<7*# GMCJHNC3 >=705 0)2

    6!# 1 7* 7+5Scala4JAFJK0 (>/ = DLBEINOptionTryEither @ !-&090* '>;@)/<2%2./$,; Scalaz : Cats 4@*=1 1 7 * • Scalaz: https://github.com/scalaz/scalaz • Cats: https://github.com/typelevel/cats 52
  49. 53.

    IDE.IntelliJ IDEA3 • IntelliJ IDEA: https://www.jetbrains.com/idea/ • JetBrains - Scala

    Plugin )% https://plugins.jetbrains.com/plugin/1347-scala • +7J? • !2+8I:;59A4F4> • BH?<JH sbt *- F4CFG- • E@;>=9>- • Scala-),.- Community Edition )0 )% • &-%%/DF64I.#(10 "'$ IntelliJ IDEA%%/DF64I: https://qiita.com/crossroad0201/items/bf5528cc57cadab67638 53
  50. 54.

    $ !$"$ • $ !$"$ $  • Scalafmt: https://github.com/scalameta/scalafmt

     # • Scalariform: https://github.com/scala-ide/scalariform •  scalafmt : https://speakerdeck.com/norinori3/scalafmtnituitediao-hetemita : ---- ---- --- ---- ---- > : ---- ---- --- ---- ---- > < ) < : < ( < = :: : : < ( < >>>>>> :: : : < < ) < : < ( < = :: : : < ( < >>>>>> :: : : < 54
  51. 55.

    -2 • The Scala Programming Language: https://www.scala-lang.org/ • %12Scala "!$:

    https://dwango.github.io/scala_text/ • Effective Scala: http://twitter.github.io/effectivescala/index-ja.html • Scala*+: http://www.ne.jp/asahi/hishidama/home/tech/scala/ •  Scala!3,'.(0,)2 3: http://amzn.asia/d/f56DXon •   Scala: http://amzn.asia/d/1TQ38iG •  Scala / &: http://amzn.asia/d/7igHuwm •  Scala#2&(0,)2: http://amzn.asia/d/hgUMwzq 55