This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var p = new Person("Saeed", "[email protected]"); | |
| var json = toJson(p, personWriter()).as(JsObject.class); | |
| System.out.println(json.get().get("name").as(JsString.class).get()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| static <A> Json toJson(A value, JsonWriter<A> w){ | |
| return w.write(value); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| static JsonWriter<Person> personWriter() { | |
| return v -> new JsObject(Map.of("name", new JsString(v.getName()), "email", new JsString(v.getEmail()))); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Person { | |
| private String name; | |
| private String email; | |
| public Person(String name, String email) { | |
| this.name = name; | |
| this.email = email; | |
| } | |
| public String getName() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| interface JsonWriter<A> { | |
| Json write(A value); | |
| static JsonWriter<String> stringWriter() { | |
| return JsString::new; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| interface Json{ | |
| default <T> T as(){ | |
| return (T) this; | |
| } | |
| default <T> T as(Class<T> clazz){ | |
| return clazz.cast(this); | |
| } | |
| } | |
| class JsObject implements Json{ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var pairs = List.of(new Pair<>(1, "hello"), new Pair<>(2, " "), new Pair<>(3, "world")); | |
| System.out.println(show(combineAll(pairs, deriveMonoidPair(intAdditionMonoid(), stringConcatMonoid())), showPair(showInt(), showString()))); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| interface Monoid<A> extends Empty<A> { | |
| A combine(A x, A y); | |
| ... | |
| static <A, B> Monoid<Pair<A, B>> deriveMonoidPair(Monoid<A> A, Monoid<B> B) { | |
| return new Monoid<>() { | |
| @Override | |
| public Pair<A, B> combine(Pair<A, B> x, Pair<A, B> y) { | |
| return new Pair<>(A.combine(x.getFirst(), y.getFirst()), B.combine(x.getSecond(), y.getSecond())); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Pair<A, B> { | |
| private A first; | |
| private B second; | |
| public Pair(A first, B second) { | |
| this.first = first; | |
| this.second = second; | |
| } | |
| public A getFirst() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var mPairs = List.of( | |
| new MPair<>(1, "hello", intAdditionMonoid(), stringConcatMonoid()) | |
| , new MPair<>(2, " ", intAdditionMonoid(), stringConcatMonoid()) | |
| , new MPair<>(3, "world", intAdditionMonoid(), stringConcatMonoid())); | |
| combineAll(mPairs) | |
| .ifPresent(v -> System.out.println(show(v, showMPair(showInt(), showString())))); |