Skip to content

Commit e7b21a3

Browse files
committed
Clear of personal practice. Keep global config for java 21 and better gradle implementation
1 parent d5e952c commit e7b21a3

17 files changed

Lines changed: 0 additions & 727 deletions

File tree

docs/Apuntes java functional.docx

-644 KB
Binary file not shown.

docs/Functional SE.pdf

-1.12 MB
Binary file not shown.
-312 KB
Binary file not shown.
Lines changed: 0 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,4 @@
11
package com.platzi.functional_student_practice._03_inmutable;
22

3-
import java.util.List;
4-
53
public class C8InmutObjWithMutAttributes {
6-
7-
public static void main(String[] args) {
8-
9-
System.out.println("////////////////////////////////////////////////////////////////////////////////////");
10-
System.out.println("\nCLASS 8\n");
11-
12-
// 1. We make an inmutable object (without setters)
13-
Direccion dir = new Direccion("New York");
14-
15-
// 2. We make a mutable object (with setters)
16-
Persona p1 = new Persona(dir);
17-
18-
// 3. We make an inmutable list
19-
List<Persona> lista1 = List.of(p1);
20-
21-
// 4. Try to change mutable element from inmutable list is allowed
22-
System.out.println("Antes de modificar: " + lista1);
23-
p1.getDireccion().setCiudad("Los Angeles");
24-
System.out.println("Después de modificar: " + lista1);
25-
26-
}
27-
28-
private static class Direccion {
29-
String ciudad;
30-
31-
Direccion(String ciudad) {
32-
this.ciudad = ciudad;
33-
}
34-
35-
String getCiudad() {
36-
return ciudad;
37-
}
38-
39-
void setCiudad(String ciudad) {
40-
this.ciudad = ciudad;
41-
}
42-
43-
@Override
44-
public String toString() {
45-
return ciudad;
46-
}
47-
}
48-
49-
private static class Persona {
50-
private final Direccion direccion;
51-
52-
Persona(Direccion direccion) {
53-
this.direccion = direccion;
54-
}
55-
56-
public Direccion getDireccion() {
57-
return direccion;
58-
}
59-
60-
@Override
61-
public String toString() {
62-
return "Persona{direccion=" + direccion + "}";
63-
}
64-
}
65-
664
}
Lines changed: 0 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,4 @@
11
package com.platzi.functional_student_practice._04_functional;
22

3-
import java.util.function.IntPredicate;
4-
import java.util.function.IntUnaryOperator;
5-
import java.util.function.Predicate;
6-
73
public class C11y12MathFunctions {
8-
public static void main(String[] args) {
9-
10-
System.out.println("////////////////////////////////////////////////////////////////////////////////////");
11-
System.out.println("\nCLASS 11\n");
12-
13-
// 1. function as type
14-
// Function<Integer, Integer> square1 = new Function<Integer, Integer>() {
15-
// @Override
16-
// public Integer apply(Integer x) {
17-
// return x * x;
18-
// }
19-
// };
20-
// System.out.println(square1.apply(5)+"\n");
21-
22-
// 2. function 1 writen in a more performant way
23-
IntUnaryOperator square2 = x -> x * x;
24-
System.out.println(square2.applyAsInt(6)+"\n");
25-
26-
// 3. function 3 declared in the class, outside the main method
27-
System.out.println(square3(7));
28-
System.out.println(square3(8)+"\n");
29-
30-
31-
System.out.println("////////////////////////////////////////////////////////////////////////////////////");
32-
System.out.println("\nCLASS 12\n");
33-
34-
// 1. Function as type using lambda to declare operation in one line
35-
// Function<Integer, Boolean> isOdd1 = x -> x % 2 == 1;
36-
// System.out.println(isOdd1.apply(7));
37-
// System.out.println(isOdd1.apply(8)+"\n");
38-
39-
// 2. function 1 writen in a more performant way
40-
IntPredicate isOdd2 = x -> x % 2 == 1;
41-
System.out.println(isOdd2.test(7));
42-
System.out.println(isOdd2.test(8)+"\n");
43-
44-
// 3. function Predicate by teacher
45-
// Predicate<Integer> isEven1 = x -> x % 2 == 0;
46-
// System.out.println(isEven1.test(7));
47-
// System.out.println(isEven1.test(8)+"\n");
48-
49-
// 4. function 3 performant
50-
IntPredicate isEven2 = x -> x % 2 == 0;
51-
System.out.println(isEven2.test(7));
52-
System.out.println(isEven2.test(8)+"\n");
53-
54-
// 5. Student example
55-
Predicate<Student> isApproved = student -> student.getQualification()>= 7;
56-
System.out.println(isApproved.test(new Student(6)));
57-
System.out.println(isApproved.test(new Student(7)));
58-
}
59-
60-
private static int square3(int x) {
61-
return x * x;
62-
}
63-
64-
private static class Student {
65-
private final double qualification;
66-
public Student(double qualification) {
67-
this.qualification = qualification;
68-
}
69-
public double getQualification() {
70-
return qualification;
71-
}
72-
}
73-
744
}
Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,4 @@
11
package com.platzi.functional_student_practice._04_functional;
22

