Java仿haskell
作者:互联网
Functor函子
package cn.lyf.fx;
import java.util.*;
import java.util.function.*;
//集成了Either函子
public class Functor {
private Object value;
protected Functor(Object val) {
value = val;
}
/// Pointed///产生个函子
public static Functor of(Object val) {
return new Functor(val);
}
// 范畴间映射变换
public <T, R> Functor map(Function<T, R> fx) {
return innerMap(value, fx);
}
@SuppressWarnings("unchecked")
protected <T, R> Functor innerMap(Object o, Function<T, R> fx) {
try {
/// Either/
if (o instanceof Exception)
return this;
R r = fx.apply((T) o);
return of(r);
} catch (Exception e) {
return of(e);
}
}
// 获取被包装的value
public Functor unpack() {
while (value instanceof Functor) {
value = ((Functor) value).value;
}
return of(value);
}
public Object getValue() {
return value;
}
static class composeFunction<T, E, R> implements Function<T, R> {
Function<T, E> f;
Function<E, R> g;
composeFunction(Function<T, E> f, Function<E, R> g) {
this.f = f;
this.g = g;
}
@Override
public R apply(T t) {
return g.apply(f.apply(t));
}
}
// 复合函数
public static <T, E, R> Function<T, R> compose(final Function<T, E> f, final Function<E, R> g) {
return new composeFunction<T, E, R>(f, g);
}
static class Currying<R> implements Function<Object, Object> {
List<Object> avgs;
Function<Object[], R> fx;
boolean run;
Currying(List<Object> avgs, Function<Object[], R> fx) {
this.avgs = avgs;
this.fx = fx;
}
@Override
public Object apply(Object t) {
if (run)
return fx.apply(avgs.toArray());
avgs.add(t);
return new Currying<>(avgs, fx);
}
}
// 柯里化
public static <R> Function<Object, Object> currying(Function<Object[], Function<Object[], R>> f) {
return new Function<Object, Object>() {
@Override
public Object apply(Object t) {
List<Object> avgs = new LinkedList<>();
avgs.add(t);
return new Currying<>(avgs, f);
}
};
}
public Functor end() {
Currying<?> curry = (Currying<?>) value;
curry.run = true;
return of(curry.apply(null));
}
// Monad///
public <T, R> Functor flatMap(Function<T, R> fx) {
return this.map(fx).join();
}
public Functor join() {
if (value == null || !(value instanceof Functor))
return this;
return (Functor) value;
}
// Maybe
public <T, R> Functor Maybe(Function<T, R> fx) {
if (value == null)
return this;
return innerMap(value, fx);
}
// Applicative//value是函数,返回一个中间态函子
public Functor ap(Object box) {
Function<?, ?> fx = (Function<?, ?>) value;
return of(box).map(fx);
}
@Override
public String toString() {
return "{" + value.toString() + "}";
}
@Override
public boolean equals(Object obj) {
if (obj instanceof Functor) {
return ((Functor) obj).value.equals(value);
}
return false;
}
@Override
public int hashCode() {
if (value != null)
return value.hashCode();
return 0;
}
}
IO 函子
package cn.lyf.fx;
import java.util.function.*;
import java.io.*;
public class IO extends Functor {
IO(Object fx) {
super(fx);
}
public static IO of(String str) {
return new IO(str);
}
public static Function<String, String> scan = new Function<>() {
public String apply(String path) {
try (FileInputStream fis = new FileInputStream(path);
InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
BufferedReader br = new BufferedReader(isr);) {
String line = null;
StringBuffer sb = new StringBuffer();
while ((line = br.readLine()) != null) {
sb.append(line);
sb.append("\n");
}
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
};
};
public static Function<String, String> print = new Function<>() {
public String apply(String str) {
System.out.println(str);
return str;
};
};
}
标签:Function,return,fx,value,haskell,Functor,Java,public 来源: https://blog.csdn.net/qq_45256489/article/details/119001055