Skip to content

Commit

Permalink
0.3.1 - refactoring and low pri for apply
Browse files Browse the repository at this point in the history
  • Loading branch information
yurique committed Jan 1, 2021
1 parent 80a05ee commit 7fca17a
Show file tree
Hide file tree
Showing 13 changed files with 100 additions and 172 deletions.
1 change: 1 addition & 0 deletions .scalafmt.conf
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ docstrings.oneline = unfold
trailingCommas = "never"
optIn.breaksInsideChains = true
includeCurlyBraceInSelectChains = true
trailingCommas = "preserve"
36 changes: 33 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,39 @@
# Changelog

### 0.2.0
## 0.3.1

Adding apply converters for converting `FunctionN[..., Out]` into `Function1[TupleN[...], Out]`
#### `ApplyConverter`

### 0.1.0
Refactoring + `ApplyConverterInstancesPriLow` with an "identity" transformation for unary functions (with a scalar param).

#### `tuplez-*`

No changes.

#### `tuplez-tuple`

No longer exists.

## 0.3.0

Now publishing multiple interchangeable modules:

#### `tuplez-full`
tupleN + scalar, scalar + tupleN, tupleN + tupleM, up to `Tuple22`

#### `tuplez-full-light`
tupleN + scalar, scalar + tupleN, tupleN + tupleM, up to `Tuple10`

#### `tuplez-basic`
tupleN + scalar, up to `Tuple22`

#### `tuplez-basic-light`
tupleN + scalar, up to `Tuple10`

## 0.2.0

`tuplez-apply` – utilities to build API's that allow using a `FunctionN[A, B, C, ... Out]` instead of `Function1[TupleN[A, B, C, ...], Out]`

## 0.1.0

