Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/giacomocavalieri/tote
👜 Bags (or multisets) in Gleam
https://github.com/giacomocavalieri/tote
bag multiset
Last synced: about 2 months ago
JSON representation
👜 Bags (or multisets) in Gleam
- Host: GitHub
- URL: https://github.com/giacomocavalieri/tote
- Owner: giacomocavalieri
- License: apache-2.0
- Created: 2023-10-22T20:18:07.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2024-02-10T22:01:11.000Z (10 months ago)
- Last Synced: 2024-03-14T19:05:39.058Z (9 months ago)
- Topics: bag, multiset
- Language: Gleam
- Homepage: https://hexdocs.pm/tote/
- Size: 32.2 KB
- Stars: 6
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-gleam - tote - [📚](https://hexdocs.pm/tote/) - Bags (or multisets) in Gleam (Packages / Data Structures)
README
# tote
[![Package Version](https://img.shields.io/hexpm/v/tote)](https://hex.pm/packages/tote)
[![Hex Docs](https://img.shields.io/badge/hex-docs-ffaff3)](https://hexdocs.pm/tote/)👜 Bags (or multisets) in Gleam
> ⚙️ This package works for both the Erlang and JavaScript target
## What's a bag?
A `Bag` is a data structure similar to a set with the difference that it
can store multiple copies of the same item.This means that you can efficiently check if an element is inside a bag with
the [contains](https://hexdocs.pm/tote/tote/bag.html#contains) function,
just like a set.
At the same time, you can ask how many copies of an item are contained inside a
bag using the [copies](https://hexdocs.pm/tote/tote/bag.html#copies) function.> If you are curious about implementation details, a `Bag(a)` is nothing
> more than a handy wrapper around a `Map(a, Int)` that is used to keep
> track of the number of occurrences of each item.## Installation
To add this package to your Gleam project:
```sh
gleam add tote
```## Getting started
All the bag-related functions are in the `tote/bag` module, so once you've
imported that you're good to go:```gleam
import gleam/int
import gleam/io
import tote/bagpub fn main() {
let valentino_bag =
bag.from_list([
"lipstick", "in", "my", "wh-", "wh-", "lipstick",
"in", "my", "Valentino", "white", "bag", "?!"
])bag.copies(of: "lipstick", in: valentino_bag)
|> int.to_string
|> io.println
// -> 2
}
```If you don't get the Valentino reference,
[you're welcome!](https://www.youtube.com/watch?v=IzTlPjgJfTk)## Contributing
If you think there's any way to improve this package, or if you spot a bug don't
be afraid to open PRs, issues or requests of any kind! Any contribution is
welcome 💜