diff --git a/slides.md b/slides.md index 4e3d814..655381b 100644 --- a/slides.md +++ b/slides.md @@ -2,15 +2,15 @@ marp: true title: Una mirada funciona a Java 21 theme: gaia -color: #fff footer: #Commitconf 2024 -backgroundImage: url('https://cdn.wallpapersafari.com/97/99/MnWulT.jpg') +color: #fff transition: fade-out +backgroundImage: url('https://cdn.wallpapersafari.com/97/99/MnWulT.jpg') --- # Una mirada :eyes: funcional a Java :two::one: - + _Antonio Muñoz_ @@ -114,11 +114,11 @@ * Deconstructores, nos permite acceder a los componentes de los objectos. ```java -var area = switch (shape) { +var area = switch (this) { case Square(var side) -> side * side; case Rectangle(var weight, var height) -> weight * height; - case Circle(var radious) -> Math.PI * radious^2; -} + case Circle(var radious) -> Math.PI * Math.pow(radious, 2); +}; ``` --- @@ -140,10 +140,8 @@ ```java public sealed interface Tree { - record Node(String value, Tree left, Tree right) implements Tree { } record Leaf(String value) implements Tree {} - } ``` @@ -155,15 +153,34 @@ ```java public sealed interface Optional { - record Empty() implements Optional { } record Present(T value) implements Optional {} - } ``` --- +# Tipos de datos algebraicos (IV) + +* Podemos representar `Json` con ADTs + +```java +public sealed interface Json { + enum JsonNull implements Json { + NULL + } + enum JsonBoolean implements Json { + TRUE, FALSE + } + record JsonString(String value) implements Json {} + record JsonNumber(Number value) implements Json {} + record JsonObject(Map value) implements Json {} + record JsonArray(List value) implements Json {} +} +``` + +--- + # Ejemplo * ¿Pero eso para qué me sirve? diff --git a/snippets/Json.java b/snippets/Json.java new file mode 100644 index 0000000..ca4642b --- /dev/null +++ b/snippets/Json.java @@ -0,0 +1,17 @@ +package snippets; + +import java.util.List; +import java.util.Map; + +public sealed interface Json { + enum JsonNull implements Json { + NULL + } + enum JsonBoolean implements Json { + TRUE, FALSE + } + record JsonString(String value) implements Json {} + record JsonNumber(Number value) implements Json {} + record JsonObject(Map value) implements Json {} + record JsonArray(List value) implements Json {} +} \ No newline at end of file diff --git a/snippets/Optional.java b/snippets/Optional.java new file mode 100644 index 0000000..b419128 --- /dev/null +++ b/snippets/Optional.java @@ -0,0 +1,6 @@ +package snippets; + +public sealed interface Optional { + record Empty() implements Optional { } + record Present(T value) implements Optional {} +} diff --git a/snippets/Shape.java b/snippets/Shape.java new file mode 100644 index 0000000..16e20a1 --- /dev/null +++ b/snippets/Shape.java @@ -0,0 +1,18 @@ +package snippets; + +public sealed interface Shape { + + default double area() { + var area = switch (this) { + case Square(var side) -> side * side; + case Rectangle(var weight, var height) -> weight * height; + case Circle(var radious) -> Math.PI * Math.pow(radious, 2); + }; + return area; + } + + record Square(int side) implements Shape {} + record Rectangle(int weight, int height) implements Shape {} + record Circle(int radious) implements Shape {} + +} diff --git a/snippets/Switches.java b/snippets/Switches.java new file mode 100644 index 0000000..0ff0a40 --- /dev/null +++ b/snippets/Switches.java @@ -0,0 +1,25 @@ +package snippets; + +public class Switches { + + void method1(String input) { + var value = switch (input) { + case "a" -> 1; + case "b" -> 2; + default -> 0; + }; + System.out.println(value); + } + + void method2(Object obj) { + var result = switch (obj) { + case Integer i -> String.format("int %d", i); + case Long l -> String.format("long %d", l); + case Double d -> String.format("double %f", d); + case String s -> String.format("String %s", s); + default -> obj.toString(); + }; + System.out.println(result); + } + +} diff --git a/snippets/Tree.java b/snippets/Tree.java new file mode 100644 index 0000000..687595d --- /dev/null +++ b/snippets/Tree.java @@ -0,0 +1,6 @@ +package snippets; + +public sealed interface Tree { + record Node(String value, Tree left, Tree right) implements Tree { } + record Leaf(String value) implements Tree {} +}