PaulGraham,在他的精彩文章中RevengeoftheNerds,声称语言的力量各不相同。他提到了一个很好的练习——编写一个累加器生成器:Wewanttowriteafunctionthatgeneratesaccumulators--afunctionthattakesanumbern,andreturnsafunctionthattakesanothernumberiandreturnsnincrementedbyi.Java中的解决方案是publicclassAccumulator{publicinterfaceInttoint{publicintcall(inti);}
我已经解决了一个Y组合器问题。刚才发现不能递归引用泛型参数。Y=λf.(λx.f(xx))(λx.f(xx))例如:IntUnaryOperatorfact=Y(rec->n->n==0?1:n*rec.applyAsInt(n-1));IntUnaryOperatorY(Functionf){returng(g->f.apply(x->g.apply(g).applyAsInt(x)));}IntUnaryOperatorg(Gg){returng.apply(g);}//v---Iwanttoremovethemiddle-interface`G`interfaceGextends
我有使用Java8新的示例代码streamfunctionality(获取一系列int值1..20,跳过前9个,然后取剩余的10个,每个int值:减一并乘以2)。System.out.println(Arrays.toString(IntStream.rangeClosed(1,20).skip(9).limit(10).map((newIntUnaryOperator(){@OverridepublicintapplyAsInt(intoperand){returnoperand-1;}}).andThen(newIntUnaryOperator(){@Overridepublici
我有使用Java8新的示例代码streamfunctionality(获取一系列int值1..20,跳过前9个,然后取剩余的10个,每个int值:减一并乘以2)。System.out.println(Arrays.toString(IntStream.rangeClosed(1,20).skip(9).limit(10).map((newIntUnaryOperator(){@OverridepublicintapplyAsInt(intoperand){returnoperand-1;}}).andThen(newIntUnaryOperator(){@Overridepublici