Last active
December 20, 2015 22:09
-
-
Save yareally/6203227 to your computer and use it in GitHub Desktop.
Ugly fake Java Lambdas
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
package helpers; | |
import java.util.ArrayList; | |
import java.util.List; | |
/** | |
* @author Wes Lanning | |
* @version 2013-08-01 | |
*/ | |
public class FakeLambda | |
{ | |
public interface ƛƛ<A, B> | |
{ | |
B apply(A a); | |
} | |
public interface ƛ<A> | |
{ | |
void apply(A a); | |
} | |
public static <A> List<A> filter(Iterable<A> data, ƛƛ<A, Boolean> predicate) | |
{ | |
List<A> result = new ArrayList<>(); | |
for (A a : data) { | |
if (predicate.apply(a)) result.add(a); | |
} | |
return result; | |
} | |
public static <A> List<A> map(Iterable<A> data, ƛƛ<A, A> funct) { | |
List<A> result = new ArrayList<>(); | |
for (A a : data) { | |
result.add(funct.apply(a)); | |
} | |
return result; | |
} | |
public static <A> void foreach(Iterable<A> data, ƛ<A> funct) { | |
for (A a : data) { | |
funct.apply(a); | |
} | |
} | |
public static List<String> filterData(List<String> data) | |
{ | |
return filter(data, new ƛƛ<String, Boolean>() { | |
public Boolean apply(String elem) { | |
return !elem.isEmpty(); | |
} | |
}); | |
} | |
public static List<String> mapData(List<String> data) | |
{ | |
return map(data, new ƛƛ<String, String>() { | |
public String apply(String elem) { | |
return String.format("-- %s --", elem); | |
} | |
}); | |
} | |
public static void formatData(List<String> data) | |
{ | |
foreach(data, new ƛ<String>() { | |
public void apply(String elem) { | |
System.out.format("** %s **", elem); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment