fork(1) download
  1. import scala.collection.generic.CanBuildFrom
  2. import scala.math.Ordering
  3. import collection.{TraversableLike, SeqLike}
  4. import collection.immutable.BitSet
  5.  
  6. class QuickSort[Coll](a: Coll) {
  7. //should be able to sort only something with defined order (someting like a Seq)
  8. def quickSort[T](implicit ev0: Coll => SeqLike[T, Coll],
  9. cbf: CanBuildFrom[Coll, T, Coll],
  10. n: Ordering[T]): Coll = {
  11. quickSortAnything(ev0, cbf, n)
  12. }
  13.  
  14. //we can even sort a Set, if we really want to
  15. def quickSortAnything[T](implicit ev0: Coll => TraversableLike[T, Coll],
  16. cbf: CanBuildFrom[Coll, T, Coll],
  17. n: Ordering[T]): Coll = {
  18. import n._
  19. if (a.size < 2) {
  20. a
  21. } else {
  22. // We pick the first value for the pivot.
  23. val pivot = a.head
  24. val (lower, tmp) = a.partition(_ < pivot)
  25. val (upper, same) = tmp.partition(_ > pivot)
  26. val b = cbf()
  27. b.sizeHint(a.size)
  28. b ++= new QuickSort(lower).quickSortAnything
  29. b ++= same
  30. b ++= new QuickSort(upper).quickSortAnything
  31. b.result
  32. }
  33. }
  34. }
  35.  
  36. class FilterMap[Repr](a: Repr) {
  37. def filterMap[A, B, That](f: A => Option[B])(implicit ev0: Repr => TraversableLike[A, Repr],
  38. cbf: CanBuildFrom[Repr, B, That]): That = {
  39. a.flatMap(e => f(e).toSeq)
  40. }
  41. }
  42.  
  43. class FilterMapFixed[A, Repr <% TraversableLike[A, Repr]](a: Repr) {
  44. def filterMap2[B, That](f: A => Option[B])(implicit cbf: CanBuildFrom[Repr, B, That]): That = {
  45. a.flatMap(e => f(e).toSeq)
  46. }
  47. }
  48.  
  49. object MyEnhancements {
  50. implicit def toQS[Coll](a: Coll) = new QuickSort(a)
  51. implicit def toFM[Coll](a: Coll) = new FilterMap(a)
  52. implicit def toFM2[A, Repr <% TraversableLike[A, Repr]](a: Repr) = new FilterMapFixed(a)
  53. }
  54.  
  55. object Main extends Application {
  56.  
  57. import MyEnhancements._
  58.  
  59. println("qwe".quickSort)
  60. println(Array(2, 0).quickSort)
  61. println(Seq(2, 0).quickSort)
  62. //not very useful to sort a set, but just as a demonstration
  63. println(BitSet(2, 0).quickSortAnything)
  64.  
  65. //need to hint type inferencer,
  66. //probably will be able to overcome after https://i...content-available-to-author-only...g.org/browse/SI-4699 and
  67. // related issues are fixed (by moving ev0 parameter from filterMap to toFM), see toFM2
  68. println("qwe".filterMap((c: Char) => Some(c.toInt)))
  69. println("qwe".filterMap((c: Char) => Some(c)))
  70. println(Array(2, 0).filterMap((c: Int) => Some(c.toInt)))
  71. println(Seq(2, 0).filterMap((c: Int) => if (c < 2) Some(c + "!") else None))
  72. def test(i:Int) = Option(i)
  73. println(BitSet(2,0).filterMap(test))
  74.  
  75. println(toFM2("qwe").filterMap2(c => Some(c)))
  76. println(toFM2(Array(2, 0)).filterMap2(c => Some(c.toInt)))
  77. //No implicit view available from java.lang.String => scala.collection.TraversableLike[A,java.lang.String]. :(
  78. //println("qwe".filterMap2(c => Some(c)))
  79. }
  80.  
Success #stdin #stdout 0.28s 211776KB
stdin
Standard input is empty
stdout
eqw
[I@c3c749
List(0, 2)
BitSet(0, 2)
Vector(113, 119, 101)
qwe
[I@1d8957f
List(0!)
BitSet(0, 2)
qwe
[I@1f9dc36