Code Monkey home page Code Monkey logo

comp-rs's Introduction

comp-rs

Build Status crates.io docs.rs

Pure-macro Do notation and List-comprehension for Option, Result and Iterator.

It provides syntax extensions to easily combind wrapper type (Option, Result and Iterator), which seems like for-comprehension in scala or Do notation in haskell.

Usage

First, add the following to your Cargo.toml:

[dependencies]
comp = "*"

Next, add this to your crate root:

#[macro_use]
extern crate comp;

Example

comp-rs delivers three macros : option!, result! and iter!, transforming the arrow(<-) statements into FP bind (flat_map).

Iterator

#[macro_use]
extern crate comp;

let iter = iter! {
  let x <- 0..2u8;
  let y <- vec!['a', 'b'];
  (x, y)
};

for x in iter {
  println!("{:?}", x);
}

// Print (0, 'a') (0, 'b') (1, 'a') (1, 'b')

It can be written like generator expressions in python:

# Python
values = [6, 2, 9, 4, -1, 33, 87, 23]
result = (x*x for x in values if x < 10)

or in scala

// Scala
val values = Array(6, 2, 9, 4, -1, 33, 87, 23)
val result = for(x <- values if x < 10) yield x*x

in Rust

// comp-rs
// The result is of type FlatMap and also lazy, like Python's generator expressions.
let values = vec![6, 2, 9, 4, -1, 33, 87, 23];
let result = iter!{let x <- values; if x < 10; x*x};

Option

#[macro_use]
extern crate comp;

let option = option! {
  let a <- Some(1);
  let b <- Some(2);
  a + b
};

assert_eq!(option, Some(3));

Result

Unlike Iterator and Option, rust provides Question Mark syntax to combine Results.

Let's see how comp-rs makes it more explicit and expressive.

Native way

#[macro_use]
extern crate comp;

use std::fs::File;
use std::io;
use std::io::prelude::*;

// try!() macro must be wrap into a function
fn content() -> io::Result<String> {
    let mut f = try!(File::open("foo.txt"));
    let mut s = String::new();
    try!(f.read_to_string(&mut s));
    Ok(s)
}

Question mark

#[macro_use]
extern crate comp;

use std::fs::File;
use std::io;
use std::io::prelude::*;

// '?' mark must be wrap into a function
fn content() -> io::Result<String> {
    let mut f = File::open("foo.txt")?;
    let mut s = String::new();
    f.read_to_string(&mut s)?;
    Ok(s)
}

result! way

#[macro_use]
extern crate comp;

use std::fs::File;
use std::io;
use std::io::prelude::*;

let content: io::Result<String> = result! {
  let mut f <- File::open("foo.txt");
  let mut s = String::new();
  let _ <- f.read_to_string(&mut s);
  s
};

Contribution

All kinds of contribution are welcome.

  • Issue Feel free to open an issue when you find typos, bugs, or have any question.
  • Pull requests. Better implementation, more tests, more documents and typo fixes are all welcome.

License

Licensed under MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)

comp-rs's People

Contributors

andylokandy avatar dereckson avatar willi-kappler avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

comp-rs's Issues

Is Future support planned?

Pure-macro Do notation and List-comprehension for Option, Result and Iterator.

Will Future and Stream also appear on the list as a logical continutaion?

rust-mdo

Hi!

You may be interrested by TeXitoi/rust-mdo (I know, self promotion...)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.