项目作者: tulz-app

项目描述 :
Scala tuple composition
高级语言: Scala
项目地址: git://github.com/tulz-app/tuplez.git
创建时间: 2020-12-14T18:26:55Z
项目社区:https://github.com/tulz-app/tuplez

开源协议:MIT License

下载


Maven Central

tuplez

Tuple composition in Scala and Scala.js.

  1. // tupleN + scalar, scalar + tupleN, tupleN + tupleM, up to Tuple22
  2. "app.tulz" %%% "tuplez-full" % "0.4.0"
  3. // or
  4. // tupleN + scalar, scalar + tupleN, tupleN + tupleM, up to Tuple10
  5. "app.tulz" %%% "tuplez-full-light" % "0.4.0"
  6. // or
  7. // tupleN + scalar, up to Tuple22
  8. "app.tulz" %%% "tuplez-basic" % "0.4.0"
  9. // or
  10. // tupleN + scalar, up to Tuple10
  11. "app.tulz" %%% "tuplez-basic-light" % "0.4.0"
  1. // utilities to build API's that allow using a FunctionN[A, B, C, ... Out] instead of Function1[TupleN[A, B, C, ...], Out]
  2. "app.tulz" %%% "tuplez-apply" % "0.4.0"

Published for Scala 2.12, 2.13 and 3.2.1, JVM and Scala.js 1.5.1+.

Source code

Source code is 100% generated.

Composition

app.tulz.tuplez.TupleComposition

  1. abstract class Composition[L, R] {
  2. type Composed
  3. val compose: (L, R) => Composed
  4. def decompose(c: Composed): (L, R)
  5. }

Implicit values are provided for composing tuples with tuples, and tuples with scalars (both prepending and appending).

Implicits are defined by the generated code.

The companion object provides utility functions to compose/decompose two tuples (or a tuple and a scalar)

  1. object TupleComposition {
  2. def compose[L, R](l: L, r: R)(implicit composition: Composition[L, R]): composition.Composed = composition.compose(l, r)
  3. def decompose[L, R, C](c: C)(implicit composition: Composition.Aux[L, R, C]): (L, R) = composition.decompose(c)
  4. }

Examples:

  1. import app.tulz.tuplez.TupleComposition
  2. TupleComposition.compose( Tuple1(1), Tuple1(2) ) // (1, 2)
  3. TupleComposition.compose( 1, 2 ) // (1, 2)
  4. TupleComposition.compose( (1, 2, 3, 4), (5, 6) ) // (1, 2, 3, 4, 5, 6)
  5. TupleComposition.compose( (1, 2, 3), 4 ) // (1, 2, 3, 4)
  6. TupleComposition.compose( 1, (2, 3, 4) ) // (1, 2, 3, 4)
  7. TupleComposition.compose( (1, 2, 3), Tuple1(4) ) // (1, 2, 3, 4)
  8. TupleComposition.compose( Tuple1(1), (2, 3, 4) ) // (1, 2, 3, 4)
  9. TupleComposition.compose( (1, 2, 3), () ) // (1, 2, 3)
  10. TupleComposition.compose( (), (1, 2, 3) ) // (1, 2, 3)
  11. // etc

Apply converters

app.tulz.tuplez.ApplyConverter

Utilities for converting FunctionN[..., Out] into Function1[TupleN[...], Out]

Example:

  1. import app.tulz.tuplez._
  2. object instances extends ApplyConverters[String]
  3. // in order to make type and implicits resolution possible, the apply converters are generated for a fixed output type
  4. import instances._
  5. val acceptingTupledFunc: ((Int, Int, Int, Int) => String) => String = func => func((1, 2, 3, 4))
  6. val nonTupledFunction = (x1: Int, x2: Int, x3: Int, x4: Int) => s"I return [${x1}, ${x2}, ${x3}, ${x4}]"
  7. assert(acceptingTupledFunc(toTupled4(nonTupledFunction)) == "I return [1, 2, 3, 4]")

Intended usage

Simple example:

  1. import app.tulz.tuplez._
  2. case class MyStructure[T](
  3. data: T
  4. ) {
  5. def appendScalar[U](value: U)(implicit composition: Composition[T, U]): MyStructure[composition.Composed] =
  6. copy(data = composition.compose(data, value))
  7. // or
  8. // copy(data = TupleComposition.compose(data, value))
  9. }

A more complete example: https://github.com/tulz-app/frontroute/blob/main/src/main/scala/io/frontroute/DirectiveApplyConverters.scala

Author

Iurii Malchenko – @yurique

License

tuplez is provided under the MIT license.