{"id":17032607,"url":"https://github.com/andylokandy/comp-rs","last_synced_at":"2026-04-02T05:37:15.265Z","repository":{"id":57609282,"uuid":"82472205","full_name":"andylokandy/comp-rs","owner":"andylokandy","description":"Pure-macro Do notation and List-comprehension for Option, Result and Iterator.","archived":false,"fork":false,"pushed_at":"2019-10-26T18:27:12.000Z","size":18,"stargazers_count":52,"open_issues_count":2,"forks_count":4,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-06-15T04:17:51.474Z","etag":null,"topics":["do-notation","list-comprehension","macro","rust-lang"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/andylokandy.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-02-19T16:21:21.000Z","updated_at":"2024-04-24T17:17:02.000Z","dependencies_parsed_at":"2022-08-27T11:11:58.312Z","dependency_job_id":null,"html_url":"https://github.com/andylokandy/comp-rs","commit_stats":null,"previous_names":["goandylok/comp-rs"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/andylokandy/comp-rs","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andylokandy%2Fcomp-rs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andylokandy%2Fcomp-rs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andylokandy%2Fcomp-rs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andylokandy%2Fcomp-rs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/andylokandy","download_url":"https://codeload.github.com/andylokandy/comp-rs/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andylokandy%2Fcomp-rs/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263244726,"owners_count":23436478,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["do-notation","list-comprehension","macro","rust-lang"],"created_at":"2024-10-14T08:29:26.098Z","updated_at":"2026-04-02T05:37:15.216Z","avatar_url":"https://github.com/andylokandy.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# `comp-rs`\r\n\r\n[![Build Status](https://travis-ci.org/goandylok/comp-rs.svg?branch=master)](https://travis-ci.org/goandylok/comp-rs)\r\n[![crates.io](https://img.shields.io/crates/v/comp.svg)](https://crates.io/crates/comp)\r\n[![docs.rs](https://docs.rs/comp/badge.svg)](https://docs.rs/comp)\r\n\r\nPure-macro Do notation and List-comprehension for Option, Result and Iterator.\r\n\r\nIt provides syntax extensions to easily combind wrapper type (`Option`, `Result` and `Iterator`), which seems like\r\n`for-comprehension` in scala or `Do notation` in haskell.\r\n\r\n### [**Documentation**](https://docs.rs/comp/)\r\n\r\n## Usage\r\n\r\nFirst, add the following to your `Cargo.toml`:\r\n\r\n```toml\r\n[dependencies]\r\ncomp = \"*\"\r\n```\r\n\r\nNext, add this to your crate root:\r\n\r\n```rust\r\n#[macro_use]\r\nextern crate comp;\r\n```\r\n\r\n## Example\r\n\r\n`comp-rs` delivers three macros : *`option!`*, *`result!`* and *`iter!`*,\r\ntransforming the `arrow(\u003c-)` statements into FP bind (`flat_map`).\r\n\r\n### Iterator\r\n\r\n```rust\r\n#[macro_use]\r\nextern crate comp;\r\n\r\nlet iter = iter! {\r\n  let x \u003c- 0..2u8;\r\n  let y \u003c- vec!['a', 'b'];\r\n  (x, y)\r\n};\r\n\r\nfor x in iter {\r\n  println!(\"{:?}\", x);\r\n}\r\n\r\n// Print (0, 'a') (0, 'b') (1, 'a') (1, 'b')\r\n```\r\n\r\nIt can be written like generator expressions in python:\r\n\r\n```python\r\n# Python\r\nvalues = [6, 2, 9, 4, -1, 33, 87, 23]\r\nresult = (x*x for x in values if x \u003c 10)\r\n```\r\n\r\nor in scala\r\n\r\n```scala\r\n// Scala\r\nval values = Array(6, 2, 9, 4, -1, 33, 87, 23)\r\nval result = for(x \u003c- values if x \u003c 10) yield x*x\r\n```\r\n\r\nin Rust\r\n\r\n```rust\r\n// comp-rs\r\n// The result is of type FlatMap and also lazy, like Python's generator expressions.\r\nlet values = vec![6, 2, 9, 4, -1, 33, 87, 23];\r\nlet result = iter!{let x \u003c- values; if x \u003c 10; x*x};\r\n```\r\n\r\n\r\n### Option\r\n```rust\r\n#[macro_use]\r\nextern crate comp;\r\n\r\nlet option = option! {\r\n  let a \u003c- Some(1);\r\n  let b \u003c- Some(2);\r\n  a + b\r\n};\r\n\r\nassert_eq!(option, Some(3));\r\n```\r\n\r\n### Result\r\n\r\nUnlike `Iterator` and `Option`, rust provides __*Question Mark*__ syntax to combine `Result`s.\r\n\r\nLet's see how `comp-rs` makes it more explicit and expressive.\r\n\r\n#### Native way\r\n\r\n```rust\r\n#[macro_use]\r\nextern crate comp;\r\n\r\nuse std::fs::File;\r\nuse std::io;\r\nuse std::io::prelude::*;\r\n\r\n// try!() macro must be wrap into a function\r\nfn content() -\u003e io::Result\u003cString\u003e {\r\n    let mut f = try!(File::open(\"foo.txt\"));\r\n    let mut s = String::new();\r\n    try!(f.read_to_string(\u0026mut s));\r\n    Ok(s)\r\n}\r\n```\r\n\r\n#### Question mark\r\n\r\n```rust\r\n#[macro_use]\r\nextern crate comp;\r\n\r\nuse std::fs::File;\r\nuse std::io;\r\nuse std::io::prelude::*;\r\n\r\n// '?' mark must be wrap into a function\r\nfn content() -\u003e io::Result\u003cString\u003e {\r\n    let mut f = File::open(\"foo.txt\")?;\r\n    let mut s = String::new();\r\n    f.read_to_string(\u0026mut s)?;\r\n    Ok(s)\r\n}\r\n```\r\n\r\n#### `result!` way\r\n\r\n```rust\r\n#[macro_use]\r\nextern crate comp;\r\n\r\nuse std::fs::File;\r\nuse std::io;\r\nuse std::io::prelude::*;\r\n\r\nlet content: io::Result\u003cString\u003e = result! {\r\n  let mut f \u003c- File::open(\"foo.txt\");\r\n  let mut s = String::new();\r\n  let _ \u003c- f.read_to_string(\u0026mut s);\r\n  s\r\n};\r\n```\r\n\r\n## Contribution\r\n\r\nAll kinds of contribution are welcome.\r\n\r\n- **Issue** Feel free to open an issue when you find typos, bugs, or have any question.\r\n- **Pull requests**. Better implementation, more tests, more documents and typo fixes are all welcome.\r\n\r\n## License\r\n\r\nLicensed under MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandylokandy%2Fcomp-rs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fandylokandy%2Fcomp-rs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandylokandy%2Fcomp-rs/lists"}