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

Labmdas in Java 8

Labmdas in Java 8

Любомир Вовк software engineer at SoftServe

Labmdas in Java 8
В цій короткій презентації відбудеться спроба пояснити що таке лямбди чому вони такі важливі (або не важливі) і як вони "працюють" в джаві.
Матеріал буде цікавий Java розробникам, які ще не розібрались з лямбдами в Java 8 .

Grygoriy Mykhalyuno

May 17, 2014
Tweet

More Decks by Grygoriy Mykhalyuno

Other Decks in Programming

Transcript

  1. WHAT IS LAMBDAS IN JAVA 8 not only closures not

    only syntactic sugar It's a Functions!
  2. EX #1: FUNCTIONAL SET every set could be presented as

    a predicate Set of integers greater than 0 can be expressed as x > 0
  3. EX #1: FUNCTIONAL SET i n t e r f

    a c e S e t e x t e n d s P r e d i c a t e < I n t e g e r >
  4. EX #1: FUNCTIONAL SET i n t e r f

    a c e S e t e x t e n d s P r e d i c a t e < I n t e g e r > b o o l e a n c o n t a i n s ( S e t s , i n t i ) { r e t u r n s . t e s t ( i ) ; }
  5. EX #1: FUNCTIONAL SET i n t e r f

    a c e S e t e x t e n d s P r e d i c a t e < I n t e g e r > b o o l e a n c o n t a i n s ( S e t s , i n t i ) { r e t u r n s . t e s t ( i ) ; } S e t u n i o n ( S e t s , S e t t ) { r e t u r n x - > c o n t a i n s ( s , x ) | | c o n t a i n s ( t , x ) ; } S e t i n t e r s e c t ( S e t s , S e t t ) { r e t u r n x - > c o n t a i n s ( s , x ) & & c o n t a i n s ( t , x ) ; } S e t d i f f ( S e t s , S e t t ) { r e t u r n x - > c o n t a i n s ( s , x ) & & ! c o n t a i n s ( t , x ) ; }
  6. EX #1: FUNCTIONAL SET S e t p o s

    i t i v e = x - > x > 0 ;
  7. THEORY Every function has a type: x -> y Function

    that returns function: x -> y -> z java.util.function.Function<T, R>: t -> r java.util.function.Predicate<T>: t -> Boolean java.util.function.Consumer<T>: t -> () java.util.function.Suplier<T>: () -> t
  8. EX #2: ABSTRACTION F i l e O u t

    p u t S t r e a m f i l e = n e w F i l e O u t p u t S t r e a m ( " f i l e . t x t " ) ; t r y { f i l e . w r i t e ( " H e l l o f r o m p a s t ! " . g e t B y t e s ( ) ) ; c a t c h ( E x c e p t i o n e ) { S y s t e m . o u t . p r i n t l n ( " S o m e t h i n g b a d h a p p e n e d . " ) ; e . p r i n t S t a c k T r a c e ( ) ; } f i n a l l y { i f ( f i l e ! = n u l l ) { t r y { f i l e . c l o s e ( ) ; } c a t c h ( E x c e p t i o n e ) { } } }
  9. EX #2: ABSTRACTION t r y ( F i l

    e O u t p u t S t r e a m f i l e = n e w F i l e O u t p u t S t r e a m ( " f i l e . t x t " ) ) { f i l e . w r i t e ( " H e l l o f r o m p r e s e n t ! " . g e t B y t e s ( ) ) ; } c a t c h ( E x c e p t i o n e ) { S y s t e m . o u t . p r i n t l n ( " S o m e t h i n g b a d h a p p e n e d . " ) ; e . p r i n t S t a c k T r a c e ( ) ; }
  10. EX #2: ABSTRACTION w i t h R e s

    o u r c e ( n e w F i l e O u t p u t S t r e a m ( " f i l e . t x t " ) ) . p e r f o r m ( r - > r . w r i t e ( " H e l l o f r o m b e t t e r p r e s e n t ! " . g e t B y t e s ( ) ) ) ;
  11. EX #2: ABSTRACTION p u b l i c c

    l a s s R e s o u r c e s { p u b l i c s t a t i c < R > A c t i o n < R > w i t h R e s o u r c e ( f i n a l R r ) { r e t u r n n e w A c t i o n < R > ( ) { @ O v e r r i d e p u b l i c v o i d p e r f o r m ( C o n s u m e r < R > c o n s u m e r ) { t r y ( A u t o C l o s e a b l e a = ( A u t o C l o s e a b l e ) r ) { R r e s = ( R ) a ; c o n s u m e r . a c c e p t ( r e s ) ; } c a t c h ( E x c e p t i o n e ) { S y s t e m . o u t . p r i n t l n ( " S o m e t h i n g b a d h a p p e n e d . " ) ; } } } ; } p u b l i c s t a t i c i n t e r f a c e C o n s u m e r < T > { v o i d a c c e p t ( T t ) t h r o w s E x c e p t i o n ; } p u b l i c s t a t i c i n t e r f a c e A c t i o n < T > { v o i d p e r f o r m ( C o n s u m e r < T > c o n s u m e r ) ; } }
  12. IMPLEMENTATION p u b l i c c l a

    s s M a i n { p u b l i c s t a t i c v o i d m a i n ( S t r i n g [ ] a r g s ) { P r e d i c a t e < I n t e g e r > p = x - > x > 0 ; b o o l e a n r e s = p . t e s t ( 1 ) ; S y s t e m . o u t . p r i n t l n ( r e s ) ; } }
  13. IMPLEMENTATION j a v a p - c M a

    i n p u b l i c s t a t i c v o i d m a i n ( j a v a . l a n g . S t r i n g [ ] ) ; C o d e : 0 : i n v o k e d y n a m i c # 2 , 0 / / I n v o k e D y n a m i c # 0 : t e s t : ( ) L j a v a / u t i l / f u n c t i o 5 : a s t o r e _ 1 6 : a l o a d _ 1 7 : i c o n s t _ 1 8 : i n v o k e s t a t i c # 3 / / M e t h o d j a v a / l a n g / I n t e g e r . v a l u e O f : ( I ) L j a v a / 1 1 : i n v o k e i n t e r f a c e # 4 , 2 / / I n t e r f a c e M e t h o d j a v a / u t i l / f u n c t i o n / P r e d i c a 1 6 : i s t o r e _ 2 1 7 : g e t s t a t i c # 5 / / F i e l d j a v a / l a n g / S y s t e m . o u t : L j a v a / i o / P r i n t S 2 0 : i l o a d _ 2 2 1 : i n v o k e v i r t u a l # 6 / / M e t h o d j a v a / i o / P r i n t S t r e a m . p r i n t l n : ( Z ) V 2 4 : r e t u r n
  14. IMPLEMENTATION j a v a p - p - c

    M a i n p r i v a t e s t a t i c b o o l e a n l a m b d a $ m a i n $ 0 ( j a v a . l a n g . I n t e g e r ) ; C o d e : 0 : a l o a d _ 0 1 : i n v o k e v i r t u a l # 7 / / M e t h o d j a v a / l a n g / I n t e g e r . i n t V a l u e : ( ) I 4 : i f l e 1 1 7 : i c o n s t _ 1 8 : g o t o 1 2 1 1 : i c o n s t _ 0 1 2 : i r e t u r n
  15. METHODS ARE FUNCTIONS p u b l i c c

    l a s s M a i n 2 { p u b l i c s t a t i c v o i d m a i n ( S t r i n g [ ] a r g s ) { d o T e s t ( x - > x > 0 , 7 ) ; d o T e s t ( M a i n 2 : : t e s t , 0 ) ; } p u b l i c s t a t i c b o o l e a n d o T e s t ( P r e d i c a t e < I n t e g e r > p , i n t i ) { r e t u r n p . t e s t ( i ) ; } s t a t i c b o o l e a n t e s t ( I n t e g e r x ) { r e t u r n x > 0 ; } }
  16. METHODS ARE FUNCTIONS b o o l e a n

    t e s t ( I n t e g e r x ) { r e t u r n x > 0 ; } Has type: Integer -> Boolean
  17. RESULTS New ways for abstraction and compostiotion More power -

    more responsibility Experience comes with a practice
  18. MORE FUN Tom Stuart - Jim Weirich - Programming with

    Nothing Y Not? - Adventures in Functional Programming
  19. HOW TO USE? “Don’t think, feel! It is like a

    finger pointing away to the moon. Don’t concentrate on the finger or you will miss all that heavenly glory.” - Bruce Lee