a > v a l h o g e = S o m e ( " h e l l o " ) h o g e : S o m e [ S t r i n g ] = S o m e ( h e l l o ) s c a l a > h o g e . m a p ( _ + + " , w o r l d " ) r e s 0 : O p t i o n [ S t r i n g ] = S o m e ( h e l l o , w o r l d )
a > v a l f u g u : O p t i o n [ S t r i n g ] = N o n e f u g u : O p t i o n [ S t r i n g ] = N o n e s c a l a > f u g u . m a p ( _ + + " , w o r l d " ) r e s 1 : O p t i o n [ S t r i n g ] = N o n e
h o g e = S o m e ( 1 0 ) h o g e : S o m e [ I n t ] = S o m e ( 1 0 ) s c a l a > v a l f u g u = S o m e ( 2 ) f u g u : S o m e [ I n t ] = S o m e ( 2 ) s c a l a > f o r { | a < ‐ h o g e | b < ‐ f u g u | } y i e l d a / b r e s 2 : O p t i o n [ I n t ] = S o m e ( 5 )
a > v a l h o g e = S o m e ( 1 0 ) h o g e : S o m e [ I n t ] = S o m e ( 1 0 ) s c a l a > v a l f u g u : O p t i o n [ I n t ] = N o n e f u g u : O p t i o n [ I n t ] = N o n e s c a l a > f o r { | a < ‐ h o g e | b < ‐ f u g u | } y i e l d a / b r e s 3 : O p t i o n [ I n t ] = N o n e
need some goodies: s c a l a z i m p o r t s c a l a z . _ , S c a l a z . _ S t r i n g \ / I n t is read as: this thing can be a S t r i n g or an I n t . One or the other, not both. S t r i n g in this case is the l e f t value and I n t is the r i g h t value.
h o g e : S t r i n g \ / I n t = 1 . r i g h t h o g e : s c a l a z . \ / [ S t r i n g , I n t ] = \ / ‐ ( 1 ) s c a l a > v a l f u g u : S t r i n g \ / I n t = " s o m e t h i n g e l s e " . l e f t f u g u : s c a l a z . \ / [ S t r i n g , I n t ] = ‐ \ / ( s o m e t h i n g e l s e )
h o g e = 1 0 . r i g h t [ S t r i n g ] h o g e : s c a l a z . \ / [ S t r i n g , I n t ] = \ / ‐ ( 1 0 ) s c a l a > v a l f u g u = 2 . r i g h t [ S t r i n g ] f u g u : s c a l a z . \ / [ S t r i n g , I n t ] = \ / ‐ ( 2 ) s c a l a > f o r { | a < ‐ h o g e | b < ‐ f u g u | } y i e l d a / b r e s 4 : s c a l a z . \ / [ S t r i n g , I n t ] = \ / ‐ ( 5 )
h o g e = 1 0 . r i g h t [ S t r i n g ] h o g e : s c a l a z . \ / [ S t r i n g , I n t ] = \ / ‐ ( 1 0 ) s c a l a > v a l f u g u = " s o m e t h i n g e l s e " . l e f t [ I n t ] f u g u : s c a l a z . \ / [ S t r i n g , I n t ] = ‐ \ / ( s o m e t h i n g e l s e ) s c a l a > f o r { | a < ‐ h o g e | b < ‐ f u g u | } y i e l d a / b r e s 5 : s c a l a z . \ / [ S t r i n g , I n t ] = ‐ \ / ( s o m e t h i n g e l s e )
c a l a > v a l h o g e = 1 0 . s o m e h o g e : O p t i o n [ I n t ] = S o m e ( 1 0 ) s c a l a > h o g e \ / > " w h a t i s t h i s ? " r e s 6 : s c a l a z . \ / [ S t r i n g , I n t ] = \ / ‐ ( 1 0 ) s c a l a > v a l f u g u = n o n e [ I n t ] f u g u : O p t i o n [ I n t ] = N o n e s c a l a > f u g u \ / > " w h a t i s t h i s ? " r e s 7 : s c a l a z . \ / [ S t r i n g , I n t ] = ‐ \ / ( w h a t i s t h i s ? )
Both O p t i o n and ∨ are short circuiting. They don't accumulate. Let's say we want to validate S h o p s . s c a l a > c a s e c l a s s S h o p ( r a t i n g : I n t , i t e m s : L i s t [ S t r i n g ] ) d e f i n e d c l a s s S h o p
d e f h a s H i g h R a t i n g ( s h o p : S h o p ) = { i f ( s h o p . r a t i n g > = 4 ) s h o p . r i g h t e l s e " l o w r a t i n g s h o p " . l e f t } d e f h a s E n o u g h I t e m s ( s h o p : S h o p ) = { i f ( s h o p . i t e m s . s i z e > 1 ) s h o p . r i g h t e l s e " n o t e n o u g h i t e m s " . l e f t }
v a l b a d = S h o p ( 3 , L i s t . e m p t y [ S t r i n g ] ) d e f v a l i d a t e ( s h o p : S h o p ) = f o r { _ < ‐ h a s H i g h R a t i n g ( s h o p ) _ < ‐ h a s E n o u g h I t e m s ( s h o p ) } y i e l d s h o p v a l r e s u l t = v a l i d a t e ( b a d ) s c a l a > r e s u l t r e s 1 2 : s c a l a z . \ / [ S t r i n g , S h o p ] = ‐ \ / ( l o w r a t i n g s h o p )
S t r i n g \ / S h o p is not enough We need L i s t [ S t r i n g ] \ / S h o p to accumulate errors In fact, N o n E m p t y L i s t [ S t r i n g ] \ / S h o p is what we need. We can turn S t r i n g \ / S h o p into V a l i d a t i o n [ N o n E m p t y L i s t [ S t r i n g ] , S h o p ] by using . v a l i d a t i o n N e l Finally we can chain validations using | @ | function
d e f v a l i d a t e A c c ( s h o p : S h o p ) = ( h a s H i g h R a t i n g ( s h o p ) . v a l i d a t i o n N e l | @ | h a s E n o u g h I t e m s ( s h o p ) . v a l i d a t i o n N e l ) { ( _ , _ ) = > s h o p } v a l r e s u l t = v a l i d a t e A c c ( b a d ) s c a l a > p r i n t l n ( r e s u l t ) F a i l u r e ( N o n E m p t y [ l o w r a t i n g s h o p , n o t e n o u g h i t e m s ] )