* initial release
68 changes: 36 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,46 +1,48 @@
![Maven Central](https://img.shields.io/maven-central/v/app.tulz/tuplez-full_sjs1_2.13.svg)

### app.tulz.tuplez
### tuplez

A tiny library for tuple composition.
Tuple composition in Scala and Scala.js.

```scala
// tupleN + scalar, scalar+tupleN, tupleN+tupleM, for all sizes (2 to 22)
"app.tulz" %%% "tuplez-full" % "0.3.0"
// tupleN + scalar, scalar + tupleN, tupleN + tupleM, up to Tuple22
"app.tulz" %%% "tuplez-full" % "0.3.1"

// or

// tupleN + scalar, scalar+tupleN, tupleN+tupleM, for all sizes (2 to 10)
"app.tulz" %%% "tuplez-full-light" % "0.3.0"
// tupleN + scalar, scalar + tupleN, tupleN + tupleM, up to Tuple10
"app.tulz" %%% "tuplez-full-light" % "0.3.1"

// or

// tupleN + scalar, for all sizes (2 to 22)
"app.tulz" %%% "tuplez-basic" % "0.3.0"
// tupleN + scalar, up to Tuple22
"app.tulz" %%% "tuplez-basic" % "0.3.1"

// or

// tupleN + scalar, for all sizes (2 to 10)
"app.tulz" %%% "tuplez-basic-light" % "0.3.0"
// tupleN + scalar, up to Tuple10
"app.tulz" %%% "tuplez-basic-light" % "0.3.1"
```

```scala
// apply converters (use a FunctionN where Function1[TupleN] is expected)
"app.tulz" %%% "tuplez-apply" % "0.3.0"
// utilities to build API's that allow using a FunctionN[A, B, C, ... Out] instead of Function1[TupleN[A, B, C, ...], Out]
"app.tulz" %%% "tuplez-apply" % "0.3.1"
```

```scala
// Tuple marker trait and evidences for Tuple1 .. Tuple22
"app.tulz" %%% "tuplez-tuple" % "0.3.0"
Published for Scala `2.12` and `2.13`.

```
### Source code

### Tuple marker traits
Source code is 100% generated. Snapshots as of `v0.3.1`:

* `app.tulz.tuplez.Tuple[T]` - instances exist for `Tuple1` .. `Tuple22` (a utility, not actually used by this library)
* `tuplez-full`[gist](https://gist.github.com/yurique/2e465979e89bb86214a4ccb6a0adc66c)
* `tuplez-full-light`[gist](https://gist.github.com/yurique/80f8fb8701e49cfd749348015f1f546a)
* `tuplez-basic`[gist](https://gist.github.com/yurique/6f53c0da31b30c6f637b6292aedfd144)
* `tuplez-basic-light`[gist](https://gist.github.com/yurique/482ca61973266dead614367b5ee833f7)
* `tuplez-apply`[gist](https://gist.github.com/yurique/d91802e5758e5b85b4c49bc024c23abf) + [gist](https://gist.github.com/yurique/1fac62007c52a00d90c058241bb797b3)


### Composition
## Composition

`app.tulz.tuplez.TupleComposition`

Expand All @@ -55,11 +57,7 @@ Implicit values are provided for composing tuples with tuples, and tuples with s

Implicits are defined by the generated code.

### TupleComposition

`app.tulz.tuplez.TupleComposition`

Provides a single utility function to compose two tuples (or a tuple and a scalar)
The companion object provides a single utility function to compose two tuples (or a tuple and a scalar)

```scala
object TupleComposition {
Expand All @@ -70,22 +68,26 @@ object TupleComposition {

```

Example:
Examples:

```scala
import app.tulz.tuplez.TupleComposition

TupleComposition.compose( (1), (2) ) // (1, 2)
TupleComposition.compose( Tuple1(1), Tuple1(2) ) // (1, 2)
TupleComposition.compose( 1, 2 ) // (1, 2)
TupleComposition.compose( (1, 2, 3, 4), (5, 6) ) // (1, 2, 3, 4, 5, 6)
TupleComposition.compose( (1, 2, 3), 4 ) // (1, 2, 3, 4)
TupleComposition.compose( 1, (2, 3, 4) ) // (1, 2, 3, 4)
TupleComposition.compose( (1, 2, 3), Tuple1(4) ) // (1, 2, 3, 4)
TupleComposition.compose( Tuple1(1), (2, 3, 4) ) // (1, 2, 3, 4)
TupleComposition.compose( (1, 2, 3), () ) // (1, 2, 3)
TupleComposition.compose( (), (1, 2, 3) ) // (1, 2, 3)
// etc
```

### Apply converters
## Apply converters

`app.tulz.tuplez.TupleComposition`
`app.tulz.tuplez.ApplyConverter`

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

Expand All @@ -94,15 +96,16 @@ Example:
```scala
import app.tulz.tuplez._

val instances = new ApplyConverters[String] {} // for now, in order to make type and implicits resolution possible, the apply converters are generated for a fixed output type
object instances extends ApplyConverters[String]
// in order to make type and implicits resolution possible, the apply converters are generated for a fixed output type
import instances._

val acceptingTupledFunc: ((Int, Int, Int, Int) => String) => String = func => func((1, 2, 3, 4))
val nonTupledFunction = (x1: Int, x2: Int, x3: Int, x4: Int) => s"I return [${x1}, ${x2}, ${x3}, ${x4}]"
assert(acceptingTupledFunc(toTupled4(nonTupledFunction)) == "I return [1, 2, 3, 4]")
```

### Intended usage
## Intended usage

Simple example:

Expand All @@ -114,7 +117,8 @@ case class MyStructure[T](
) {

def appendScalar[U](value: U)(implicit composition: Composition[T, U]): MyStructure[composition.Composed] =
copy(data = composition.compose(data, value)) // or
copy(data = composition.compose(data, value))
// or
// copy(data = TupleComposition.compose(data, value))

}
Expand All @@ -125,7 +129,7 @@ A more complete example: https://github.com/tulz-app/frontroute/blob/main/src/ma

## Author

Iurii Malchenko – [@yurique](https://twitter.com/yurique)
Iurii Malchenko – [@yurique](https://twitter.com/yurique) / [keybase.io/yurique](https://keybase.io/yurique)


## License
Expand Down
31 changes: 4 additions & 27 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ lazy val `tuplez-full` =
}.taskValue,
Test / sourceGenerators += Def.task {
Seq.concat(
new TupleTestGenerator((Test / sourceManaged).value, to = 22, testConcats = true, testPrepends = true).generate()
new TupleCompositionTestGenerator((Test / sourceManaged).value, to = 22, testConcats = true, testPrepends = true).generate()
)
}.taskValue,
mappings in (Compile, packageSrc) := {
Expand All @@ -88,7 +88,7 @@ lazy val `tuplez-full-light` =
}.taskValue,
Test / sourceGenerators += Def.task {
Seq.concat(
new TupleTestGenerator((Test / sourceManaged).value, to = 10, testConcats = true, testPrepends = true).generate()
new TupleCompositionTestGenerator((Test / sourceManaged).value, to = 10, testConcats = true, testPrepends = true).generate()
)
}.taskValue,
mappings in (Compile, packageSrc) := {
Expand All @@ -114,7 +114,7 @@ lazy val `tuplez-basic` =
}.taskValue,
Test / sourceGenerators += Def.task {
Seq.concat(
new TupleTestGenerator((Test / sourceManaged).value, to = 22, testConcats = false, testPrepends = false).generate()
new TupleCompositionTestGenerator((Test / sourceManaged).value, to = 22, testConcats = false, testPrepends = false).generate()
)
}.taskValue,
mappings in (Compile, packageSrc) := {
Expand All @@ -140,7 +140,7 @@ lazy val `tuplez-basic-light` =
}.taskValue,
Test / sourceGenerators += Def.task {
Seq.concat(
new TupleTestGenerator((Test / sourceManaged).value, to = 10, testConcats = false, testPrepends = false).generate()
new TupleCompositionTestGenerator((Test / sourceManaged).value, to = 10, testConcats = false, testPrepends = false).generate()
)
}.taskValue,
mappings in (Compile, packageSrc) := {
Expand Down Expand Up @@ -180,27 +180,6 @@ lazy val `tuplez-apply` =
description := "A tiny library for tuple composition"
)

lazy val `tuplez-tuple` =
crossProject(JVMPlatform, JSPlatform)
.crossType(CrossType.Pure)
.in(file("modules/tuple"))
.settings(commonSettings)
.settings(
Compile / sourceGenerators += Def.task {
Seq.concat(
new TupleGenerator((Compile / sourceManaged).value).generate()
)
}.taskValue,
mappings in (Compile, packageSrc) := {
val base = (sourceManaged in Compile).value
val files = (managedSources in Compile).value
files.map { f =>
(f, f.relativeTo(base / "scala").get.getPath)
}
},
description := "Tuple evidence trait and instances for Tuple1 .. Tuple22"
)

lazy val `tuplez-js` = project
.in(file(".tuplez-js"))
.settings(
Expand All @@ -213,7 +192,6 @@ lazy val `tuplez-js` = project
`tuplez-basic`.js,
`tuplez-basic-light`.js,
`tuplez-apply`.js,
`tuplez-tuple`.js
)

lazy val `tuplez-jvm` = project
Expand All @@ -228,7 +206,6 @@ lazy val `tuplez-jvm` = project
`tuplez-basic`.jvm,
`tuplez-basic-light`.jvm,
`tuplez-apply`.jvm,
`tuplez-tuple`.jvm
)

lazy val root = project
Expand Down
2 changes: 1 addition & 1 deletion project/ApplyConverterGenerator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import sbt._
import java.io.File

class ApplyConverterGenerator(sourceManaged: File)
extends TuplezSourceGenerator(
extends SourceGenerator(
sourceManaged / "scala" / "app" / "tulz" / "tuplez" / "ApplyConverter.scala"
) {

Expand Down
25 changes: 17 additions & 8 deletions project/ApplyConverterInstancesGenerator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,35 @@ import sbt._
import java.io.File

class ApplyConverterInstancesGenerator(sourceManaged: File)
extends TuplezSourceGenerator(
extends SourceGenerator(
sourceManaged / "scala" / "app" / "tulz" / "tuplez" / "ApplyConverterInstances.scala"
) {

def doGenerate(): Unit = {
println("""package app.tulz.tuplez""")
println()
enter("""trait ApplyConverterInstances[O] {""")
for (size <- 1 to 22) {
val tpe = tupleType(size)
val mtch = tupleType(size, "t")
enter("""trait ApplyConverterInstancesPriLow[O] {""")
enter("""implicit def singleParam[T1]: ApplyConverter[T1, O] { type In = (T1) => O } = new ApplyConverter[T1, O] {""")
println("""type In = T1 => O""")
println("""def apply(fn: T1 => O): T1 => O = fn""")
leave("""}""")
leave("""}""")
println()
enter("""trait ApplyConverterInstances[O] extends ApplyConverterInstancesPriLow[O] {""")

enter("""implicit def toTupled1[T1]: ApplyConverter[Tuple1[T1], O] { type In = (T1) => O } = new ApplyConverter[Tuple1[T1], O] {""")
println("""type In = (T1) => O""")
println("""def apply(fn: (T1) => O): (Tuple1[T1]) => O = t => fn(t._1)""")
leave("""}""")
for (size <- 2 to 22) {
val tpe = tupleType(size)
enter(
s"""implicit def toTupled${size}[${tpe}]: ApplyConverter[Tuple${size}[${tupleType(size)}], O] { type In = (${tupleType(
size
)}) => O } = new ApplyConverter[Tuple${size}[${tpe}], O] {"""
)
println(s"""type In = (${tpe}) => O""")
enter(s"""def apply(fn: In): (Tuple${size}[${tpe}]) => O = { case Tuple${size}(${mtch}) =>""")
println(s"""fn(${mtch})""")
leave("""}""")
println(s"""def apply(fn: (${tpe}) => O): (Tuple${size}[${tpe}]) => O = fn.tupled""")
leave("""}""")
}
leave("""}""")
Expand Down
2 changes: 1 addition & 1 deletion project/ApplyConverterTestGenerator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import sbt._
import java.io.File

class ApplyConverterTestGenerator(sourceManaged: File)
extends TuplezSourceGenerator(
extends SourceGenerator(
sourceManaged / "scala" / "app" / "tulz" / "tuplez" / "ApplyConverterTests.scala"
) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import java.io.File
import java.io.FileOutputStream
import java.io.PrintStream

abstract class TuplezSourceGenerator(file: File) {
abstract class SourceGenerator(file: File) {

file.getParentFile.mkdirs()
private val printStream = new PrintStream(new FileOutputStream(file))
Expand Down
20 changes: 0 additions & 20 deletions project/ToTupledGenerator.scala

This file was deleted.

2 changes: 1 addition & 1 deletion project/TupleCompositionGenerator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import sbt._
import java.io.File

class TupleCompositionGenerator(sourceManaged: File, to: Int, generateConcats: Boolean, generatePrepends: Boolean)
extends TuplezSourceGenerator(
extends SourceGenerator(
sourceManaged / "scala" / "app" / "tulz" / "tuplez" / "TupleComposition.scala"
) {

Expand Down
Loading

0 comments on commit 7fca17a

Please sign in to comment.