Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Functions {
- public static <T,S> Function<T,S> constant(S wynik)throws GenericFunctionsException{
- Function funkcja= new Function<T,S>() {
- @Override
- public int arity() {
- return 0;
- }
- @Override
- public S compute(List<? extends T> args) throws GenericFunctionsException {
- if(args==null || !args.isEmpty())
- throw new GenericFunctionsException();
- return wynik;
- }
- };
- return funkcja;
- }
- public static <T extends S,S> Function<T,S> proj(int n, int k)throws GenericFunctionsException{
- Function funkcja= new Function<T,S>(){
- @Override
- public int arity() throws GenericFunctionsException {
- if(n<=0 || k<0 || k>n)
- throw new GenericFunctionsException();
- return n;
- }
- @Override
- public S compute(List<? extends T> args) throws GenericFunctionsException {
- if(args==null)
- throw new GenericFunctionsException();
- if(args.size()!=n)
- throw new GenericFunctionsException();
- if(args.get(k)==null)
- throw new GenericFunctionsException();
- return args.get(k);
- }
- };
- return funkcja;
- }
- public static <T,S,R> Function<R,S> compose(final Function<? super T,? extends S> outer, final List<? extends Function<? super R,? extends T>> inner )throws GenericFunctionsException {
- Function funkcja= new Function<R,S>() { // ??
- @Override
- public int arity() throws GenericFunctionsException {
- if(outer.arity()!=inner.size())
- throw new GenericFunctionsException();
- if(inner.isEmpty())
- return 0;
- if(inner.contains(null))
- throw new GenericFunctionsException();
- int checkArity=-1;
- for(Function<? super R,? extends T> f : inner){
- if(checkArity==-1)
- checkArity=f.arity();
- if(f.arity()!=checkArity)
- throw new GenericFunctionsException();
- }
- return checkArity;
- }
- @Override
- public Object compute(List args) throws GenericFunctionsException { // tu cos z S
- if(outer.arity()!=inner.size())
- throw new GenericFunctionsException();
- // ArrayList<T> l=new ArrayList();
- ArrayList l=new ArrayList();
- for(Function<? super R,? extends T> f : inner){
- l.add(f.compute(args));
- }
- return outer.compute(l);
- }
- };
- return funkcja;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement