NLinker

anyCommonElements from swift

Sep 7th, 2016
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.94 KB | None | 0 0
  1. /*
  2. func anyCommonElements <T: SequenceType, U: SequenceType where T.Generator.Element: Equatable, T.Generator.Element == U.Generator.Element> (lhs: T, _ rhs: U) -> [T.Generator.Element] {
  3.     var common : [T.Generator.Element] = []
  4.     for lhsItem in lhs {
  5.         for rhsItem in rhs {
  6.             if lhsItem == rhsItem {
  7.                 common.append(lhsItem)
  8.             }
  9.         }
  10.     }
  11.    
  12.    return common
  13. }*/
  14.  
  15. trait Eq[A] {
  16.   def eq(a1: A, a2: A): Boolean
  17. }
  18.  
  19. trait SequenceType[T] {
  20.  
  21. }
  22.  
  23. implicit val constraint = new
  24.  
  25. def anyCommonElements[T <: SequenceType, U <: SequenceType](lhs: T, rhs: U)
  26.                                                            (implicit cs: Constraint[T, U])
  27.                                                            (implicit ev: Eq[T])
  28. :  {
  29.   val common = new ArrayBuffer[T#Generator#Element]
  30.   for {
  31.     li <- lhs
  32.     ri <- rhs
  33.     if ev.eq(li, ri)
  34.   } {
  35.     common += li
  36.   }
  37.   common
  38. }
Add Comment
Please, Sign In to add comment