Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Data type organization #4824

Merged
merged 65 commits into from Apr 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
65 commits
Select commit Hold shift + click to select a range
f93dadd
categorize data types in summary page.
khajavi Mar 11, 2021
29ae46c
upgrade docusaurus version.
khajavi Mar 11, 2021
1ee8535
categorize data type side bar to tree structure.
khajavi Mar 11, 2021
8805954
enable collapsible sidebar navigations.
khajavi Mar 11, 2021
bcf4df7
categorize datatype sidebar.
khajavi Mar 12, 2021
ed02126
add summary for each datatype category.
khajavi Mar 12, 2021
8cd06f9
update gitignore to exclude .bsp files.
khajavi Mar 12, 2021
ab131cb
check-in the package-lock.json file into source control.
khajavi Mar 12, 2021
917d897
add toc for the main summary.
khajavi Mar 12, 2021
e26c970
document reasons behind using Chunk over it's Array analog.
khajavi Mar 12, 2021
e797cce
document some usefull operations on creating Chunks.
khajavi Mar 12, 2021
4463499
improve fiber documentation.
khajavi Mar 14, 2021
86c3708
improve FiberRef documentation.
khajavi Mar 15, 2021
2c1d953
remove module category in data type sidebar.
khajavi Mar 15, 2021
18b0e7f
improve Ref documentation.
khajavi Mar 18, 2021
d3bd5a6
enrich the introduction section of Promise.
khajavi Mar 19, 2021
9f8b8a6
document Exit data types.
khajavi Mar 20, 2021
8d62ad7
document Cause data type.
khajavi Mar 20, 2021
a7d3662
add an example for exit data type.
khajavi Mar 20, 2021
84a89cc
improve die exit example.
khajavi Mar 20, 2021
6d7a606
change the main index page of data types to introduction.
khajavi Mar 21, 2021
3c19360
dedicate a separate section for fibers.
khajavi Mar 21, 2021
2b9e516
index exit and cause page to the introduction page.
khajavi Mar 21, 2021
6ae42bd
combine ecosystem and resources into one main section.
khajavi Mar 26, 2021
92ee273
fix zlayer address.
khajavi Mar 21, 2021
55bb4db
add slides of Error Management ZIO vs. Future talk.
khajavi Mar 22, 2021
482ed81
document how to handle different type of workloads with fibers.
khajavi Mar 24, 2021
6eb3919
add a separate page for UIO type alias.
khajavi Mar 25, 2021
7691005
add a separate page for URIO type alias.
khajavi Mar 25, 2021
4db79ba
document task type alias.
khajavi Mar 25, 2021
c1ee8ed
document RIO type alias.
khajavi Mar 25, 2021
c21eeba
document IO type alias.
khajavi Mar 25, 2021
92a178b
create a separate page for ZIO data type.
khajavi Mar 25, 2021
0c46df3
index type aliases in summary and overview sections.
khajavi Mar 25, 2021
96a6ab8
document Runtime data type.
khajavi Mar 25, 2021
697692c
add a finalizing section to the resource management section for ZIO d…
khajavi Mar 25, 2021
e6c5301
improve bracket section.
khajavi Mar 25, 2021
3f68f17
refactor finalizer sample code.
khajavi Mar 26, 2021
1e67a86
add a note on finalizers, on resource management.
khajavi Mar 26, 2021
6f676ab
add a new section about unswallowed exceptions.
khajavi Mar 26, 2021
5cd4adb
document unstoppable behaviour of finalizers.
khajavi Mar 26, 2021
fdec8eb
document RefM data type in concurrency primitive section.
khajavi Mar 26, 2021
9f8d402
change the note style and link to the ref data type.
khajavi Mar 26, 2021
2a217b2
document ZRef and ZRefM.
khajavi Mar 27, 2021
53aa427
add Natan Silnitsky article on avoiding pitfals when writing ZIO apps.
khajavi Mar 27, 2021
7a09eb3
add a note about task usecase.
khajavi Mar 27, 2021
148ff2b
categorize creation methods.
khajavi Mar 27, 2021
30aced7
move type alias specific documentations to their related pages.
khajavi Mar 27, 2021
8512c9c
document all functions for lifting pure data types.
khajavi Mar 27, 2021
cc8bf55
restructure mapping section.
khajavi Mar 27, 2021
31fe615
document mapEffect.
khajavi Mar 27, 2021
c142e50
list all other ways of lifting and importing functions into ZIO data …
khajavi Mar 28, 2021
ca2a9b9
copy parallelism from overview section.
khajavi Mar 28, 2021
5cca1b2
copy time out section of overview into ZIO data type.
khajavi Mar 28, 2021
b0a9a80
add zipping section into the ZIO data type.
khajavi Mar 28, 2021
a57bf17
document various ways of creating ZIO effect.
khajavi Mar 29, 2021
e243cb6
add error management section.
khajavi Mar 29, 2021
04a569e
update chaining section.
khajavi Mar 29, 2021
0a43dc7
update mapping section.
khajavi Mar 29, 2021
2d40034
reorder chaining and zipping sections.
khajavi Mar 29, 2021
01d7855
hide import line.
khajavi Mar 29, 2021
f4e7802
add introduction paragraph to retry.
khajavi Mar 30, 2021
8826e55
update schedule documentation.
khajavi Apr 1, 2021
2151cbd
add service section.
khajavi Apr 1, 2021
9983a54
document console service.
khajavi Apr 1, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -19,3 +19,4 @@ website/node_modules
website/build
website/i18n/en.json
website/yarn.lock
.bsp/
85 changes: 0 additions & 85 deletions docs/datatypes/chunk.md

