Skip to content

Commit

Permalink
Add docs to README
Browse files Browse the repository at this point in the history
  • Loading branch information
julianpeeters committed Dec 16, 2023
1 parent fc8a322 commit 5999618
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 28 deletions.
70 changes: 60 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@

Common typeclasses and constructors, but parameterized by `A` instead of `F[_]`

### Why?
### Libraries for Scala 3 (JS, JVM, and Native platforms)
- [`destructured-cats`](#destructured-cats): typeclasses that provide cats typeclasses for the underlying functor, e.g., `Applicative[Option]`
- [`destructured-scala`](#destructured-scala): typeclasses that provide the underlying data constructors of a type `A`, e.g., `Some[T]`


This might be useful if your model uses subtypes in its definition.
#### Why?

This library can be useful if your model uses subtypes in its definition.

For example, if the compiler knows that a type `A`, is, at a call site, a
`Some[T]` or a `None.type`, then `destructured` typeclasses can be used to
Expand All @@ -20,16 +25,22 @@ val b: Option[String] = f(a).pure("foo")
// b: Option[String] = Some(value = "foo")
```

## Libraries for Scala 3 (JS, JVM, and Native platforms)
- [`destructured-cats`](#destructured-cats): typeclasses that provide common cats typeclasses, e.g., `Applicative[Option]`
- [`destructured-scala`](#destructured-scala): typeclasses that provide the underlying data constructors of a type `A`, e.g., `Some[T]`
<small>(for a more realistic example, see the [dynamical](https://github.com/julianpeeters/dynamical) library)</small>

### `destructured-cats`

## `destructured-cats`

```scala
"com.julianpeeters" %% "destructured-cats" % "0.0.0"
```

Supported types: `Applicative[Option]`, `Functor[Option]`, `// TODO`

##### Examples:

##### `ApplicativeOf`


```scala
import destructured.cats.{ApplicativeOf, given}

Expand All @@ -40,18 +51,57 @@ val b: Option[String] = f(a).pure("foo")
// b: Option[String] = Some(value = "foo")
```

### `destructured-scala`

##### `FunctorOf`

```scala
import destructured.cats.{FunctorOf, given}

val fa: Option[Int] = Option(1)
// fa: Option[Int] = Some(value = 1)
def f[A](a: A)(using A: FunctorOf[A]): FunctorOf[A] = A
val fb: Option[Int] = f(fa).map(fa)(_ + 1)
// fb: Option[Int] = Some(value = 2)
```




## `destructured-scala`

```scala
"com.julianpeeters" %% "destructured-scala" % "0.0.0"
```

The following constructors are supported:

| Option | Either |
| :---: | :---: |
| Some | Left |
| None | Right |

##### Examples:

##### `Some[T]`

```scala
import destructured.scala.{CtorOf, given}

def f[A](a: A)(using A: CtorOf[A]): CtorOf[A] = A
val a: Some[Int] = Some(1)
// a: Some[Int] = Some(value = 1)
val b: Option[String] = f(a)("foo")
// b: Option[String] = Some(value = "foo")
def f[A](a: A)(using A: CtorOf[A]): CtorOf[A] = A
val b: Some[String] = f(a).apply("foo")
// b: Some[String] = Some(value = "foo")
```

##### `Either[L, R]`

```scala
import destructured.scala.{CtorOf, given}

val a: Right[Boolean, Int] = Right(1)
// a: Right[Boolean, Int] = Right(value = 1)
def f[A](a: A)(using A: CtorOf[A]): CtorOf[A] = A
val b: Right[Boolean, String] = f(a).apply("foo")
// b: Right[Boolean, String] = Right(value = "foo")
```
72 changes: 60 additions & 12 deletions docs/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,94 @@

Common typeclasses and constructors, but parameterized by `A` instead of `F[_]`

### Why?
### Libraries for Scala @SCALA@ (JS, JVM, and Native platforms)
- [`destructured-cats`](#destructured-cats): typeclasses that provide cats typeclasses for the underlying functor, e.g., `Applicative[Option]`
- [`destructured-scala`](#destructured-scala): typeclasses that provide the underlying data constructors of a type `A`, e.g., `Some[T]`


This might be useful if your model uses subtypes in its definition.
#### Why?

This library can be useful if your model uses subtypes in its definition.

For example, if the compiler knows that a type `A`, is, at a call site, a
`Some[T]` or a `None.type`, then `destructured` typeclasses can be used to
summon `cats` `Applicative` typeclass for the underlying `Option`:

```scala mdoc
```scala mdoc:reset
import destructured.cats.{ApplicativeOf, given}

def f[A](a: A)(using A: ApplicativeOf[A]): ApplicativeOf[A] = A
val a: Some[Int] = Some(1)
val b: Option[String] = f(a).pure("foo")
```

## Libraries for Scala @SCALA@ (JS, JVM, and Native platforms)
- [`destructured-cats`](#destructured-cats): typeclasses that provide common cats typeclasses, e.g., `Applicative[Option]`
- [`destructured-scala`](#destructured-scala): typeclasses that provide the underlying data constructors of a type `A`, e.g., `Some[T]`
<small>(for a more realistic example, see the [dynamical](https://github.com/julianpeeters/dynamical) library)</small>

### `destructured-cats`

| ApplicativeOf| Supported data types |
| ------------- | ------------- |
| Option | Content Cell |
## `destructured-cats`

```scala
"com.julianpeeters" %% "destructured-cats" % "@VERSION@"
```

```scala mdoc
Supported types: `Applicative[Option]`, `Functor[Option]`, `// TODO`

##### Examples:

##### `ApplicativeOf`


```scala mdoc:reset
import destructured.cats.{ApplicativeOf, given}

def f[A](a: A)(using A: ApplicativeOf[A]): ApplicativeOf[A] = A
val a: Some[Int] = Some(1)
val b: Option[String] = f(a).pure("foo")
```

### `destructured-scala`

##### `FunctorOf`

```scala mdoc:reset
import destructured.cats.{FunctorOf, given}

val fa: Option[Int] = Option(1)
def f[A](a: A)(using A: FunctorOf[A]): FunctorOf[A] = A
val fb: Option[Int] = f(fa).map(fa)(_ + 1)
```


## `destructured-scala`

```scala
"com.julianpeeters" %% "destructured-scala" % "@VERSION@"
```

The following constructors are supported:

| Option | Either |
| :---: | :---: |
| Some | Left |
| None | Right |

##### Examples:

##### `Some[T]`

```scala mdoc:reset
import destructured.scala.{CtorOf, given}

val a: Some[Int] = Some(1)
def f[A](a: A)(using A: CtorOf[A]): CtorOf[A] = A
val b: Some[String] = f(a).apply("foo")
```

##### `Either[L, R]`

```scala mdoc:reset
import destructured.scala.{CtorOf, given}

val a: Right[Boolean, Int] = Right(1)
def f[A](a: A)(using A: CtorOf[A]): CtorOf[A] = A
val b: Right[Boolean, String] = f(a).apply("foo")
```
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ type Func[X, B] = X match

given [A] (using F: Functor[Option]): FunctorOf[Option[A]] =
new FunctorOf[Option[A]]:
def map[B](fa: FA[Option[A]])(f: Func[Option[A], B]): FB[Option[A], B] =
F.map(fa)(f)
def map[B](fa: FA[Option[A]])(f: Func[Option[A], B]): FB[Option[A], B] =
F.map(fa)(f)

given [A] (using F: Functor[Option]): FunctorOf[Some[A]] =
new FunctorOf[Some[A]]:
def map[B](fa: FA[Some[A]])(f: Func[Some[A], B]): FB[Some[A], B] =
F.map(fa)(f)
def map[B](fa: FA[Some[A]])(f: Func[Some[A], B]): FB[Some[A], B] =
F.map(fa)(f)

given (using F: Functor[Option]): FunctorOf[None.type] =
new FunctorOf[None.type]:
def map[B](fa: FA[None.type])(f: Func[None.type, B]): FB[None.type, B] =
F.map(fa)(f)
def map[B](fa: FA[None.type])(f: Func[None.type, B]): FB[None.type, B] =
F.map(fa)(f)

0 comments on commit 5999618

Please sign in to comment.