Created
March 28, 2010 13:26
-
-
Save vladimirdolzhenko/346755 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ru.dolzhenko.lambda.functions.LambdaFunctions | |
package ru.dolzhenko.lambda.functions; | |
import java.math.BigDecimal; | |
import ru.dolzhenko.lambda.iterators.Pair; | |
import com.google.common.base.Function; | |
/** | |
* LambdaFunctions | |
* | |
* @author Vladimir Dolzhenko, vladimir.dolzhenko@gmail.com | |
*/ | |
public final class LambdaFunctions { | |
private LambdaFunctions(){ | |
// LambdaFunctions is utility class | |
} | |
public static Function<BigDecimal, BigDecimal> inc(){ | |
return new Function<BigDecimal, BigDecimal>() { | |
public BigDecimal apply(final BigDecimal from) { | |
return from.add(BigDecimal.ONE); | |
} | |
}; | |
} | |
public static Function<BigDecimal, BigDecimal> dec(){ | |
return new Function<BigDecimal, BigDecimal>() { | |
public BigDecimal apply(BigDecimal from) { | |
return from.subtract(BigDecimal.ONE); | |
} | |
}; | |
} | |
public static Function<Pair<BigDecimal, BigDecimal>, BigDecimal> sum(){ | |
return new Function<Pair<BigDecimal, BigDecimal>, BigDecimal>() { | |
public BigDecimal apply(final Pair<BigDecimal, BigDecimal> from) { | |
return from.first().add(from.second()); | |
} | |
}; | |
} | |
public static Function<Pair<BigDecimal, BigDecimal>, BigDecimal> sub(){ | |
return new Function<Pair<BigDecimal, BigDecimal>, BigDecimal>() { | |
public BigDecimal apply(Pair<BigDecimal, BigDecimal> from) { | |
return from.first().subtract(from.second()); | |
} | |
}; | |
} | |
public static Function<Pair<BigDecimal, BigDecimal>, BigDecimal> multiply(){ | |
return new Function<Pair<BigDecimal, BigDecimal>, BigDecimal>() { | |
public BigDecimal apply(final Pair<BigDecimal, BigDecimal> from) { | |
return from.first().multiply(from.second()); | |
} | |
}; | |
} | |
public static Function<Pair<BigDecimal, BigDecimal>, BigDecimal> divide(){ | |
return new Function<Pair<BigDecimal, BigDecimal>, BigDecimal>() { | |
public BigDecimal apply(final Pair<BigDecimal, BigDecimal> from) { | |
return from.first().divide(from.second()); | |
} | |
}; | |
} | |
public static Function<Pair<BigDecimal, BigDecimal>, BigDecimal> mod(){ | |
return new Function<Pair<BigDecimal, BigDecimal>, BigDecimal>() { | |
public BigDecimal apply(final Pair<BigDecimal, BigDecimal> from) { | |
return from.first().remainder(from.second()); | |
} | |
}; | |
} | |
public static Function<BigDecimal, BigDecimal> mod(final BigDecimal div){ | |
return new Function<BigDecimal, BigDecimal>() { | |
public BigDecimal apply(final BigDecimal from) { | |
return from.remainder(div); | |
} | |
}; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ru.dolzhenko.lambda.iterators.LambdaIterators | |
package ru.dolzhenko.lambda.iterators; | |
import static com.google.common.base.Preconditions.checkNotNull; | |
import java.math.BigDecimal; | |
import java.util.Iterator; | |
import com.google.common.base.Function; | |
import com.google.common.base.Predicate; | |
import com.google.common.collect.AbstractIterator; | |
import com.google.common.collect.Iterators; | |
/** | |
* LambdaIterators | |
* | |
* @author Vladimir Dolzhenko, vladimir.dolzhenko@gmail.com | |
*/ | |
public final class LambdaIterators { | |
private LambdaIterators (){ | |
// nothing | |
} | |
/** | |
* Returns a lazy seq of nums from start (inclusive) to end (exclusive), by step | |
* where start defaults to 0 and step to 1. | |
* | |
* @param end | |
* @return | |
*/ | |
public static Iterator<BigDecimal> range(final BigDecimal end){ | |
return range(BigDecimal.ZERO, end, BigDecimal.ONE); | |
} | |
public static Iterator<BigDecimal> range(final int end){ | |
return range(BigDecimal.ZERO, new BigDecimal(end), BigDecimal.ONE); | |
} | |
/** | |
* Returns a lazy seq of nums from start (inclusive) to end (exclusive), by step | |
* where step defaults to 1. | |
* | |
* @param start | |
* @param end | |
* @return | |
*/ | |
public static Iterator<BigDecimal> range(final BigDecimal start, final BigDecimal end){ | |
return range(start, end, BigDecimal.ONE); | |
} | |
public static Iterator<BigDecimal> range(final int start, final int end){ | |
return range(new BigDecimal(start), new BigDecimal(end), BigDecimal.ONE); | |
} | |
/** | |
* Returns a lazy seq of nums from start (inclusive) to end (exclusive), by step | |
* @param start | |
* @param end | |
* @param step | |
* @return | |
*/ | |
public static Iterator<BigDecimal> range(final BigDecimal start, final BigDecimal end, final BigDecimal step){ | |
return new AbstractIterator<BigDecimal>(){ | |
private BigDecimal index = start; | |
@Override | |
protected BigDecimal computeNext() { | |
if (this.index.compareTo(end) < 0) { | |
final BigDecimal v = this.index; | |
this.index = this.index.add(step); | |
return v; | |
} | |
return endOfData(); | |
} | |
}; | |
} | |
public static Iterator<BigDecimal> range(final int start, final int end, final int step){ | |
return range(new BigDecimal(start), new BigDecimal(end), new BigDecimal(step)); | |
} | |
/** | |
* Returns a lazy sequence of the first n items in coll, or all items if there are fewer than n. | |
* @param <T> | |
* @param n | |
* @param coll | |
* @return | |
*/ | |
public static <T> Iterator<T> take(final int n, final Iterator<T> coll){ | |
checkNotNull(coll); | |
return new AbstractIterator<T>(){ | |
private int pos; | |
@Override | |
protected T computeNext() { | |
if (coll.hasNext() && this.pos < n) { | |
this.pos++; | |
return coll.next(); | |
} | |
return endOfData(); | |
} | |
}; | |
} | |
/** | |
* Returns a lazy sequence of successive items from coll while (predicate item) returns true. | |
* predicate must be free of side-effects. | |
* @param <T> | |
* @param predicate | |
* @param coll | |
* @return | |
*/ | |
public static <T> Iterator<T> takeWhile(final Predicate<T> predicate, final Iterator<T> coll) { | |
checkNotNull(predicate); | |
checkNotNull(coll); | |
return new AbstractIterator<T>(){ | |
@Override | |
protected T computeNext() { | |
if (coll.hasNext()) { | |
final T element = coll.next(); | |
if (predicate.apply(element)) { | |
return element; | |
} | |
} | |
return endOfData(); | |
} | |
}; | |
} | |
/** | |
* Iterate - a lazy sequence of x, (f x), (f (f x)) etc. f must be free of side-effects | |
* | |
* @param <T> | |
* @param f | |
* @param x | |
* @return | |
*/ | |
public static <T> Iterator<T> iterate(final Function<T , T> f, final T x){ | |
checkNotNull(f); | |
checkNotNull(x); | |
return new AbstractIterator<T>() { | |
private T value; | |
@Override | |
protected T computeNext() { | |
this.value = this.value != null ? f.apply(this.value) : x; | |
return this.value; | |
} | |
}; | |
} | |
/** | |
* Returns the first item in the collection. | |
* Calls seq on its argument. If coll is nil, returns nil. | |
* | |
* @param <T> | |
* @param coll | |
* @return | |
*/ | |
public static <T> Iterator<T> first(final Iterator<T> coll){ | |
return Iterators.singletonIterator(coll != null && coll.hasNext() ? coll.next() : null); | |
} | |
/** | |
* Returns a possibly empty seq of the items after the first. | |
* Calls seq on its argument. | |
* | |
* @param <T> | |
* @param coll | |
* @return | |
*/ | |
public static <T> Iterator<T> rest(final Iterator<T> coll){ | |
if (coll.hasNext()){ | |
coll.next(); | |
} | |
return coll; | |
} | |
/** | |
* reduce function | |
* @param <T> | |
* @param f | |
* @param coll | |
* @return | |
*/ | |
public static <T> T reduce(final Function<Pair<T, T>, T> f, final Iterator<T> coll){ | |
T value = null; | |
boolean first = true; | |
while(coll.hasNext()){ | |
final T next = coll.next(); | |
if (first){ | |
value = next; | |
first = false; | |
} else { | |
final Pair<T, T> pair = new PairImpl<T, T>(value, next); | |
value = f.apply(pair); | |
} | |
} | |
return value; | |
} | |
public static final class PairImpl<K, V> implements Pair<K, V> { | |
private final K first; | |
private final V second; | |
public PairImpl(final K first, final V second) { | |
this.first = first; | |
this.second = second; | |
} | |
@Override | |
public K first() { | |
return this.first; | |
} | |
@Override | |
public V second() { | |
return this.second; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ru.dolzhenko.lambda.functions.LambdaPredicates | |
package ru.dolzhenko.lambda.functions; | |
import java.math.BigDecimal; | |
import com.google.common.base.Function; | |
import com.google.common.base.Predicate; | |
/** | |
* LambdaPredicates | |
* | |
* @author Vladimir Dolzhenko, vladimir.dolzhenko@gmail.com | |
*/ | |
public final class LambdaPredicates { | |
private static final BigDecimal TWO = new BigDecimal(2); | |
private LambdaPredicates(){ | |
// LambdaPredicates is utility class | |
} | |
public static Predicate<BigDecimal> zero(){ | |
return new Predicate<BigDecimal>() { | |
@Override | |
public boolean apply(BigDecimal input) { | |
return input.equals(BigDecimal.ZERO); | |
} | |
}; | |
} | |
public static Predicate<BigDecimal> zero(final Function<BigDecimal, BigDecimal> f){ | |
return new Predicate<BigDecimal>() { | |
@Override | |
public boolean apply(BigDecimal input) { | |
return f.apply(input).equals(BigDecimal.ZERO); | |
} | |
}; | |
} | |
public static Predicate<BigDecimal> even(){ | |
return new Predicate<BigDecimal>() { | |
@Override | |
public boolean apply(BigDecimal input) { | |
return input.remainder(TWO).equals(BigDecimal.ZERO); | |
} | |
}; | |
} | |
public static Predicate<BigDecimal> even(final Function<BigDecimal, BigDecimal> f){ | |
return new Predicate<BigDecimal>() { | |
@Override | |
public boolean apply(BigDecimal input) { | |
return f.apply(input).remainder(TWO).equals(BigDecimal.ZERO); | |
} | |
}; | |
} | |
public static Predicate<BigDecimal> odd(){ | |
return new Predicate<BigDecimal>() { | |
@Override | |
public boolean apply(BigDecimal input) { | |
return !input.remainder(TWO).equals(BigDecimal.ZERO); | |
} | |
}; | |
} | |
public static Predicate<BigDecimal> odd(final Function<BigDecimal, BigDecimal> f){ | |
return new Predicate<BigDecimal>() { | |
@Override | |
public boolean apply(BigDecimal input) { | |
return !f.apply(input).remainder(TWO).equals(BigDecimal.ZERO); | |
} | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment