harryh RSS

I don't really like tumblr much, but all my friends seem to use it so I'm kinda stuck. I miss the era of LiveJournal.

Archive

Aug
20th
Thu
permalink

functional programming ftw

Simple, but fun:

def partition[T](items: List[T], p: (T, T) => Boolean): List[List[T]] = {
  items.foldRight[List[List[T]]](Nil)((item: T, items: List[List[T]]) => items match {
    case (first :: rest) :: last if p (first, item) =>
      (List(item)) :: (first :: rest) :: last
    case (first :: rest) :: last =>
      (item :: first :: rest) :: last
    case _ => List(List(item))
  })
}

If you understand what this does, or know someone who does, I’m hiring. Send me an e-mail.