|
| 1 | +package com.platzi.functional._13_partial_application; |
| 2 | + |
| 3 | +import java.util.function.BiFunction; |
| 4 | +import java.util.function.Function; |
| 5 | + |
| 6 | +public class Partial { |
| 7 | + |
| 8 | + static void partialApplication() { |
| 9 | + /* |
| 10 | + Usando currying podemos hacer uso de otra estrategia que se conoce como aplicacion parcial. |
| 11 | +
|
| 12 | + Hacer uso de la aplicacion parcial es almacenar la version "curried" de una funcion pero |
| 13 | + con un parametro. |
| 14 | +
|
| 15 | + Tomemos una BiFunction por ejemplo: |
| 16 | + */ |
| 17 | + BiFunction<Integer, Integer, Integer> multiplyXbyY = (x, y) -> x * y; |
| 18 | + |
| 19 | + //Es facil usarla: |
| 20 | + System.out.println( |
| 21 | + multiplyXbyY.apply(5, 4) //20 |
| 22 | + ); |
| 23 | + |
| 24 | + //Pero si quisieramos asignar un valor fijo a una funcion? Partial application al rescate. |
| 25 | + |
| 26 | + /* |
| 27 | + Primero, definamos una funcion que hace currying: |
| 28 | + */ |
| 29 | + Function<BiFunction<Integer, Integer, Integer>, Function<Integer, Function<Integer, Integer>>> |
| 30 | + curryBiFunction = biFun -> X -> Y -> biFun.apply(X, Y); |
| 31 | + |
| 32 | + /* |
| 33 | + Ahora podemos definir una funcion que genera mas funciones, usando la BiFunction que ya tenemos: |
| 34 | + */ |
| 35 | + Function<Integer, Function<Integer, Integer>> multiplyBy = |
| 36 | + x -> curryBiFunction.apply(multiplyXbyY).apply(x); |
| 37 | + |
| 38 | + //Y crear funciones para valores especificos: |
| 39 | + Function<Integer, Integer> multiplyBy5 = y -> multiplyBy.apply(5).apply(y); |
| 40 | + |
| 41 | + System.out.println( |
| 42 | + multiplyBy5.apply(20) |
| 43 | + ); |
| 44 | + |
| 45 | + Function<Integer, Integer> multiplyBy2 = x -> multiplyBy.apply(2).apply(x); |
| 46 | + |
| 47 | + System.out.println( |
| 48 | + multiplyBy2.apply(10) |
| 49 | + ); |
| 50 | + } |
| 51 | + |
| 52 | + |
| 53 | +// |
| 54 | +// |
| 55 | +// |
| 56 | +// |
| 57 | +// |
| 58 | +// |
| 59 | +// |
| 60 | +// |
| 61 | +// |
| 62 | +// |
| 63 | +// |
| 64 | +// |
| 65 | +// |
| 66 | +// |
| 67 | +// |
| 68 | +// |
| 69 | +// |
| 70 | + |
| 71 | + /** |
| 72 | + * Aqui un ejemplo mas complejo: |
| 73 | + */ |
| 74 | + static void ejemploDB(DBConfiguration connectionConf) { |
| 75 | + //Imagina que tuvieramos esta funcion, que puede recibir una configuracion |
| 76 | + // de conexion a una base de datos y un query a ejecutar sobre dicha conexion. |
| 77 | + BiFunction<DBConfiguration, Query, QueryResult> biDB = (conf, query) -> |
| 78 | + new DataBaseConnection(conf).executeQuery(query); |
| 79 | + |
| 80 | + //Por cada corrida sobre esa funcion, tendremos que pasar la configuracion |
| 81 | + QueryResult result1 = biDB.apply(connectionConf, new Query("SELECT …")); |
| 82 | + //Que aunque no esta mal, parece algo redundante… |
| 83 | + result1 = biDB.apply(connectionConf, new Query("INSERT …")); |
| 84 | + |
| 85 | + //Podemos aplicar curry a nuestra bifunction para simplificar las llamadas: |
| 86 | + Function<DBConfiguration, Function<Query, QueryResult>> dbFunCreator = curryBiFunction(biDB); |
| 87 | + |
| 88 | + //Creamos una funcion que trabajara sobre postgres… |
| 89 | + Function<Query, QueryResult> postgresExecutor = |
| 90 | + pgQuery -> dbFunCreator.apply(new DBConfiguration(/*Postgres configs*/)).apply(pgQuery); |
| 91 | + |
| 92 | + //Y otra funcion para MariaDB |
| 93 | + Function<Query, QueryResult> mariaDBExecutor = |
| 94 | + query -> dbFunCreator.apply(connectionConf).apply(query); |
| 95 | + |
| 96 | + postgresExecutor.apply(new Query("SELECT…")); |
| 97 | + postgresExecutor.apply(new Query("INSERT…")); |
| 98 | + |
| 99 | + mariaDBExecutor.apply(new Query("UPDATE…")); |
| 100 | + } |
| 101 | +// |
| 102 | +// |
| 103 | +// |
| 104 | +// |
| 105 | +// |
| 106 | +// |
| 107 | +// |
| 108 | +// |
| 109 | +// |
| 110 | +// |
| 111 | +// |
| 112 | +// |
| 113 | +// |
| 114 | +// |
| 115 | + |
| 116 | + static <F, S, R> Function<F, Function<S, R>> curryBiFunction(BiFunction<F, S, R> biFunction) { |
| 117 | + return f -> s -> biFunction.apply(f, s); |
| 118 | + } |
| 119 | +// |
| 120 | +// |
| 121 | +// |
| 122 | +// |
| 123 | +// |
| 124 | +// |
| 125 | +// |
| 126 | +// |
| 127 | +// |
| 128 | +// |
| 129 | +// |
| 130 | +// |
| 131 | +// |
| 132 | +// |
| 133 | + |
| 134 | + static class QueryResult { |
| 135 | + |
| 136 | + } |
| 137 | + |
| 138 | + static class Query { |
| 139 | + public Query() { |
| 140 | + |
| 141 | + } |
| 142 | + |
| 143 | + public Query(String query) { |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + static class DBConfiguration { |
| 148 | + private String host; |
| 149 | + private String user; |
| 150 | + private String password; |
| 151 | + |
| 152 | + private int port; |
| 153 | + |
| 154 | + public DBConfiguration() { |
| 155 | + } |
| 156 | + |
| 157 | + public DBConfiguration(String host, String user, String password, int port) { |
| 158 | + this.host = host; |
| 159 | + this.user = user; |
| 160 | + this.password = password; |
| 161 | + this.port = port; |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + static class DataBaseConnection { |
| 166 | + public DataBaseConnection(DBConfiguration dbConfiguration) { |
| 167 | + } |
| 168 | + |
| 169 | + public QueryResult executeQuery(Query query) { |
| 170 | + return null; |
| 171 | + } |
| 172 | + } |
| 173 | +} |
0 commit comments