This file was deleted.

12 changes: 12 additions & 0 deletions docs/datatypes/concurrency/index.md
@@ -0,0 +1,12 @@
---
id: index
title: "Summary"
---

- **[Promise](promise.md)** — A `Promise` is a model of a variable that may be set a single time, and awaited on by many fibers.
- **[Semaphore](semaphore.md)** — A `Semaphore` is an asynchronous (non-blocking) semaphore that plays well with ZIO's interruption.
- **[ZRef](zref.md)** — A `ZRef[EA, EB, A, B]` is a polymorphic, purely functional description of a mutable reference. The fundamental operations of a `ZRef` are `set` and `get`.
+ **[Ref](ref.md)** — `Ref[A]` models a mutable reference to a value of type `A`. The two basic operations are `set`, which fills the `Ref` with a new value, and `get`, which retrieves its current content. All operations on a `Ref` are atomic and thread-safe, providing a reliable foundation for synchronizing concurrent programs.
- **[ZRefM](zrefm.md)** — A `ZRefM[RA, RB, EA, EB, A, B]` is a polymorphic, purely functional description of a mutable reference.
+ **[RefM](refm.md)** — `RefM[A]` models a **mutable reference** to a value of type `A` in which we can store **immutable** data, and update it atomically **and** effectfully.
- **[Queue](queue.md)** — A `Queue` is an asynchronous queue that never blocks, which is safe for multiple concurrent producers and consumers.
40 changes: 21 additions & 19 deletions docs/datatypes/promise.md → docs/datatypes/concurrency/promise.md
@@ -1,19 +1,27 @@
---
id: datatypes_promise
title: "Promise"
id: promise
title: "Promise"
---

A `Promise[E, A]` is a variable of `IO[E, A]` type that can be set exactly once.

Promises are used to build higher level concurrency primitives, and are often used in situations where multiple `Fiber`s
need to coordinate passing values to each other.
Promise is a **purely functional synchronization primitive** which represents a single value that may not yet be available. When we create a Promise, it always started with an empty value, then it can be completed exactly once at some point, and then it will never become empty or modified again.

## Creation
Promise is a synchronization primitive. So, it is useful whenever we want to wait for something to happen. Whenever we need to synchronize a fiber with another fiber, we can use Promise. It allows us to have fibers waiting for other fibers to do things. Any time we want to handoff of a work from one fiber to another fiber or anytime we want to suspend a fiber until some other fiber does a certain amount of work, well we need to be using a Promise. Also, We can use `Promise` with `Ref` to build other concurrent primitives, like Queue and Semaphore.

By calling `await` on the Promise, the current fiber blocks, until that event happens. As we know, blocking thread in ZIO, don't actually block kernel threads. They are semantic blocking, so when a fiber is blocked the underlying thread is free to run all other fibers.

Promise is the equivalent of Scala's Promise. It's almost the same, except it has two type parameters, instead of one. Also instead of calling `future`, we need to call `await` on ZIO Promise to wait for the Promise to be completed.

