<commit_before>package fpscala.chapter2

/**
 * Created by sajit on 5/1/15.
 */
object Chapter2 {

  //@annotation.tailrec
  /**
   * This is not a tailrec function as we are doing some computation. Hence compiler will throw an error
   * @param n
   * @return
   */
  def factorial(n:Int):Int = {
    n match {
      case 0 => 1
      case 1 => 1
      case _ => n * factorial(n-1)
    }
  }

  /**
   * tail_factorial is not a recursive call. So no sense in making tail rec
   * @param n
   * @return
   */
  def tail_factorial(n:Int):Int = {
    @annotation.tailrec
    def do_tail_factorial(n:Int,acc:Int):Int = {
      n match {
        case 0 => acc
        case 1 => acc
        case _ => do_tail_factorial(n-1,n*acc)
      }
    }
    do_tail_factorial(n,1)
  }

  def fibo(n:Int):Int = {
    @annotation.tailrec
    def do_fibo(n:Int,a:Int,b:Int):Int = {
      n match {
        case x if x<=1 => a + b
        case _ => do_fibo(n-1,b,a+b)
      }

    }
    do_fibo(n,0,1)
  }

}
<commit_msg>format functions<commit_after>package fpscala.chapter2

/**
 * Created by sajit on 5/1/15.
 */
object Chapter2 {

  //@annotation.tailrec
  /**
   * This is not a tailrec function as we are doing some computation. Hence compiler will throw an error
   * @param n
   * @return
   */
  def factorial(n:Int):Int = {
    n match {
      case 0 => 1
      case 1 => 1
      case _ => n * factorial(n-1)
    }
  }

  /**
   * tail_factorial is not a recursive call. So no sense in making tail rec
   * @param n
   * @return
   */
  def tail_factorial(n:Int):Int = {
    @annotation.tailrec
    def do_tail_factorial(n:Int,acc:Int):Int = {
      n match {
        case 0 => acc
        case 1 => acc
        case _ => do_tail_factorial(n-1,n*acc)
      }
    }
    do_tail_factorial(n,1)
  }

  def fibo(n:Int):Int = {
    @annotation.tailrec
    def do_fibo(n:Int,a:Int,b:Int):Int = {
      n match {
        case x if x<=1 => a + b
        case _ => do_fibo(n-1,b,a+b)
      }

    }
    do_fibo(n,0,1)
  }

  def formatAbs(x:Int) = {
    val message = "The absolute value of %d is %d"
    message.format(x, Math.abs(x))
  }

  def formatFactorial(x:Int) = {
    val message = "The factorial of %d is %d."
    message.format(x,factorial(x))
  }
}

{'fraction_non_alphanumeric': 0.13458110516934046, 'fraction_numerical': 0.01693404634581105, 'mean_word_length': 2.0795610425240056, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 19, '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': '418187', 'n_tokens_mistral': 893, 'n_tokens_neox': 856, 'n_words': 209}