3-
import java.util.ArrayList;
4-
import java.util.List;
5-
import java.util.function.Consumer;
6-
import java.util.function.Supplier;
7-
83
public class C13CLIUtils {
9-
10-
public static void main(String[] args) {
11-
12-
System.out.println("////////////////////////////////////////////////////////////////////////////////////\n");
13-
System.out.println("CLASS 13\n");
14-
15-
// 1. Created CLI class, consumer and supplier
16-
List<CLIArguments> list = new ArrayList<>();
17-
for (int i = 0; i < 5; i++) {
18-
list.add(generateCLI());
19-
list.get(i).setHelp(i % 2 == 0);
20-
}
21-
22-
for (int i = 0; i < list.size(); i++) {
23-
showHelp(list.get(i), i);
24-
}
25-
26-
}
27-
28-
29-
private static void showHelp(CLIArguments cliArguments, int i) {
30-
Consumer<CLIArguments> consumerHelper = cliArguments1 -> {
31-
if (cliArguments1.isHelp()) {
32-
System.out.println(i +": Manual required");
33-
}
34-
};
35-
consumerHelper.accept(cliArguments);
36-
}
37-
38-
39-
private static CLIArguments generateCLI() {
40-
Supplier<CLIArguments> generator = CLIArguments::new; //same as () -> new CLIArguments()
41-
return generator.get();
42-
}
43-
44-
45-
private static class CLIArguments {
46-
private boolean isHelp;
47-
48-
public boolean isHelp() {
49-
return isHelp;
50-
}
51-
52-
public void setHelp(boolean help) {
53-
isHelp = help;
54-
}
55-
}
56-
574
}
Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,4 @@
11
package com.platzi.functional_student_practice._04_functional;
22

3-
import java.util.function.BiFunction;
4-
import java.util.function.BinaryOperator;
5-
import java.util.function.IntBinaryOperator;
6-
import java.util.function.UnaryOperator;
7-
83
public class C14StringFunctions {
9-
10-
public static void main(String[] args) {
11-
12-
System.out.println("////////////////////////////////////////////////////////////////////////////////////\n");
13-
System.out.println("CLASS 14\n");
14-
15-
// 1. Unary operators are a Function class where input and return types match
16-
final String SALUTE = "Hi programing student";
17-
UnaryOperator<String> quote = text -> "\"" + text + "\"";
18-
System.out.println(quote.apply(SALUTE));
19-
20-
UnaryOperator<String> addMark = text -> text + "!";
21-
System.out.println(quote.apply(addMark.apply(SALUTE)) + "\n");
22-
23-
// 2. BiFunction class is a Function class that accept 2 parameters and return another
24-
BiFunction<Integer, Integer, Integer> multiply1 = (x, y) -> x * y;
25-
System.out.println(multiply1.apply(5, 4));
26-
27-
// 3. function 2 improved
28-
BinaryOperator<Integer> multiply2 = (x, y) -> x * y;
29-
System.out.println(multiply2.apply(5, 4));
30-
31-
// 4. function 3 improved
32-
IntBinaryOperator multiply3 = (x, y) -> x * y;
33-
System.out.println(multiply3.applyAsInt(5, 4) + "\n");
34-
35-
// 5. BiFunction with mixed types
36-
BiFunction<String, Integer, String> leftPad = (text, number) -> {
37-
String formatSpecifier = "%" + number + "s"; //add spaces up to making a string of n characters
38-
return String.format(formatSpecifier, text);
39-
};
40-
System.out.println(leftPad.apply("Java", 10) + "\n");
41-
42-
43-
}
44-
45-
464
}
Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,4 @@
11
package com.platzi.functional_student_practice._05_sam;
22

