001 package fj.function; 002 003 import fj.F; 004 import fj.F2; 005 import static fj.Function.curry; 006 import fj.data.List; 007 import fj.data.Option; 008 import static fj.data.Option.some; 009 import static fj.data.Option.none; 010 import fj.pre.Monoid; 011 import static fj.pre.Semigroup.intAdditionSemigroup; 012 import static fj.pre.Semigroup.intMultiplicationSemigroup; 013 014 import static java.lang.Math.abs; 015 016 /** 017 * Curried functions over Integers. 018 * 019 * @version %build.number%<br> 020 * <ul> 021 * <li>$LastChangedRevision: 166 $</li> 022 * <li>$LastChangedDate: 2009-06-04 02:15:48 +1000 (Thu, 04 Jun 2009) $</li> 023 * </ul> 024 */ 025 public final class Integers { 026 private Integers() { 027 throw new UnsupportedOperationException(); 028 } 029 030 /** 031 * Curried Integer addition. 032 */ 033 public static final F<Integer, F<Integer, Integer>> add = intAdditionSemigroup.sum(); 034 035 /** 036 * Curried Integer multiplication. 037 */ 038 public static final F<Integer, F<Integer, Integer>> multiply = intMultiplicationSemigroup.sum(); 039 040 /** 041 * Curried Integer subtraction. 042 */ 043 public static final F<Integer, F<Integer, Integer>> subtract = curry(new F2<Integer, Integer, Integer>() { 044 public Integer f(final Integer x, final Integer y) { 045 return x - y; 046 } 047 }); 048 049 /** 050 * Negation. 051 */ 052 public static final F<Integer, Integer> negate = new F<Integer, Integer>() { 053 public Integer f(final Integer x) { 054 return x * -1; 055 } 056 }; 057 058 /** 059 * Absolute value. 060 */ 061 public static final F<Integer, Integer> abs = new F<Integer, Integer>() { 062 public Integer f(final Integer x) { 063 return abs(x); 064 } 065 }; 066 067 /** 068 * Remainder. 069 */ 070 public static final F<Integer, F<Integer, Integer>> remainder = curry(new F2<Integer, Integer, Integer>() { 071 public Integer f(final Integer a, final Integer b) { 072 return a % b; 073 } 074 }); 075 076 /** 077 * Power. 078 */ 079 public static final F<Integer, F<Integer, Integer>> power = curry(new F2<Integer, Integer, Integer>() { 080 public Integer f(final Integer a, final Integer b) { 081 return (int) StrictMath.pow(a, b); 082 } 083 }); 084 085 /** 086 * Evenness. 087 */ 088 public static final F<Integer, Boolean> even = new F<Integer, Boolean>() { 089 public Boolean f(final Integer i) { 090 return i % 2 == 0; 091 } 092 }; 093 094 /** 095 * Sums a list of integers. 096 * 097 * @param ints A list of integers to sum. 098 * @return The sum of the integers in the list. 099 */ 100 public static int sum(final List<Integer> ints) { 101 return Monoid.intAdditionMonoid.sumLeft(ints); 102 } 103 104 /** 105 * Returns the product of a list of integers. 106 * 107 * @param ints A list of integers to multiply together. 108 * @return The product of the integers in the list. 109 */ 110 public static int product(final List<Integer> ints) { 111 return Monoid.intMultiplicationMonoid.sumLeft(ints); 112 } 113 114 /** 115 * A function that converts strings to integers. 116 * 117 * @return A function that converts strings to integers. 118 */ 119 public static F<String, Option<Integer>> fromString() { 120 return new F<String, Option<Integer>>() { 121 public Option<Integer> f(final String s) { 122 try { return some(Integer.valueOf(s)); } 123 catch (final NumberFormatException ignored) { 124 return none(); 125 } 126 } 127 }; 128 } 129 130 }