0

I have the following type of data:

case class TipoDeDato[T] (nombreCampo: String,valor: T)

And in my exercise, I need to create the following structure, using the type of data I mentioned:

Data Structure

So, I created the following structure

  val registro0: List[TipoDeDato[_>: String with Int]] = List(
    new TipoDeDato[String]("Autor", "Gabo"),
    new TipoDeDato[String]("Titulo", "100 Años"),
    new TipoDeDato[Int]("Numero de Paginas", 700)
  )
  val registro1: List[TipoDeDato[_>: String with Int]] = List(
  new TipoDeDato[String]("Autor", "Gabo"),
  new TipoDeDato[String]("Titulo", "Maria"),
  new TipoDeDato[Int]("Numero de Paginas", 1200)
  )
  val registro2: List[TipoDeDato[_>: String with Int]] = List(
    new TipoDeDato[String]("Autor", "Gabo"),
    new TipoDeDato[String]("Titulo", "Carrasco"),
    new TipoDeDato[Int]("Numero de Paginas", 150)
  )
  val registro3: List[TipoDeDato[_>: String with Int]] = List(
    new TipoDeDato[String]("Autor", "Gabo"),
    new TipoDeDato[String]("Titulo", "Oceano"),
    new TipoDeDato[Int]("Numero de Paginas", 200)
  )

And to create the "Libros" object, I have done the following:

val Libros: List[List[TipoDeDato[_>: String with Int]]] = List(registro0,registro1,registro2,registro3)

My question is, how can I sort the "Libros" object, by any of its components, "Autor", "Titulo", "Numero de paginas"?, is this structure adequate for what I need to do?

jamlhet
  • 619
  • 7
  • 13
  • Your questions isn't well defined: First, you're sorting different data types (in the example - `Int` and `String` - how is the sorting order defined? E.g. is 752 greater than or smaller than "HGabo" ? And second - how is the order of _lists_ of such items defined? longest list to shortest? order of maximum values of lists? – Tzach Zohar Jul 25 '17 at 01:17
  • Hi, i need sort, by field "new TipoDeDato[Int]("Numero de Paginas", 752)", by example. And I do not know if with this possible sea data structure, or if I have to change it. – jamlhet Jul 25 '17 at 01:26
  • Hi, I change it. I have List of List, how to sort by "NumeroDePaginas" object? – jamlhet Jul 25 '17 at 01:33
  • btw, you don't have to do `new` in order to instantiate case class. `TipoDeDato(Autor("HGabo"))` is enough – dk14 Jul 25 '17 at 13:50
  • also you could make `TipoDeDato` to be a trait: `sealed trait TipoDeDato; case class Autor (autor: String) extends TipoDeDato ...`. It would simplify your code a lot (especially type signatures) – dk14 Jul 25 '17 at 13:51
  • Thanks, I edit the question, placing an example of what I want to do, I will implement what you tell me, again thank you very much! – jamlhet Jul 25 '17 at 13:57

1 Answers1

1

To sort List of List:

sealed trait TipoDeDato

case class Autor (autor: String) extends TipoDeDato
case class Titulo (titulo: String) extends TipoDeDato
case class NumeroDePaginas (numeroDePaginas: Int) extends TipoDeDato

class TablaItems(var registros: List[List[TipoDeDato]]){
  def insertInto(reg: List[List[TipoDeDato]]): TablaItems = {
    registros = registros ::: reg
    this
  }
}

  val registro0: List[TipoDeDato] = List(
    Autor("HGabo"), 
    Titulo("ZLa María"),
    NumeroDePaginas(752)
  )

  val registro1: List[TipoDeDato] = List(
    Autor("AGabo"),
    Titulo("CLa María"),
    NumeroDePaginas(521)
  )

  val Registros1: List[List[TipoDeDato]] = List(registro0)
  val Registros2: List[List[TipoDeDato]] = List(registro1)

  val tablaLibros = new TablaItems(Registros1)
  tablaLibros.registros.foreach(println)
  println("----")
  tablaLibros.insertInto(Registros2)
  tablaLibros.registros.foreach(println)
  println("----")
  tablaLibros.registros.sortBy(r=>r.collectFirst{
      case NumeroDePaginas(n) => n
  }.getOrElse(0))

Actually I think you need:

case class Dato(autor: String, titulo: String, numeroDePaginas: Int)


class TablaItems(var registros: List[Dato]){
  def insertInto(reg: List[Dato]): TablaItems = {
    registros = registros ::: reg
    this
  }
}

  //you can also do (if you prefer) `Dato(author = "HGabo", titulo = "ZLa María", numeroDePaginas = 752)
  val registro0 = Dato("HGabo", "ZLa María", 752) 

  val registro1 = Dato("AGabo", "CLa María", 521)

  val Registros1: List[Dato] = List(registro0)
  val Registros2: List[Dato] = List(registro1)

  val tablaLibros = new TablaItems(Registros1)
  tablaLibros.registros.foreach(println)
  println("----")
  tablaLibros.insertInto(Registros2)
  tablaLibros.registros.foreach(println)
  println("----")
  tablaLibros.registros.sortBy(_.numeroDePaginas)

Also, if this problem requires functional programming (no side-effects, and also I got rid of OOP - however the latter is not mandatory, OOP and FP are orthogonal):

case class TablaItems(registros: List[Dato])

implicit class TablaItemsOperations(tabla: TablaItems){
  def withData(reg: List[Dato]) = TablaItems(tabla.registos :: reg)
}

...


val tablaLibros = TablaItems(Registros1)
tablaLibros.registros.foreach(println)
println("----")
val tablaLibrosUpdated = tablaLibros.withData(Registros2)
tablaLibrosUpdated.registros.foreach(println)
println("----")
tablaLibrosUpdated.registros.sortBy(_.numeroDePaginas)
dk14
  • 22,206
  • 4
  • 51
  • 88