Slide 1

Slide 1 text

SCALA 2018-11-10

Slide 2

Slide 2 text

• #&"%$" • Java 17Scala 3 • !% $& Yohei TSUJI @crossroad0201 2

Slide 3

Slide 3 text

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_DCE)P2MK96Q7?'@EB."&3-MG9* 3

Slide 4

Slide 4 text

1. # !" 2. 3. 4. case class #" 5. 6. # 7. Java 8. 4

Slide 5

Slide 5 text

5

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

)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

Slide 8

Slide 8 text

(. 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

Slide 9

Slide 9 text

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./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>

Slide 10

Slide 10 text

10

Slide 11

Slide 11 text

),$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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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) +DH,-9 /:)9%,"+#3' • ._1 6 ._2 +.;<@?+#92!val 6 case +/: 8(3' • +'"/.&=IB/5"9/+0+' 14

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

implicit class , 0@NB: (8 . = = A : = )= (: : = $ )= " = A : = = : = " // "Hello World" • implicit class :!%-, 0@NB:(8%-.$MCEH:,#3( SEnrich my libraryIDRQ-293(T • ;[email protected])40MCEH:'+ $%-,@NB"(8%-:,#3 ( • 67 .1DDD,>QF

Slide 17

Slide 17 text

17

Slide 18

Slide 18 text

!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

Slide 19

Slide 19 text

' ".*,% 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

Slide 20

Slide 20 text

-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

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

(178- • # %$"% implicit parameter !& 052,'! Scala$PureFunctional6)48/(8*.+-3 https://www.fringeneer-blog.com/entry/2018/11/01/100531 22

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

(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

Slide 25

Slide 25 text

case class 25

Slide 26

Slide 26 text

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]G8 case class 2# B\L[]X];C]PE548 "7 class# 7<&5%+65=3%:.$ • equals()#hashCode()#toString() ( 6 ,>=72SAX]RZ]L?2):.$ • .917R[OJ@60%17# NIDWB]M(,>:.$ • OG]\TIH2R[OJ@6<=( 6':.$^_ 26

Slide 27

Slide 27 text

+&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

Slide 28

Slide 28 text

-)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

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

30

Slide 31

Slide 31 text

532 • 532MQ62\nvsgMDK@ Y27 • JavaL,P#\)IK/D6 ?8N;9QCZHjo`xk\/DSF7 • SeqP #fold() Q6=XZH\J^bury{g{ODK6 *'BMP#(\-DA]L/DSF7 31

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

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 " : C= " OC 'O OC < O O C-CC B ""O O CB • ,/+-.0"#map() #flatten() (! • Option / Try / Either "%) %) *$ • #flatMap() *&'for !# %… 34 OC 'O O

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

38

Slide 39

Slide 39 text

,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

Slide 40

Slide 40 text

&/;?.- 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

Slide 42

Slide 42 text

#& %$%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

Slide 43

Slide 43 text

Java 43

Slide 44

Slide 44 text

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

Slide 45

Slide 45 text

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

Slide 46

Slide 46 text

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:

Slide 47

Slide 47 text

)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;>$52 Either $0- '*#&% for $".$ 47

Slide 48

Slide 48 text

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 = Either $!%'#&'" 48

Slide 49

Slide 49 text

TryEither (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

Slide 50

Slide 50 text

50

Slide 51

Slide 51 text

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< 6!3'520! • ScalaMock: https://scalamock.org )('.5"%F=9>/ • should be xxxx.#)DSL + *,$1! 51

Slide 52

Slide 52 text

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

Slide 53

Slide 53 text

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?=9>- • Scala-),.- Community Edition )0 )% • &-%%/DF64I.#(10 "'$ IntelliJ IDEA%%/DF64I: https://qiita.com/crossroad0201/items/bf5528cc57cadab67638 53

Slide 54

Slide 54 text

$ !$"$ • $ !$"$ $ • Scalafmt: https://github.com/scalameta/scalafmt # • Scalariform: https://github.com/scala-ide/scalariform • scalafmt: https://speakerdeck.com/norinori3/scalafmtnituitediao-hetemita : ---- ---- --- ---- ---- > : ---- ---- --- ---- ---- > < ) < : < ( < = :: : : < ( < >>>>>> :: : : < < ) < : < ( < = :: : : < ( < >>>>>> :: : : < 54

Slide 55

Slide 55 text

-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

Slide 56

Slide 56 text

! HAVE A GOOD SCALA LIFE !! 56