<commit_before>package vault

import java.sql.ResultSet
import scalaz._, Scalaz._

case class FromDb[+A](private val run: (Int, Row) => DbValue[(Int, Option[A])]) {
  def map[B](f: A => B): FromDb[B] =
    flatMap(a => FromDb.value(f(a)))

  def flatMap[B](f: A => FromDb[B]): FromDb[B] =
    FromDb((n, r) => run(n, r).fold(DbValue.fail, {
      case (nn, None) => DbValue.ok((nn, none))
      case (nn, Some(a)) => f(a).run(nn, r)
    }))

  def perform(r: Row): DbValue[A] =
    run(1, r).fold(DbValue.fail, {
      case (n, None) => DbValue.dbnull[A](n - 1)
      case (_, Some(a)) => DbValue.ok(a)
    })
}

object FromDb {
  def get[A: FromDb] =
    implicitly[FromDb[A]]

  def value[A](a: A): FromDb[A] =
    FromDb((n, _) => DbValue.ok[(Int, Option[A])]((n, Some(a))))

  private def fromDb[A](run: (Int, Row) => DbValue[Option[A]]) =
    FromDb((n, r) => run(n, r).map(v => (n + 1, v)))

  private def fromDbCell[A](run: Cell => DbValue[Option[A]]) =
    fromDb((n, r) => run(r.toCell(n)))

  implicit def FromDbInt: FromDb[Int] =
    fromDbCell(_.int)

  implicit def FromDbLong: FromDb[Long] =
    fromDbCell(_.long)

  implicit def FromDbString: FromDb[String] =
    fromDbCell(_.string)

  implicit def FromDbBoolean: FromDb[Boolean] =
    fromDbCell(_.boolean)

  implicit def FromDbTuple2[A: FromDb, B: FromDb]: FromDb[(A, B)] = for {
    a <- get[A]
    b <- get[B]
  } yield (a, b)

  implicit def FromDbTuple3[A: FromDb, B: FromDb, C: FromDb]: FromDb[(A, B, C)] = for {
    a <- get[A]
    b <- get[B]
    c <- get[C]
  } yield (a, b, c)

  implicit def FromDbMonad: Monad[FromDb] = new Monad[FromDb] {
    def point[A](a: => A) = value(a)
    def bind[A, B](m: FromDb[A])(f: A => FromDb[B]) = m flatMap f
  }<commit_msg>Handle option.<commit_after>package vault

import java.sql.ResultSet
import scalaz._, Scalaz._

case class FromDb[+A](private val run: (Int, Row) => DbValue[(Int, Option[A])]) {
  def map[B](f: A => B): FromDb[B] =
    flatMap(a => FromDb.value(f(a)))

  def flatMap[B](f: A => FromDb[B]): FromDb[B] =
    FromDb((n, r) => run(n, r).fold(DbValue.fail, {
      case (nn, None) => DbValue.ok((nn, none))
      case (nn, Some(a)) => f(a).run(nn, r)
    }))

  def perform(r: Row): DbValue[A] =
    run(1, r).fold(DbValue.fail, {
      case (n, None) => DbValue.dbnull[A](n - 1)
      case (_, Some(a)) => DbValue.ok(a)
    })
}

object FromDb {
  def get[A: FromDb] =
    implicitly[FromDb[A]]

  def value[A](a: A): FromDb[A] =
    FromDb((n, _) => DbValue.ok[(Int, Option[A])]((n, Some(a))))

  private def fromDb[A](run: (Int, Row) => DbValue[Option[A]]) =
    FromDb((n, r) => run(n, r).map(v => (n + 1, v)))

  private def fromDbCell[A](run: Cell => DbValue[Option[A]]) =
    fromDb((n, r) => run(r.toCell(n)))

  implicit def FromDbOption[A: FromDb]: FromDb[Option[A]] =
    FromDb((n, r) => get[A].run(n, r) map {
      case (nn, None) => (nn, None)
      case (nn, Some(a)) => (nn, Some(Some(a)))
    })

  implicit def FromDbInt: FromDb[Int] =
    fromDbCell(_.int)

  implicit def FromDbLong: FromDb[Long] =
    fromDbCell(_.long)

  implicit def FromDbString: FromDb[String] =
    fromDbCell(_.string)

  implicit def FromDbBoolean: FromDb[Boolean] =
    fromDbCell(_.boolean)

  implicit def FromDbTuple2[A: FromDb, B: FromDb]: FromDb[(A, B)] = for {
    a <- get[A]
    b <- get[B]
  } yield (a, b)

  implicit def FromDbTuple3[A: FromDb, B: FromDb, C: FromDb]: FromDb[(A, B, C)] = for {
    a <- get[A]
    b <- get[B]
    c <- get[C]
  } yield (a, b, c)

  implicit def FromDbMonad: Monad[FromDb] = new Monad[FromDb] {
    def point[A](a: => A) = value(a)
    def bind[A, B](m: FromDb[A])(f: A => FromDb[B]) = m flatMap f
  }
{'fraction_non_alphanumeric': 0.2075061191188469, 'fraction_numerical': 0.0027196083763937995, 'mean_word_length': 3.3017543859649123, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6698957', 'n_tokens_mistral': 1571, 'n_tokens_neox': 1531, 'n_words': 357}