Created
November 14, 2013 22:04
-
-
Save mmacedo/7475112 to your computer and use it in GitHub Desktop.
Functional Java
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
public interface Func<TReturn, TArg0> { | |
public TReturn call(TArg0 arg0); | |
} |
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
public interface Func2<TReturn, TArg0, TArg1> { | |
public TReturn call(TArg0 arg0, TArg1 arg1); | |
} |
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
import java.util.*; | |
public class Iter { | |
public static <T> Iterable<T> filter(Collection<T> list, Predicate<T> predicate) { | |
Collection<T> filtered = new ArrayList<T>(); | |
for (T item : list) { | |
if (predicate.call(item)) { | |
filtered.add(item); | |
} | |
} | |
return filtered; | |
} | |
public static <TReturn, TItem> Iterable<TReturn> map(Collection<TItem> list, Func<TReturn, TItem> func) { | |
Collection<TReturn> mapped = new ArrayList<TReturn>(); | |
for (TItem item : list) { | |
mapped.add(func.call(item)); | |
} | |
return mapped; | |
} | |
public static <TReturn, TItem> TReturn reduce(Collection<TItem> list, TReturn initial, Func2<TReturn, TReturn, TItem> func) { | |
TReturn acc = initial; | |
for (TItem item : list) { | |
acc = func.call(acc, item); | |
} | |
return acc; | |
} | |
} |
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
public interface Predicate<T> { | |
public boolean call(T object); | |
} |
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
import java.util.*; | |
public class X { | |
public static void main(String[] args) { | |
List<Double> grades = Arrays.asList(1.0, 1.0, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.0); | |
System.out.println("All grades:"); | |
for (double grade : grades) { | |
System.out.println(grade); | |
} | |
System.out.println(); | |
System.out.println("Only >= 6.0:"); | |
Iterable<Double> gradesPassed = Iter.filter(grades, new Predicate<Double>() { | |
public boolean call(Double grade) { | |
return grade > 6.0; | |
} | |
}); | |
for (double grade : gradesPassed) { | |
System.out.println(grade); | |
} | |
System.out.println(); | |
System.out.println("Passed:"); | |
Iterable<String> gradesPassed2 = Iter.map(grades, new Func<String, Double>() { | |
public String call(Double grade) { | |
if (grade > 6.0) { | |
return String.format("Passed (%.1f)", grade); | |
} else { | |
return String.format("Failed (%.1f)", grade); | |
} | |
} | |
}); | |
for (String grade : gradesPassed2) { | |
System.out.println(grade); | |
} | |
System.out.println(); | |
System.out.println("Average:"); | |
Double sum = Iter.reduce(grades, 0d, new Func2<Double, Double, Double>() { | |
public Double call(Double sum, Double grade) { | |
return sum + grade; | |
} | |
}); | |
System.out.format("Arithmeric average.: %.1f", sum / grades.size()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment