Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/bizzabo/typeless
running wild with shapeless
https://github.com/bizzabo/typeless
backend rnd xdotai
Last synced: 12 days ago
JSON representation
running wild with shapeless
- Host: GitHub
- URL: https://github.com/bizzabo/typeless
- Owner: bizzabo
- License: apache-2.0
- Created: 2017-02-16T16:36:25.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2018-12-18T15:42:55.000Z (almost 6 years ago)
- Last Synced: 2023-06-29T12:10:14.132Z (over 1 year ago)
- Topics: backend, rnd, xdotai
- Language: Scala
- Homepage:
- Size: 97.7 KB
- Stars: 15
- Watchers: 43
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# typeless
Some typeclasses inspired and powered by shapeless
#### `Find[L <: HList, A]`
Will allow to find a type `A` in an `HList` `L`, if the type is not present it returns `None`, otherwise `Some[A]`
#### `Subset[L <: HList,S <: HList]`
Similar to `Find`, but for a group of elements, if **all** the elements of the `S` are present in `L` it returns `Some[S]` otherwise `None`
#### `ListToHList[L, H < HList]`
Converts a `List[L]` to an `Option[HList]`. The `ListToHList` implicit class exposes two features.
`.toProduct[A]`: convert `List[L]` to an `Option[A]`.
`.findByType[B]`: find type `B` within `List[L]`
```scala
import ListToHList.Ops
sealed trait A
case class B() extends A
case class C() extends Acase class D(b: B, c: C)
val listA: List[A] = List(B(), C())
listA.toProduct[D] === Some(D(B(), C()))
listA.findByType[B] === Some(B())
```#### `Convert[L <: Coproduct, S <: Coproduct]`
For Coproducts `L` and `S`, `Convert` takes a value of type `L` and converts it to type `S`.
#### example
```scala
type A = String :+: Double :+: CNil
type B = Double :+: String :+: List[Int] :+: CNilCoproduct[A]("test").convert[B] === Some(Coproduct[B]("test"))
```
#### `CoproductToHList[C <: Coproduct, L <: HList]`
For a `Seq` of Coproducts `C`, convert to `HList` of type `L`
#### example
```scala
type A = Int :+: String :+: CNil
type L = String :: Int :: HNilSeq(Coproduct[A](1), Coproduct[A]("a")).toHList[L] === Some("a" :: 1 :: HNil))
case class Foo(i:Int, s:String)
Seq(Coproduct[A](1), Coproduct[A]("a")).toHList[Foo] === Some(Food("a", 1))```
#### `SelectFunctions[L <: HList, FF <: HList]`
Takes an `HList` of functions `FF` and an `HList` of potential arguments `Context`. It applies the arguments to the functions for which all the arguments are present. It returns an `HList` with the results. It will *not compile* if there is a function that cannot be applied.
#### example
```scala
val functions =
{ (x: String, i: Int, d: Double) => d.toInt * i } ::
{ (x: String, i: Int) => s"$x + $i" } ::
{ (x: String, s: Char, i: Int) => i.toDouble } ::
{ (x: String, s: Char, i: Int) => s.toInt + i * 2 + x.size } ::
{ (x: String) => x.size } ::
{ (x: Char) => x.toInt } ::
HNilSelectFunctions.applyAll(1, hi)(functions) // won't compile
SelectFunctions.applyAll(hi, 'a', 1)(functions) == "hi + 1" :: 1.0 :: 101 :: 2 :: 97 :: HNil
```#### `FlattenFunctions[Context <: HList, FFF <: HList]`
Takes an `HList` of `HLists` of functions and an `HList` of potential arguments, and uses `SelectFunctions[Context, FF]` to calculate the resulting `HList`.
#### example
```scala
val functions1 =
{ (x: String, i: Int) => (x.size + i) } ::
{ (x: String, s: Char, i: Int) => (s.toInt + i * 2 + x.size) } ::
HNil
val functions2 =
{ (x: String, s: Char, i: Int) => (s.toInt + i + x.size) } ::
{ (i: Int) => i.toDouble } ::
HNilval functions = functions1 ::
functions2 ::
HNilFlattenFunctions.applyAll(1, "a", 'a')(functions) === 2 :: 1.0 :: 99 :: HNil
```#### `EqualsIgnoringFields[T]`
It compares two cases classes excluding specific *field names* rather than types.
#### example:
```scalaimport typeless.hlist.EqualsIgnoringFields.Ops
sealed trait Monarch
case class Butterflies(
_id: Long,
date: Long,
count: Int
) extends Monarchcase class Dictator(
_id: Long,
date: Long,
count: Int
) extends Monarchval butterfliesStation1 = Butterflies(
_id = 1,
date = 1131,
count = 3
)
val butterfliesStation2 = Butterflies(
_id = 2,
date = 1131,
count = 2
)// the two objects are the same if we ignore those two fields
assert(butterfliesStation1.equalsIgnoringFields(field => field == '_id || field == 'count)(butterfliesStation2))
// the two objects are different if not ignoring `count`
assert(!butterfliesStation1.equalsIgnoringFields(_ == '_id)(butterfliesStation2))
// the two objects are different, period
assert(butterfliesStation1 != butterfliesStation2)```
## Getting started
### Scala 2.11/2.12
```scala
libraryDependencies += "ai.x" %% "typeless" % "0.6.0"
```