Skip to content

Commit

Permalink
Intro to HLists
Browse files Browse the repository at this point in the history
  • Loading branch information
thobson committed Aug 22, 2019
1 parent ccb3677 commit 8cb4bd6
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
target
.idea
14 changes: 14 additions & 0 deletions build.sbt
@@ -0,0 +1,14 @@
import Dependencies._

ThisBuild / scalaVersion := "2.12.8"
ThisBuild / version := "0.1.0-SNAPSHOT"
ThisBuild / organization := "com.example"
ThisBuild / organizationName := "example"

lazy val root = (project in file("."))
.settings(
name := "copying-with-shapeless",
libraryDependencies ++= Seq(
shapeless,
scalaTest % Test
))
6 changes: 6 additions & 0 deletions project/Dependencies.scala
@@ -0,0 +1,6 @@
import sbt._

object Dependencies {
lazy val shapeless = "com.chuusai" %% "shapeless" % "2.3.3"
lazy val scalaTest = "org.scalatest" %% "scalatest" % "3.0.5"
}
1 change: 1 addition & 0 deletions project/build.properties
@@ -0,0 +1 @@
sbt.version=1.2.7
25 changes: 25 additions & 0 deletions src/main/scala/example/Datamodel.scala
@@ -0,0 +1,25 @@
package example

import java.time.LocalDate

/* Represents the individual "pages" of our application */

case class Location(pickup: String, dropOff: String, from: LocalDate, to: LocalDate)

case class Vehicle(vehicleCategory: String, automatic: Boolean, numDoors: Int)

case class Driver(driverAge: Int, nationality: String)

/* We will merge the all data into this one class */

case class Reservation(
pickup: String,
dropOff: String,
from: LocalDate,
to: LocalDate,
vehicleCategory: String,
automatic: Boolean,
numDoors: Int,
driverAge: Int,
nationality: String
)
37 changes: 37 additions & 0 deletions src/main/scala/example/Main.scala
@@ -0,0 +1,37 @@
package example

import java.time.LocalDate
import shapeless.{::, HNil}

object Main {

/* Our HList representation of the case classes */
type LocationH = String :: String :: LocalDate :: LocalDate :: HNil
type VehicleH = String :: Boolean :: Int :: HNil
type DriverH = Int :: String :: HNil
type ReservationH = String :: String :: LocalDate :: LocalDate :: String :: Boolean :: Int :: Int :: String :: HNil

def main(args: Array[String]): Unit = {
val locationH: LocationH = "Malaga Airport" :: "Malaga Airport" :: LocalDate.of(2018,8,1) :: LocalDate.of(2018,8,10) :: HNil
val vehicleH: VehicleH = "Economy" :: false :: 4 :: HNil
val driverH: DriverH = 35 :: "British" :: HNil

val reservationH: ReservationH = locationH ++ vehicleH ++ driverH

reservationH match {
case
pickup ::
dropOff ::
from ::
to ::
vehicleCategory ::
automatic ::
numDoors ::
driverAge ::
nationality ::
HNil =>
println(s"pickup location: $pickup")
}
}

}

0 comments on commit 8cb4bd6

Please sign in to comment.