Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/vonr/set_builder
A procedural macro to create Iterators over a set defined by Haskell-inspired set-builder notation.
https://github.com/vonr/set_builder
Last synced: about 1 month ago
JSON representation
A procedural macro to create Iterators over a set defined by Haskell-inspired set-builder notation.
- Host: GitHub
- URL: https://github.com/vonr/set_builder
- Owner: Vonr
- Created: 2023-09-04T16:12:08.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2023-11-29T03:12:44.000Z (about 1 year ago)
- Last Synced: 2024-11-13T14:56:21.470Z (about 2 months ago)
- Language: Rust
- Size: 21.5 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# set_builder
A procedural macro to create Iterators over a set defined by Haskell-inspired [set-builder notation](https://wikipedia.org/wiki/Set-builder_notation).
It should be noted that these "sets" are not true sets in the sense that there is no guarantee that the members (elements) of the set are unique.
# Syntax
## Complex Set
```rust,ignore
// The pattern of the binding(s)
// │
// Mapping │ ┌── Expression that evaluate into types implementing `IntoIterator`.
// expression │ │
// │ │ │ ┌─ Predicate that evaluates to `bool`
// ▼ ▼ ▼ ▼
set![ expr : $($(pat <- expr) | $(expr)),* ]
```## Simple Enumeration Set
This is only provided for mathematical parity and returns arrays rather than Iterators,
array syntax `[...]` should always be preferred to this.```rust,ignore
// ┌─ Values to put in the set
// ▼
set![ $(expr),* ]
```# Examples
```rust
use set_builder::set;// Single-binding set with a predicate
let set = set![ x * 2 : x <- [1, 2, 3], *x > 1 ];
assert_eq!(set.collect::>(), [4, 6]);// Cartesian product without a predicate
let set = set![ (x, y) : x <- [1, 2], y <- [3, 4] ];
assert_eq!(set.collect::>(), [(1, 3), (1, 4), (2, 3), (2, 4)]);// Simple enumeration
let set = set![ 1, 2, 3, 4, 5 ];
assert_eq!(set, [1, 2, 3, 4, 5]);
```