Guest User

Untitled

a guest
Jan 18th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. public interface ReversableList<T> {
  2. // Return a copy of this list but reversed
  3. ReversableList<T> Reverse();
  4.  
  5. // We still need to return the contained type sometimes
  6. T Get(int index)
  7. }
  8.  
  9. public class ListImplementation<T> : ReversableList<T>
  10. ReversableList<T> Reverse();
  11. }
  12.  
  13. var concreteList = new ListImplementation<String>();
  14. ReversableList<T> reversed = concreteList.Reverse();
  15.  
  16. var concreteList = new ListImplementation<String>();
  17. ListImplementation<String> reversed = concreteList.Reverse();
  18.  
  19. trait GenSeqLike[+A, +Repr] ...
  20. def reverse: Repr // <--- NO BODY, means abstract
  21.  
  22. trait SeqLike[+A, +Repr] extends GenSeqLike[A, Repr]
  23. def reverse: Repr
  24. //iterates through this seqence
  25. // and reverses
  26. for (x <- this)
  27. //...etc
  28.  
  29. trait Seq[+A] extends SeqLike[A, Seq[A]] //<-- NOTE here is the type for "Repr"
  30. //no mention of reverse
  31.  
  32. trait LinearSeq[+A] extends Seq[A] with SeqLike[A, Seq[A]] //<-- NOTE type again
  33. // no mention of reverse
  34.  
  35. sealed abstract class List[+A] extends LinearSeq[A]
  36. with LinearSeqOptimized[A, List[A]]
  37.  
  38. override def reverse: List[A] = {
  39. //faster impl without foreach iteration
  40. ...
  41. }
  42.  
  43. public interface ReversableList<L, T> where L : ReversableList<L, T>
  44. {
  45. // Return a copy of this list but reversed
  46. L Reverse();
  47. }
  48.  
  49. public class ListImplementation<T> : ReversableList<ListImplementation<T>, T>
  50. {
  51. public ListImplementation<T> Reverse() { /* code */ }
  52. }
  53.  
  54. public interface IReversableList<T> where T : IReversableList<T>
  55. {
  56. // Return a copy of this list but reversed
  57. T Reverse();
  58. }
  59.  
  60. public class ListImplementation<T> : IReversableList<ListImplementation<T>>
  61. {
  62. public ListImplementation<T> Reverse()
  63. {
  64. return new ListImplementation<T>();
  65. }
  66. }
  67.  
  68. var concreteList = new ListImplementation<string>();
  69. ListImplementation<string> reversed = concreteList.Reverse();
  70.  
  71. var list = new List<string>(){"a","b","c"};
  72. list.Reverse();
Add Comment
Please, Sign In to add comment