Skip to content

Instantly share code, notes, and snippets.

@weidagang
Created July 1, 2014 10:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save weidagang/b1e45cc4cd635b88ca4c to your computer and use it in GitHub Desktop.
Save weidagang/b1e45cc4cd635b88ca4c to your computer and use it in GitHub Desktop.
HackerRank: Filter Array
package filter.array
object Main {
def filter[T](arr: List[T], cond: T => Boolean): List[T] = arr match {
case x :: xs => if (cond(x)) x :: filter(xs, cond) else filter(xs, cond)
case _ => List[T]()
}
def f(delim: Int,arr: List[Int]): List[Int] = filter(arr, (x: Int) => x < delim)
def main(args: Array[String]) {
val filtered = f(3, List(1, 2, 3, 4, 5));
println(filtered);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment