Last active
August 29, 2015 14:01
-
-
Save hamnis/da3fadd9f662c52bee49 to your computer and use it in GitHub Desktop.
This file contains 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
import net.hamnaberg.funclite.*; | |
public class Collect { | |
public static <A, B> Option<B> collect(A value, Predicate<A> p, Function<A, B> f) { | |
if (p.apply(value)) { | |
return Option.fromNullable(f.apply(value)); | |
} | |
return Option.none(); | |
} | |
public static void main(String[] args) { | |
String hello = "hello"; | |
collect(hello, String::isEmpty, Functions::identity) | |
} | |
} |
This file contains 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
import net.hamnaberg.funclite.*; | |
public class Collect2 { | |
public static class PartialFunction<A, B> implements Function<A, B> { | |
private final Predicate<A> p; | |
private final Function<A, B> f; | |
public PartialFunction(Predicate<A> p, Function<A, B> f) { | |
this.p = p; | |
this.f = f; | |
} | |
public boolean isDefinedAt(A value) { | |
return p.apply(value); | |
} | |
public B apply(A value) { | |
return f.apply(value); | |
} | |
} | |
public static <A, B> Option<B> collect(A value, PartialFunction<A, B> pf) { | |
if (pf.isDefinedAt(value)) { | |
return Option.fromNullable(pf.apply(value)); | |
} | |
return Option.none(); | |
} | |
public static void main(String[] args) { | |
String hello = "hello"; | |
collect(hello, new PartialFunction(String::isEmpty, Functions::identity)) | |
} | |
} |
This file contains 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
import net.hamnaberg.funclite.*; | |
public class Collect3 { | |
public static interface PartialFunction<A, B> extends Function<A, B> { | |
boolean isDefinedAt(A value); | |
B apply(A value); | |
} | |
public static class MapPartialFunction<A,B> implements PartialFunction<A,B> { | |
private Map<A, B> map; | |
public MapPartialFunction(Map<A, B> m) { | |
this.map = m; | |
} | |
public boolean isDefinedAt(A value) { | |
return map.containsKey(value); | |
} | |
public B apply(A value) { | |
return map.get(value); | |
} | |
} | |
public static class PartialFunctionImpl<A, B> implements PartialFunction<A, B> { | |
private final Predicate<A> p; | |
private final Function<A, B> f; | |
public PartialFunctionImpl(Predicate<A> p, Function<A, B> f) { | |
this.p = p; | |
this.f = f; | |
} | |
public boolean isDefinedAt(A value) { | |
return p.apply(value); | |
} | |
public B apply(A value) { | |
return f.apply(value); | |
} | |
} | |
public static <A, B> Option<B> collect(A value, PartialFunction<A, B> pf) { | |
if (pf.isDefinedAt(value)) { | |
return Option.fromNullable(pf.apply(value)); | |
} | |
return Option.none(); | |
} | |
public static void main(String[] args) { | |
String hello = "hello"; | |
collect(hello, new PartialFunction(String::isEmpty, Functions::identity)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment