Advertisement
Guest User

Untitled

a guest
May 3rd, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Functions {
  4. public static <T,S> Function<T,S> constant(S wynik)throws GenericFunctionsException{
  5. Function funkcja= new Function<T,S>() {
  6. @Override
  7. public int arity() {
  8. return 0;
  9. }
  10.  
  11. @Override
  12. public S compute(List<? extends T> args) throws GenericFunctionsException {
  13. if(args==null || !args.isEmpty())
  14. throw new GenericFunctionsException();
  15. return wynik;
  16. }
  17. };
  18. return funkcja;
  19.  
  20. }
  21.  
  22. public static <T extends S,S> Function<T,S> proj(int n, int k)throws GenericFunctionsException{
  23. Function funkcja= new Function<T,S>(){
  24. @Override
  25. public int arity() throws GenericFunctionsException {
  26. if(n<=0 || k<0 || k>n)
  27. throw new GenericFunctionsException();
  28. return n;
  29. }
  30.  
  31. @Override
  32. public S compute(List<? extends T> args) throws GenericFunctionsException {
  33. if(args==null)
  34. throw new GenericFunctionsException();
  35. if(args.size()!=n)
  36. throw new GenericFunctionsException();
  37. if(args.get(k)==null)
  38. throw new GenericFunctionsException();
  39. return args.get(k);
  40. }
  41. };
  42. return funkcja;
  43.  
  44. }
  45.  
  46. 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 {
  47. Function funkcja= new Function<R,S>() { // ??
  48.  
  49. @Override
  50. public int arity() throws GenericFunctionsException {
  51. if(outer.arity()!=inner.size())
  52. throw new GenericFunctionsException();
  53. if(inner.isEmpty())
  54. return 0;
  55. if(inner.contains(null))
  56. throw new GenericFunctionsException();
  57. int checkArity=-1;
  58. for(Function<? super R,? extends T> f : inner){
  59. if(checkArity==-1)
  60. checkArity=f.arity();
  61. if(f.arity()!=checkArity)
  62. throw new GenericFunctionsException();
  63. }
  64. return checkArity;
  65. }
  66.  
  67. @Override
  68. public Object compute(List args) throws GenericFunctionsException { // tu cos z S
  69. if(outer.arity()!=inner.size())
  70. throw new GenericFunctionsException();
  71. // ArrayList<T> l=new ArrayList();
  72. ArrayList l=new ArrayList();
  73. for(Function<? super R,? extends T> f : inner){
  74. l.add(f.compute(args));
  75. }
  76. return outer.compute(l);
  77. }
  78. };
  79. return funkcja;
  80. }
  81.  
  82.  
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement