|
| 1 | +package com.platzi.functional._08_lambda; |
| 2 | + |
| 3 | +import java.util.function.BiFunction; |
| 4 | +import java.util.function.Consumer; |
| 5 | +import java.util.function.Function; |
| 6 | +import java.util.function.Supplier; |
| 7 | + |
| 8 | +public class Syntax { |
| 9 | + private static void sintaxis() { |
| 10 | + //Una funcion puede ser definida de la siguiente manera: |
| 11 | + Function<String, String> funFunction = s -> ""; |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | + |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | + //Y como vimos en la leccion anterior, si queremos ser muy especificos con el tipo: |
| 26 | + Function<String, String> boringFunction = (String s) -> ""; |
| 27 | + |
| 28 | + |
| 29 | + |
| 30 | + |
| 31 | + |
| 32 | + |
| 33 | + |
| 34 | + |
| 35 | + |
| 36 | + |
| 37 | + |
| 38 | + |
| 39 | + //Tambien hemos mencionado que si nuestra funcion es muy extensa podemos usar {} |
| 40 | + Function<String, String> notFun = s -> { |
| 41 | + System.out.println("Ejecutando… 1"); |
| 42 | + System.out.println("Ejecutando… 2"); |
| 43 | + System.out.println("Ejecutando… 3"); |
| 44 | + return ""; |
| 45 | + }; |
| 46 | + |
| 47 | + |
| 48 | + |
| 49 | + |
| 50 | + |
| 51 | + |
| 52 | + |
| 53 | + |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | + |
| 60 | + //La variante empieza cuando tenemos mas de un parametro, pues nos vemos obligados |
| 61 | + //a agrupar los parametros entre parentesis. |
| 62 | + BiFunction<String, String, Integer> biFunFunction = (xs, s) -> 0; |
| 63 | + |
| 64 | + //Incluso si ponemos el tipo explisito |
| 65 | + BiFunction<String, String, Integer> notFunBiFunction = (String xs, String s) -> 0; |
| 66 | + |
| 67 | + |
| 68 | + |
| 69 | + |
| 70 | + |
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | + |
| 77 | + |
| 78 | + |
| 79 | + |
| 80 | + |
| 81 | + |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | + |
| 87 | + //Si nuestra funcion (un Consumer tambien es una funcion) no hace nada, |
| 88 | + //Java nos exige que usemos { } como cuerpo de nuestra funcion. |
| 89 | + Consumer<String> consumer = s -> { }; |
| 90 | + |
| 91 | + |
| 92 | + |
| 93 | + |
| 94 | + |
| 95 | + |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | + |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | + |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | + //Y si nuestra funcion no toma parametros… |
| 108 | + //Java nos exige que usemos () para indicar la ausencia de los mismos: |
| 109 | + Supplier<String> textSupplier = () -> "Hello Platzi"; |
| 110 | + } |
| 111 | +} |
0 commit comments