Rubyã®injectã¿ãããªã¡ã½ãããJavaã§ã©ãæ¸ã
ããããªãè¦ãããªã誰ãæãã¦ãã ããã
ç·ç§°åã使ãã°ããã¡ãããããããï¼ï¼ï¼ãªããããã
import static org.junit.Assert.assertEquals; import org.junit.Test; public class Inject { interface Proc { int execute(final int n, final int m); } public int inject(int[] input, int init, Proc proc) { int result = init; for (int i : input) { result = proc.execute(result, i); } return result; } @Test public void testInject() throws Exception { assertEquals( 55, inject(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, 0, new Proc() { @Override public int execute(int n, int m) { return n + m; } })); assertEquals( 3628800, inject(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, 1, new Proc() { @Override public int execute(int n, int m) { return n * m; } })); } }
ãªãã©ã¤ã³ã©ãæ¸ãã®æ親ä¼ã§ããJavaã«ã¯é¢æ°ãªãã¸ã§ã¯ãããªããããªãã§ããã¼ãã£ã¦è¨ããããã©ããããã«ä¸è¨ã¯ã³ã¬ã¸ã£ãã¤æããããã