Discovering Lambdas (Speech)

  • View
    15

  • Download
    0

Embed Size (px)

Text of Discovering Lambdas (Speech)

  • Discovering Lambdas in Java8by Aleksandra Dmytrenko

  • Java -> ?

  • !

  • ? , final ( ) ( ). ( ?) () - , .

  • ? ( ) final const, public ( ?) ( ) () - . .

  • , ?

  • -

  • -> ?Imperative style:public boolean isPrime(int number) { for (int curNumb = 2; curNumb number % curNumb == 0);}

  • public boolean isPrimeImperative(int number) { if (number < 0) { return false; } for (int curNumb = 2; curNumb = 0 && IntStream.rangeClosed(2, number.2).noneMatch(curNumb -> number % curNumb == 0);}

  • -

  • ?public static int factorialOf(int number) { if (number >= 0) { int factorial = 1; for (int curNumb = 2; curNumb
  • . ?public static int factorialOf(int number) { if (number > 0) return number*factorialOf(number-1); if (number == 0) return 1; else throw new IllegalArgumentException("Factorial can be counted only of not negative numbers.");}3 !public static int factorialOf(int number) { if (number >= 0) return IntStream.rangeClosed(2, number) .reduce(1, (accResult, curNumb) -> accResult * curNumb); else throw new IllegalArgumentException("Factorial can be counted only of not negative numbers.");}3 !

  • - , . .

  • Chuck Norris can do multiple inheritance in Java

  • , . ! , .

  • C , C?interface Elephant { default String makeSound(String name) { return name + ": !"; }}

    interface Dog { default String makeSound(String name) { return name + ": -"; }}

  • C , C?class EveryDogWantsToBeAnElephant implements Dog, Elephant {@Overridepublic String makeSound(String name) { return name + ": ";}public static void main(final String[] args) { EveryDogWantsToBeAnElephant elephantDog = new EveryDogWantsToBeAnElephant(); Elephant e = new Elephant(){}; Dog d = new Dog(){}; System.out.println(e.makeSound("")); System.out.println(d.makeSound("")); System.out.println(elephantDog.makeSound("-"));}}: !: --:

  • - . ?

  • .. , interface Dog {

    static String walk() { return " .; }}

    interface Elephant {

    static String walk() { return " ."; }}System.out.println(Elephant.walk());System.out.println(Dog.walk());

    OUTPUT:

    . .

  • (), . , .

  • : Comparator int compare(T o1, T o2), Comparable int compareTo(T o) ,

  • -:Elephant e = new Elephant() { @Override public boolean isProud() { return false; }}; -:Elephant e = () -> { return false; };

    :Elephant e = () -> false;

  • interface Elephant { boolean isProud();}

    interface Dog { boolean isProud();}

    System.out.println(" : " + e.isProud());System.out.println(" : " + d.isProud());System.out.println("- : " + elephantDog.isProud());

    OUTPUT: : false : true- : false

  • :public Predicate isGreaterThan2New() { return a -> a > 2;} :public boolean isGreaterThan2Old(Integer a) { return a > 2;}

    -:public BiPredicate isGreaterThanFunc() { return (a, b) -> a > b;}

    :public boolean isGreaterThan(Integer a, Integer b) { return a > b;}

    , boolean test(T t); boolean test(T t), ?

  • :public Function multiplyFuncBy2() { return (a) -> a * 2;} :public Integer multiplyBy2(Integer a) { return a * 2;}

    -:public BiFunction multiplyFuncBy() { return (a, b) -> a * b;} :public Integer multiplyFuncBy(Integer a, Integer b) { return a * b;}

    , , ,

  • -, :boolean actual = p.isGreaterThan2New().test(4);boolean actual = p.isGreaterThan2Old(4);p.isGreaterThanNew().test(4, 8);p.isGreaterThanOld(4, 8); :values.stream().filter(p.isGreaterThan2New( )).collect(Collectors.toList());List actual = values.stream().filter(v -> p.isGreaterThan2(v)).collect(Collectors.toList());

    . .

  • ?

    -, :int actual = f.multiplyFuncBy2().apply(5);int actual = f.multiplyBy2(5);f.multiplyFuncBy().apply(5, 3);f.multiplyBy(5, 3);

    : values.stream().map(f.multiplyFuncBy2()).skip(2).limit(2).collect(Collectors.toList());List actual = values.stream().map(v -> f.multiplyFuncBy().apply(v, 3)).skip(2).limit(2).collect(Collectors.toList());

    apply()

  • Consumer -

    void :public void useConsumer(T t){ System.out.print("Consumer says: " + t);}public Consumer useConsumerFunc(){ return (t) -> System.out.print("Consumer says: " + t);} :consumer.useConsumer("Hello ;)"); consumer.useConsumerFunc().accept("Hello ;)"); : Stream.of(1, 6).forEach(consumer.useConsumerFunc() .andThen(l -> System.out.print("\n")));OUTPUT:

    Consumer says: 1Consumer says: 6

  • Supplier -

    2 - public String useSupplier() { return Thread.currentThread().getStackTrace()[1].getMethodName(); }

    public Supplier useSupplierFunc() { System.out.println(Thread.currentThread().getStackTrace()[1].getMethodName()); return () -> Thread.currentThread().getStackTrace()[1].getMethodName(); }

  • Supplier -

    :String actual = supplier.useSupplier();String actual = supplier.useSupplierFunc().get();Stream.of("1 ").forEach(n -> System.out.println(n + supplier.useSupplierFunc()));:1) useSupplier2) useSupplierFunc lambda$useSupplierFunc$03) useSupplierFunc 1 lambdas.functionalInterface.MySupplier$$Lambda$1/1449621165@3d8c7aca

  • , , Java

    https://github.com/olexandra-dmytrenko/LambdasForConfs.git

  • :)