Giter Site home page Giter Site logo

iterable's Introduction

Iterable

An iterable library for Rust collection like types.

Installation

# Cargo.toml
[dependencies]
iterable = "0.6"

Features

iterate collection like types without iter() and collect():

use iterable::*;

fn main() {
    // convert a vec of i32 to a vec of String
    let v = vec![1, 2, 3];
    // only one `map` function instead of `v.iter().map(|i| i.to_string()).collect()`
    let res = v.map(|i| i.to_string());
    assert_eq!(res, vec!["1".to_string(), "2".to_string(), "3".to_string()]);

    // iterable also support array and string
    let v = [1, 2, 3];
    // res's type: [i32; 3]
    let res = v.rev();
    assert_eq!(res, [3, 2, 1]);
    
    // lazy combinator
    let v = vec![1,2,3];
    let s = v
        .lazy_filter(|x| x > &1)
        .lazy_map(|x| x.to_string())
        .rev();
    assert_eq!(s, vec!["3".to_string(), "2".to_string()]);

    // iterate over reference
    let v = vec![1, 2, 3];
    let res = (&v).filter(|i| i > &&1);
    assert_eq!(res, vec![&2, &3]);

    // functional append
    let a = vec![1, 2, 3];
    let res = a.add_one(1);
    assert_eq!(res, vec![1, 2, 3 ,1]);

    // a bunch of try methods: try_map, try_add_one, try_flat_map, try_flatten
    let a = vec![1, 2, 3];
    let res = a.try_map(|x| Some(x));
    assert_eq!(res, Some(vec![1, 2, 3]));

    let a = vec![1, 2, 3];
    let res: Result<_,()> = a.try_map(|x| Ok(x));
    assert_eq!(res, Ok(vec![1, 2, 3]));
}

License

MIT

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.