Skip to content

Instantly share code, notes, and snippets.

@blast-hardcheese
Created October 14, 2015 03:18
Show Gist options
  • Save blast-hardcheese/93c51fe2a2faa8666264 to your computer and use it in GitHub Desktop.
Save blast-hardcheese/93c51fe2a2faa8666264 to your computer and use it in GitHub Desktop.
def split[A, B](f: A => A => C)(x: A): C = f(x)(x)
def swap[A, B, C](f: A => B => C): B => A => C = (b: B) => (a: A) => f(a)(b)
def filter[A](as: List[A])(f: A => Boolean): List[A] = foldRight2(as, List.empty[A])(uncurried[A, List[A], List[A]](split(f andThen (if(_) (Cons.apply[A] _).curried else swap(const[List[A], A] _))) _))
def filterAnnotated[A](as: List[A])(f: A => Boolean): List[A] = {
val prepend: A => List[A] => List[A] = (Cons.apply[A] _).curried
val ignore: A => List[A] => List[A] = swap(const[List[A], A] _)
val getApply: A => A => List[A] => List[A] = f andThen (if(_) prepend else ignore)
val replicated: A => List[A] => List[A] = split(getApply) _
val foldBody: (A, List[A]) => List[A] = uncurried(replicated)
foldRight2(as, List.empty[A])(foldBody)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment