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
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
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
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
> 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
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
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
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
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
. , ??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
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
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
> 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
= =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
=: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