tobyweston / temperature-machine

Data logger for multiple DS18B20 temperature sensors on one or more machines
Apache License 2.0
67 stars 22 forks source link

More IO monads! #62

Closed tobyweston closed 6 years ago

tobyweston commented 6 years ago

For example,

import cats.implicits._

object IOs {

  def init(hosts: List[Host]) = {
    for {
      exists <- RrdFile.exists
      _      <- info(s"RRD initialising for ${hosts.map(_.name).mkString("'", "', '", "'")} (with up to $MaxSensors sensors each)...").unlessA(exists)
      _      <- IO(RrdFile(hosts, 30 seconds).create()).unlessA(exists)
      _      <- info("RRD initialised Ok").unlessA(exists)
      _      <- info(s"RRD initialised for ${hosts.map(_.name).mkString("'", "', '", "'")} (with up to $MaxSensors sensors each).").whenA(exists)
    } yield ()
  }

  ...
}