|
1 | | -# hxl |
| 1 | +# Hxl |
| 2 | +Hxl is a pure applicative batching library for Scala. |
| 3 | + |
| 4 | +Hxl is based on the ideas presented in [Haxl](https://simonmar.github.io/bib/papers/haxl-icfp14.pdf), but diverges in in a few ways. |
| 5 | +Notably, hxl does not use side effects, but is instead based on free applicatives. |
| 6 | + |
| 7 | +Hxl is very small (only a couple hundred lines of code) and only depends on cats. |
| 8 | + |
| 9 | +Hxl is written in tagless final, which allows for molding the library to your needs. |
| 10 | + |
| 11 | +## Installation |
| 12 | +Hxl is available on Maven Central for Scala 2.13 and 3.2. |
| 13 | +```scala |
| 14 | +libraryDependencies += "com.github.casehubdk" %% "hxl" % "0.1.0" |
| 15 | +``` |
| 16 | + |
| 17 | +## Usage |
| 18 | +There are two primitive structures in hxl: `DataSource[F, K, V]` and `DSKey[K, V]`. |
| 19 | +A `DataSource[F, K, V]` abstracts over a function `NonEmptyList[K] => F[Map[K, V]]`. |
| 20 | +A `DataSource` is uniquely (as in Scala universal equals) identified by its `DSKey`. |
| 21 | +```scala |
| 22 | +import cats._ |
| 23 | +import cats.implicits._ |
| 24 | + |
| 25 | +final case class MyDSKey(id: String) |
| 26 | +case object MyDSKey extends DSKey[MyDSKey, String] |
| 27 | + |
| 28 | +val database = Map( |
| 29 | + "foo" -> "bar", |
| 30 | + "baz" -> "qux" |
| 31 | +) |
| 32 | + |
| 33 | +def dataSource[F[_]: Applicative] = DataSource.from[F, MyDSKey, String](MyDSKey) { keys => |
| 34 | + keys.toList.flatMap(key => database.get(key.id).toList.tupleLeft(key)).toMap.pure[F] |
| 35 | +} |
| 36 | + |
| 37 | +val fa: Hxl[Id, Option[String]] = Hxl(MyDSKey("foo"), dataSource[Id]) |
| 38 | +val fb: Hxl[Id, Option[String]] = Hxl(MyDSKey("baz"), dataSource[Id]) |
| 39 | +(fa, fb).mapN(_.mkString + " " + _.mkString) // "bar qux" |
| 40 | +``` |
| 41 | + |
| 42 | +Hxl is an applicative, but sometimes you need a monad. |
| 43 | +Hxl is like `Validated` from `cats`, in that it can escape it's applicative nature via a method `andThen`. |
| 44 | +However, if you need Hxl to become a monad (like `Either` to `Validated`), you can use request a monadic view of your effect: |
| 45 | +```scala |
| 46 | +val fa: Hxl[F, String] = ??? |
| 47 | + |
| 48 | +val m: HxlM[F, String] = fa.monadic.flatMap{ x => |
| 49 | + ??? |
| 50 | +} |
| 51 | + |
| 52 | +val back: Hxl[F, String] = m.hxl |
| 53 | +``` |
| 54 | + |
| 55 | +## Advanced usage |
| 56 | +Since Hxl is written in tagless final, you can add various behaviors to your data sources. |
| 57 | +For instance, you can add (pure) caching. |
| 58 | +```scala |
| 59 | +import cats.data._ |
| 60 | +import cats.implicits._ |
| 61 | + |
| 62 | +final case class MyDSKey(id: String) |
| 63 | +case object MyDSKey extends DSKey[MyDSKey, String] |
| 64 | + |
| 65 | +val database = Map( |
| 66 | + "foo" -> "bar", |
| 67 | + "baz" -> "qux" |
| 68 | +) |
| 69 | + |
| 70 | +type Cache = Map[String, String] |
| 71 | +type Effect[A] = State[Cache, A] |
| 72 | + |
| 73 | +def dataSource: DataSource[Effect, MyDSKey, String] = DataSource.from(MyDSKey) { keys => |
| 74 | + State[Cache, Map[MyDSKey, String]] { cache => |
| 75 | + val (misses, hits) = keys.toList.partitionEither(k => cache.get(k.id).tupleLeft(k).toRight(k)) |
| 76 | + val fetched = misses.flatMap(key => database.get(key.id).toList.map(key -> _)).toMap |
| 77 | + (cache ++ fetched.map{ case (k, v) => k.id -> v }, hits.toMap ++ fetched) |
| 78 | + } |
| 79 | +} |
| 80 | +``` |
0 commit comments