Promises can be failed with a value of type `E` and succeeded that is completed with success with the value of type `A`. So there are two ways we can complete a Promise, with failure or success and then whoever is waiting on the Promise will get back that failure or success.

Promises can be created using `Promise.make[E, A]`, which returns `UIO[Promise[E, A]]`. This is a description of creating a promise, but not the actual promise. Promises cannot be created outside of IO, because creating them involves allocating mutable memory, which is an effect and must be safely captured in IO.

## Operations

### Creation

Promises can be created using `Promise.make[E, A]`, which returns `UIO[Promise[E, A]]`. This is a description of creating a promise, but not the actual promise. Promises cannot be created outside of IO, because creating them involves allocating mutable memory, which is an effect and must be safely captured in IO.

### Completing

You can complete a `Promise[E, A]` in few different ways:
Expand Down Expand Up @@ -44,9 +52,7 @@ val race: IO[String, Int] = for {
} yield value
```

The act of completing a Promise results in an `UIO[Boolean]`, where
the `Boolean` represents whether the promise value has been set (`true`) or whether it was already set (`false`).
This is demonstrated below:
The act of completing a Promise results in an `UIO[Boolean]`, where the `Boolean` represents whether the promise value has been set (`true`) or whether it was already set (`false`). This is demonstrated below:

```scala mdoc:silent
val ioPromise1: UIO[Promise[Exception, String]] = Promise.make[Exception, String]
Expand All @@ -64,7 +70,7 @@ To re-iterate, the `Boolean` tells us whether or not the operation took place su
was set with the value or the error.

### Awaiting
You can get a value from a Promise using `await`, calling fiber will suspend until Promise is completed.
We can get a value from a Promise using `await`, calling fiber will suspend until Promise is completed.

```scala mdoc:silent
val ioPromise3: UIO[Promise[Exception, String]] = Promise.make[Exception, String]
Expand All @@ -73,18 +79,16 @@ val ioGet: IO[Exception, String] = ioPromise3.flatMap(promise => promise.await)

### Polling
The computation will suspend (in a non-blocking fashion) until the Promise is completed with a value or an error.
If you don't want to suspend and you only want to query the state of whether or not the Promise has been completed,
you can use `poll`:

If we don't want to suspend, and we only want to query the state of whether or not the Promise has been completed, we can use `poll`:

```scala mdoc:silent
val ioPromise4: UIO[Promise[Exception, String]] = Promise.make[Exception, String]
val ioIsItDone: UIO[Option[IO[Exception, String]]] = ioPromise4.flatMap(p => p.poll)
val ioIsItDone2: IO[Option[Nothing], IO[Exception, String]] = ioPromise4.flatMap(p => p.poll.get)
```

If the Promise was not completed when you called `poll` then the IO will fail with the `Unit` value otherwise,
you obtain an `IO[E, A]`, where `E` represents if the Promise completed with an error and `A` indicates
that the Promise successfully completed with an `A` value.
If the Promise was not completed when we called `poll` then the IO will fail with the `Unit` value otherwise, we obtain an `IO[E, A]`, where `E` represents if the Promise completed with an error and `A` indicates that the Promise successfully completed with an `A` value.

`isDone` returns `UIO[Boolean]` that evaluates to `true` if promise is already completed.

Expand All @@ -105,10 +109,8 @@ val program: ZIO[Console with Clock, IOException, Unit] =
fiberA <- sendHelloWorld.fork
fiberB <- getAndPrint.fork
_ <- (fiberA zip fiberB).join
} yield ()
} yield ()
```

In the example above, we create a Promise and have a Fiber (`fiberA`) complete that promise after 1 second and a second
Fiber (`fiberB`) will call `await` on that Promise to obtain a `String` and then print it to screen. The example prints
`hello world` to the screen after 1 second. Remember, this is just a description of the program and not the execution
In the example above, we create a Promise and have a Fiber (`fiberA`) complete that promise after 1 second and a second Fiber (`fiberB`) will call `await` on that Promise to obtain a `String` and then print it to screen. The example prints `hello world` to the screen after 1 second. Remember, this is just a description of the program and not the execution
itself.
@@ -1,6 +1,6 @@
---
id: datatypes_queue
title: "Queue"
id: queue
title: "Queue"
---

`Queue` is a lightweight in-memory queue built on ZIO with composable and transparent back-pressure. It is fully asynchronous (no locks or blocking), purely-functional and type-safe.
Expand Down