Skip to content

Instantly share code, notes, and snippets.

@kxbmap
Created January 10, 2012 12:30
Show Gist options
  • Save kxbmap/1588803 to your computer and use it in GitHub Desktop.
Save kxbmap/1588803 to your computer and use it in GitHub Desktop.
Scala標準バージョンとScalazバージョン
val ACGT = "ACGT".to[Stream]
@annotation.tailrec
def solve0(n: Int, ss: Stream[List[Char]]): Stream[List[Char]] =
if (n == 0) ss
else solve0(n - 1, ACGT.flatMap(c => ss.map(c :: _)))
def solve(n: Int): Stream[String] =
solve0(n, Stream(Nil)) collect {
case cs if cs.containsSlice("AAG") => cs.mkString
}
import scalaz._, Scalaz._
val ACGT = "ACGT".to[Stream]
def solve(n: Int): Stream[String] =
ACGT.replicateM(n) collect {
case cs if cs.containsSlice("AAG") => cs.mkString
}
scala> solve(5) foreach println
AAAAG
AAAGA
AAAGC
AAAGG
AAAGT
AAGAA
AAGAC
AAGAG
AAGAT
AAGCA
AAGCC
AAGCG
AAGCT
AAGGA
AAGGC
AAGGG
AAGGT
AAGTA
AAGTC
AAGTG
AAGTT
ACAAG
AGAAG
ATAAG
CAAAG
CAAGA
CAAGC
CAAGG
CAAGT
CCAAG
CGAAG
CTAAG
GAAAG
GAAGA
GAAGC
GAAGG
GAAGT
GCAAG
GGAAG
GTAAG
TAAAG
TAAGA
TAAGC
TAAGG
TAAGT
TCAAG
TGAAG
TTAAG
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment