diff --git a/slides.md b/slides.md index 749d00f..bdee195 100644 --- a/slides.md +++ b/slides.md @@ -28,7 +28,7 @@ --- -# Antes de empezar? +# Encuesta * Java 21? :thumbsup: * Java 17? :ok: @@ -40,10 +40,16 @@ --- -# Java 8 +# Jetbrains ecosystem survey -* Lanzado en 2014 :rocket:. -* [2023 Jetbrains ecosystem survey](https://www.jetbrains.com/lp/devecosystem-2023/java/#java_versions) +* [¿Qué versión de Java usáis regularmente?](https://www.jetbrains.com/lp/devecosystem-2023/java/#java_versions) + + +--- + +# Jetbrains ecosystem survey + +* [¿Qué versión de Java usáis regularmente?](https://www.jetbrains.com/lp/devecosystem-2023/java/#java_versions) | Version | Percentage| @@ -53,6 +59,12 @@ | Java11 | 38% | | Java20 | 11% | +--- + +# Java 8 + +* Lanzado en 2014 :rocket: + +--> \ No newline at end of file diff --git a/snippets/Expr.java b/snippets/Expr.java index 9b9067a..55e6529 100644 --- a/snippets/Expr.java +++ b/snippets/Expr.java @@ -15,6 +15,15 @@ case Times(var left, var right) -> left.evaluate() * right.evaluate(); }; } + + default String print() { + return switch (this) { + case Lit(var value) -> String.valueOf(value); + case Sum(var left, var right) -> "(" + left.print() + "+" + right.print() + ")"; + case Diff(var left, var right) -> "(" + left.print() + "-" + right.print() + ")"; + case Times(var left, var right) -> "(" + left.print() + "*" + right.print() + ")"; + }; + } static Expr lit(int value) { return new Lit(value); diff --git a/snippets/List.java b/snippets/List.java index 227b964..9adf420 100644 --- a/snippets/List.java +++ b/snippets/List.java @@ -1,6 +1,29 @@ package snippets; +import java.util.function.Function; +import java.util.function.Predicate; + sealed interface List { record NonEmpty(T head, List tail) implements List {} record Empty() implements List {} + + default List map(Function mapper) { + return switch (this) { + case NonEmpty(var head, var tail) + -> new NonEmpty<>(mapper.apply(head), tail.map(mapper)); + case Empty() + -> new Empty<>(); + }; + } + + default List filter(Predicate filter) { + return switch (this) { + case NonEmpty(var head, var tail) when filter.test(head) + -> new NonEmpty<>(head, tail.filter(filter)); + case NonEmpty(var head, var tail) + -> tail.filter(filter); + case Empty() + -> new Empty<>(); + }; + } } diff --git a/snippets/Optional.java b/snippets/Optional.java index 5537351..4cf3d4a 100644 --- a/snippets/Optional.java +++ b/snippets/Optional.java @@ -5,34 +5,36 @@ import java.util.function.Supplier; sealed interface Optional { + record Empty() implements Optional { } record Present(T value) implements Optional {} default Optional map(Function mapper) { return switch (this) { - case Present(T value) -> new Present<>(mapper.apply(value)); - case Empty() -> new Empty<>(); + case Present(var value) -> new Present<>(mapper.apply(value)); + case Empty _ -> new Empty<>(); }; } default Optional flatMap(Function> mapper) { return switch (this) { - case Present(T value) -> mapper.apply(value); - case Empty() -> new Empty<>(); + case Present(var value) -> mapper.apply(value); + case Empty _ -> new Empty<>(); }; } default R fold(Supplier onEmpty, Function onPresent) { return switch (this) { - case Present(T value) -> onPresent.apply(value); - case Empty() -> onEmpty.get(); + case Present(var value) -> onPresent.apply(value); + case Empty _ -> onEmpty.get(); }; } default Optional filter(Predicate filter) { return switch (this) { - case Present(T value) when filter.test(value) -> this; - default -> new Empty<>(); + case Present(var value) when filter.test(value) -> this; + case Present _ -> new Empty<>(); + case Empty _ -> new Empty<>(); }; } -} +} \ No newline at end of file