3-
import java.time.LocalDate;
4-
import java.time.Period;
5-
import java.util.function.IntFunction;
6-
73
public class C15AgeUtils {
8-
9-
public static void main(String[] args) {
10-
11-
System.out.println("////////////////////////////////////////////////////////////////////////////////////");
12-
System.out.println("\nCLASS 15\n");
13-
14-
// 1. We declare a function to format integers for parser
15-
IntFunction<String> addCero = num -> num < 10 ? "0" + num : "" + num;
16-
17-
// 2. We parse integers into a LocalDate object
18-
TriFunction<Integer, Integer, Integer, LocalDate> formatDate =
19-
(dd, mm, yyyy) -> LocalDate.parse(yyyy + "-" + addCero.apply(mm) + "-" + addCero.apply(dd));
20-
21-
// 3. we calculate
22-
TriFunction<Integer, Integer, Integer, Integer> calculateAge =
23-
(d, m, y) -> Period.between(formatDate.apply(d, m, y), LocalDate.now()).getYears();
24-
25-
System.out.println(calculateAge.apply(24,6,2014));
26-
27-
}
28-
29-
// 0. We declare a FunctionalInterface to implement in our cude
30-
@FunctionalInterface
31-
interface TriFunction<T, U, V, R> {
32-
R apply(T t, U u, V v);
33-
}
34-
354
}
Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,4 @@
11
package com.platzi.functional_student_practice._06_reference_operator;
22

3-
import java.util.Arrays;
4-
import java.util.List;
5-
import java.util.function.Consumer;
6-
73
public class C16NombresUtils {
8-
9-
public static void main(String[] args) {
10-
11-
System.out.println("////////////////////////////////////////////////////////////////////////////////////");
12-
System.out.println("\nCLASS 15\n");
13-
14-
List<String> teachers = getList("Juan", "Pedro", "Sara", "Gloria");
15-
16-
// 1. Clasic Lambda
17-
//Consumer<String> printer = text -> System.out.println(text)
18-
19-
// 2. Method reference with operator "::"
20-
Consumer<String> printer = System.out::println;
21-
22-
teachers.forEach(printer);
23-
}
24-
25-
@SafeVarargs
26-
private static <T> List<T> getList(T... elements) {
27-
return Arrays.asList(elements);
28-
}
29-
304
}
Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,4 @@
11
package com.platzi.functional_student_practice._07_inference;
22

3-
import com.platzi.functional_teacher_theory._06_reference_operator.NombresUtils;
4-
5-
import java.util.List;
6-
73
public class C17Inference {
8-
public static void main(String[] args) {
9-
10-
System.out.println("////////////////////////////////////////////////////////////////////////////////////");
11-
System.out.println("\nCLASS 17\n");
12-
13-
List<String> alumnos = NombresUtils.getList("Hugo", "Paco", "Luis");
14-
//alumnos.forEach((String name) -> System.out.println(name)) // 1. Version with explicit types
15-
//alumnos.forEach(name -> System.out.println(name)) // 2. Version with lambda with explicit param, implicit type
16-
alumnos.forEach(System.out::println); // 3. Version with reference operator, implicit params and types
17-
}
184
}

0 commit comments

Comments
 (0)