Skip to content

Instantly share code, notes, and snippets.

View tonivade's full-sized avatar

Antonio Muñoz tonivade

View GitHub Profile
sealed interface Data<T> {
record DataString(String value) implements Data<String> {}
static <T> T get(Data<T> data) {
// why I need to force the cast to T?
return (T) switch (data) {
case DataString(var value) -> value;
};
}
@tonivade
tonivade / Bug.java
Created June 27, 2021 19:59
Compilation error with type inference
import java.util.function.BiFunction;
import java.util.function.Function;
public interface Bug<T> {
<R> Bug<R> map(Function<? super T, ? extends R> mapper);
<R> Bug<R> ap(Bug<Function<? super T, ? extends R>> apply);
static <A, B, C> Function<BiFunction<A, B, C>, Bug<C>> compile(Bug<? extends A> fa, Bug<? extends B> fb) {
@tonivade
tonivade / Bug.java
Last active May 30, 2020 16:27
Java 14 bug in smart constructor
import java.util.function.Supplier;
public record Bug(int value) {
static void bug(Supplier<String> str) {
System.out.println(str.get());
}
public Bug {
bug(() -> "hello");
@tonivade
tonivade / Builder.java
Last active August 30, 2020 07:43
Pure Functional Java Builder
import static java.util.Objects.requireNonNull;
public class Builder {
public static void main(String[] args) {
User user = User.builder().id(1).name("toni").email("toni@home").build();
System.out.println(user);
}
}
@tonivade
tonivade / MTL.java
Last active June 16, 2024 07:13
MTL in Java
//usr/bin/env jbang "$0" "$@" ; exit $?
//JAVA 21
//JAVAC_OPTIONS --enable-preview -source 21
//JAVA_OPTIONS --enable-preview
//DEPS com.github.tonivade:purefun-typeclasses:5.0
//DEPS com.github.tonivade:purefun-instances:5.0
//DEPS com.github.tonivade:purefun-transformer:5.0
@tonivade
tonivade / Option.java
Last active June 18, 2020 09:40
An example of Java 15 sealed classes, pattern matching instanceof and records
import java.util.function.Function;
import java.util.function.Supplier;
public class Main {
public static void main(String args[]) {
var some = new Some<>("hello");
var result = some.map(String::toUpperCase);
System.out.println(result.orElse(""));
@tonivade
tonivade / EnvEffectsTest.java
Last active May 14, 2019 13:06
Proof of concept of enviromental effects in java
/*
* Copyright (c) 2018-2019, Antonio Gabriel Muñoz Conejo <antoniogmc at gmail dot com>
* Distributed under the terms of the MIT License
*/
package com.github.tonivade.purefun.zio;
import static com.github.tonivade.purefun.monad.Console.println;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.io.BufferedReader;
@tonivade
tonivade / Program.java
Last active January 27, 2020 20:48
Tagless final in Java, Is it possible?
import static com.github.tonivade.purefun.data.Sequence.listOf;
import static java.util.Objects.requireNonNull;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
import com.github.tonivade.purefun.Higher1;
import com.github.tonivade.purefun.Kind;
import com.github.tonivade.purefun.Tuple2;
import com.github.tonivade.purefun.Unit;