<commit_before>/*
 * lidg.scala - Implementation of LIGD for Scala.
 *
 * Copyright 2014 <NAME> <<EMAIL>>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import scala.language.implicitConversions
import org.scalatest._

object LIGDCompany {
  import LIGD._
  import CompanyData._

  /* Let's play a trick */
  def fromFloat(f: Float) = Salary(f)
  def toFloat(s: Salary) = s.salary
  implicit case object RSalary extends RType[Float, Salary](RFloat, EP(toFloat, fromFloat))

  /* The simple one or two element types */
  implicit def rPerson = rType(Person.unapply(_: Person).get, (Person.apply _).tupled)
  implicit def rEmployee = rType(Employee.unapply(_: Employee).get, (Employee.apply _).tupled)
  implicit def rCompany = rType(Company.unapply(_: Company).get, Company.apply)

  /* Mutually recursive types, require annotation, also need to break loop */
  implicit def rDUnit: RType[Either[Dept, Employee], DUnit] = RType(RSum(rDept, rEmployee),
    EP(_ match {
      case PU(per)  ⇒ Right(per)
      case DU(dept) ⇒ Left(dept)
    }, _.fold(DU, PU)))

  implicit def rDept: RType[(String, (Manager, List[DUnit])), Dept] = rType(
    d ⇒ (d.name, (d.manager, d.units)),
    e ⇒ Dept(e._1, e._2._1, e._2._2)
  )

  /** Example: Summing all the salaries in a data structure */
  def sumSalaryOld[A](a: A)(implicit rep: Rep[A]): Float = (rep, a) match {
    case (RSum(ra, rb), Left(a))   ⇒ sumSalaryOld(a)(ra)
    case (RSum(ra, rb), Right(b))  ⇒ sumSalaryOld(b)(rb)
    case (RProd(ra, rb), (a, b))   ⇒ sumSalaryOld(a)(ra) + sumSalaryOld(b)(rb)<commit_msg>CompanyTest: Make Rep defs values where possible<commit_after>/*
 * lidg.scala - Implementation of LIGD for Scala.
 *
 * Copyright 2014 <NAME> <<EMAIL>>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import scala.language.implicitConversions
import org.scalatest._

object LIGDCompany {
  import LIGD._
  import CompanyData._

  /* Let's play a trick */
  def fromFloat(f: Float) = Salary(f)
  def toFloat(s: Salary) = s.salary
  implicit case object RSalary extends RType[Float, Salary](RFloat, EP(toFloat, fromFloat))

  /* The simple one or two element types */
  implicit val rPerson = rType(Person.unapply(_: Person).get, (Person.apply _).tupled)
  implicit val rEmployee = rType(Employee.unapply(_: Employee).get, (Employee.apply _).tupled)
  implicit val rCompany = rType(Company.unapply(_: Company).get, Company.apply)

  /* Mutually recursive types, require annotation, also need to break loop */
  implicit def rDUnit: RType[Either[Dept, Employee], DUnit] = RType(RSum(rDept, rEmployee),
    EP(_ match {
      case PU(per)  ⇒ Right(per)
      case DU(dept) ⇒ Left(dept)
    }, _.fold(DU, PU)))

  implicit def rDept: RType[(String, (Manager, List[DUnit])), Dept] = rType(
    d ⇒ (d.name, (d.manager, d.units)),
    e ⇒ Dept(e._1, e._2._1, e._2._2)
  )

  /** Example: Summing all the salaries in a data structure */
  def sumSalaryOld[A](a: A)(implicit rep: Rep[A]): Float = (rep, a) match {
    case (RSum(ra, rb), Left(a))   ⇒ sumSalaryOld(a)(ra)
    case (RSum(ra, rb), Right(b))  ⇒ sumSalaryOld(b)(rb)
    case (RProd(ra, rb), (a, b))   ⇒ sumSalaryOld(a)(ra) + sumSalaryOld(b)(rb)
{'fraction_non_alphanumeric': 0.12867032704702794, 'fraction_numerical': 0.0062067319169252805, 'mean_word_length': 3.855156431054461, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2800075', 'n_tokens_mistral': 1426, 'n_tokens_neox': 1400, 'n_words': 471}