# Primitives

> Rust-backed core types: Either, Validated, Option, Try, immutable collections and serde.

Source: https://docs.pyrula.io/primitives/

Typed values implemented in Rust through PyO3. The workflow, agent and Kafka packages build on them.

`Either`, `Option` and `Try` follow Scala's standard library, `Validated` follows Cats, and `IList`/`IMap` are immutable and structurally shared like Scala's `List`/`Map`. `@sealed` and `@case` with `match` correspond to `sealed trait` and `case class`, and [do-notation](https://docs.pyrula.io/primitives/do-notation.md) to the for-comprehension. Methods use snake_case (`flat_map`, `get_or_else`).

- [Either](https://docs.pyrula.io/primitives/either.md): a value or an error, without exceptions.
- [Validated](https://docs.pyrula.io/primitives/validated.md): like `Either`, but accumulates every error.
- [Option](https://docs.pyrula.io/primitives/option.md): present or absent.
- [Try](https://docs.pyrula.io/primitives/try.md): a result or the exception that broke it.
- [Collections](https://docs.pyrula.io/primitives/collections.md): `IList`, `IMap` and [`ISet`](https://docs.pyrula.io/primitives/immutable-set.md).
- [NonEmptyList](https://docs.pyrula.io/primitives/non-empty-list.md): `head` and `reduce` are total.
- [Numeric lists](https://docs.pyrula.io/primitives/numeric-lists.md): compact typed lists for `int` and `float`.
- [IntSet](https://docs.pyrula.io/primitives/int-set.md): a Roaring-bitmap set of integers.
- [Duration](https://docs.pyrula.io/primitives/duration.md): a typed time span.
- [serde](https://docs.pyrula.io/primitives/serde.md): typed encode and decode.
- [par_map](https://docs.pyrula.io/primitives/par-map.md): process-parallel map.

```python
from pyrula import Either, Validated, Option, Try
```

`Either[T, E]` and `Validated[T, E]` put the success type first. Cases subclass the generic base, so `isinstance(Ok(1), Either)` holds, and payload-bearing cases support positional `match` patterns. `Nothing` is a singleton.

`traverse` and `sequence` collect results into a `list`. `Either`, `Option` and `Try` stop at the first failure; `Validated` runs every check.

Also: [Modeling](https://docs.pyrula.io/primitives/modeling.md) with `@case`, [Composition](https://docs.pyrula.io/primitives/composition.md), [Examples](https://docs.pyrula.io/primitives/examples.md).
