Skip to content

Instantly share code, notes, and snippets.

@arahansa
Last active November 17, 2017 04:46
Show Gist options
  • Save arahansa/178fd7ec040e3183fc2a5e62fb85045e to your computer and use it in GitHub Desktop.
Save arahansa/178fd7ec040e3183fc2a5e62fb85045e to your computer and use it in GitHub Desktop.
함수형을 자바로
package chap02.chap02_6;
import chap02.chap02_5.PolyFuncJava;
import chap03.chap03_2.JavaList;
import chap10.Foldable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.BiPredicate;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.IntStream;
public class FpJava<A, B, C>{
int findFirst(final List<A> as, Predicate<A> p){
return IntStream.range(0, as.size())
.filter(n-> p.test(as.get(n)))
.findFirst()
.orElse(-1);
}
boolean isSorted(final List<A> as, BiPredicate<A,A> p){
if(as.size()==1) return true;
return IntStream.range(0, as.size()-1)
.allMatch(e-> p.test(as.get(e), as.get(e+1)));
}
Function<B,C> partial1(A a, BiFunction<A,B,C> f){
return (B b) -> f.apply(a, b);
}
Function<A, Function<B,C>> curry(BiFunction<A,B,C> f) {
return (A a) -> (B b) -> f.apply(a, b) ;
}
BiFunction<A,B,C> uncurry(Function<A,Function<B,C>> f){
return (A a, B b) -> f.apply(a).apply(b);
}
Function<A,C> compose(Function<B,C> f, Function<A,B> g){
return a -> f.apply(g.apply(a));
}
B foldRight(List<A> a, B z, BiFunction<A,B,B> bf){
if(a.isEmpty()) return z;
else{
return bf.apply(a.get(0), foldRight(tail(a), z, bf));
}
}
List<A> tail(List<A> list) {
if (list.isEmpty()) return null;
else {
List<A> newList = new ArrayList<>(list);
newList.remove(0);
return newList;
}
}
public static void main(String[] args) {
FpJava<Integer, Integer, String> fp = new FpJava<>();
FpJava<String,String,String> fp2 = new FpJava<>();
List<Integer> intList = Arrays.asList(1, 2, 3, 4);
// findFirst
System.out.println("findFirst :"+fp.findFirst(intList, n -> n == 2));
System.out.println("findFirst :"+fp2.findFirst(Arrays.asList("g,a,test".split(",")), n-> n.equals("test")));
System.out.println("findFirst :"+fp2.findFirst(Arrays.asList("g,a,test".split(",")), n-> n.equals("5")));
// isSorted
System.out.println("is true : "+fp.isSorted(intList, (c, d) -> c < d));
System.out.println("is true : "+fp.isSorted(intList, (c, d) -> c < d));
// partial
Function<Integer, String> partial = fp.partial1(1, (b, c) -> String.valueOf("result" + (b + c)));
System.out.println("partial : " + partial.apply(2));
// curry
Function<Integer, Function<Integer, String>> curry = fp.curry((a, b) -> String.valueOf("result : " + (a + b)));
System.out.println("curry result : "+curry.apply(1).apply(2));
// uncurry
BiFunction<Integer, Integer, String> uncurry = fp.uncurry(a -> b -> "uncurry result : " + (a + b));
System.out.println(uncurry.apply(1,2));
// compose (compose 는 뒤의 파라미터를 먼저 실행시킨다. )
Function<Integer, String> compose = fp.compose(b -> "result " + b, (a) -> a + 2);
System.out.println("compose : "+compose.apply(1));
// foldRight
Integer resFoldRight = fp.foldRight(intList, 0, (x, y) -> x + y);
System.out.println("foldRight : "+ resFoldRight);
}
}
// 스칼라책에서의 List 비스무리하게 구현해보려고 하였었다..
public class JavaList<T> {
int sum(List<Integer> l){
if(l.isEmpty()) return 0;
else{
List<Integer> newList = new ArrayList<>(l);
newList.remove(0);
return l.get(0)+sum(newList);
}
}
double product(List<Double> l){
if(l.isEmpty()) return 1.0;
else{
List<Double> newList = new ArrayList<>(l);
newList.remove(0);
if(l.get(0).equals(0)) return 0;
return l.get(0)*product(newList);
}
}
List<T> tail(List<T> list) {
if (list.isEmpty()) return null;
else {
List<T> newList = newList(list);
newList.remove(0);
return newList;
}
}
List<T> newList(List<T> l) { return new ArrayList<>(l);}
List<T> setHead(List<T> list, T elem) {
List<T> newList = tail(list);
newList.add(0, elem);
return newList;
}
List<T> drop(List<T> list, int num) {
if (num == 0) return list;
else {
List<T> tail = tail(list);
return drop(tail, num - 1);
}
}
List<T> dropWhile(List<T> list, Predicate<T> t) {
if (t.test(list.get(0))) {
return dropWhile(tail(list), t);
} else {
return list;
}
}
// 스칼라 자료구조형은 힘들었지만 자바 자료구조형은 상수시간으로 구현가능..할듯?
List<T> init(List<T> list){
if(list.isEmpty()){return list;}
else {
List<T> newList = newList(list);
newList.remove(newList.size()-1);
return newList;
}
}
public static void main(String[] args) {
JavaList<Integer> list = new JavaList<>();
List<Integer> iList = new ArrayList<>(Arrays.asList(1, 2, 3, 4));
List<Double> dList = new ArrayList<>(Arrays.asList(1.0, 2.0, 3.0, 4.0));
// sum
System.out.println("sum : "+list.sum(iList));
// product
System.out.println("product : "+list.product(dList));
// tail
System.out.println("tail " + list.tail(iList));
// setHead
System.out.println("set head :" + list.setHead(iList, 99));
// drop
System.out.println("drop :" + list.drop(iList, 2));
// drop while
System.out.println("drop while : " + list.dropWhile(iList, a -> a < 3));
// init
System.out.println("init :" + list.init(iList));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment