Skip to content

Instantly share code, notes, and snippets.

def find2[A](x:A, xs:List[A]):Option[A] = {
if(xs.nonEmpty){
if(xs.head == x) Some(xs.head)
else find2(x, xs.tail)
} else {
None
}
}
def find3[A](x:A, xs:List[A]):Option[A] =
xs.headOption match {
@akihiro4chawon
akihiro4chawon / BinTreeIterator.scala
Created July 25, 2011 00:17 — forked from kmizu/BinTreeIterator.scala
Tree traversal comparison: stream vs explicit state stack
object BinTreeIterator {
sealed trait Tree
case class Node(v: Int, l: Tree, r: Tree) extends Tree
case object Leaf extends Tree
def toIterator(node: Tree): Iterator[Int] = {
def toStream(n: Tree): Stream[Int] = n match {
case Node(v, l, r) => v #:: toStream(l) #::: toStream(r)
case Leaf => Stream.empty
}
@akihiro4chawon
akihiro4chawon / bubbleSort.scala
Created November 12, 2011 15:24 — forked from rrgroovy/bubbleSort.groovy
Reinvention of the Wheel Series, "Bubble Sort in Scalaz".
import scalaz._
import Scalaz._
object Bubble {
def bubbleSort[A: Order](xs: List[A]) = xs.unfold[Stream, A] {
_.foldr[Option[(A, List[A])]](none){ (x, zs) =>
zs.fold({case (y, ys) => (x lt y).fold((x, y::ys), (y, x::ys))}, (x, nil[A])).some
}
}
}
@akihiro4chawon
akihiro4chawon / getTerminals.hs
Created February 20, 2012 10:45 — forked from aya-eiya/getTerminals.hs
指定したディレクトリを再帰的に探索して末端のパスのリストを返します。#haskell
module Main where
import Control.Monad
import Control.Monad.Trans
import Control.Monad.Trans.List
import System.Directory
getAllFilesIn :: FilePath -> IO [FilePath]
getAllFilesIn path = runListT $ getAllFilesIn' path
where