idata

Crates.ioidata
lib.rsidata
version0.1.2
sourcesrc
created_at2018-10-13 12:40:00.277854
updated_at2020-05-23 18:22:24.956896
descriptionSmall tools to reduce mutability
homepage
repositoryhttps://github.com/jleahred/idata
max_upload_size
id89590
size20,542
José Luis (jleahred)

documentation

README

idata

At the moment this lib is for my personal use. Therefore, I will be adding new features, as soon as I need them.

Small tools to work with rust reducing mutability and programming more functional way.

  • Mutability and sharing is bad.
  • Viric mutability is bad.
  • Mutability on large pieces of code is bad.

In Rust, we can have no viric, not shared mutability. Lets use this feature.

This small tools aims to help with this points

Usage

Add to cargo.toml

[dependencies]
idata = "0.1.0"

Modifications

0.1.0   First version

TODO

At the moment this lib is for my personal use. Therefore, I will be adding new features, as soon as I need them.

Some examples

A very basic example...

   extern crate idata;
   use idata::cont::IVec;
   fn main() {
        let v = vec![1, 2];
        let v = v.ipush(3)
                 .ipush(4);
       assert!(v == vec![1,2,3,4]);
   }

Push an element to a vector, and return the same vector

    extern crate idata;
    use idata::cont::IVec;

    fn main() {
         let v = vec![1, 2];
         let v = v.ipush(3)
                  .ipush(4);

        assert!(v == vec![1,2,3,4]);
    }

Append a vector to another

    extern crate idata;
    use idata::cont::IVec;

    fn main() {
         let v1 = vec![1, 2];
         let v2 = vec![3, 4, 5];
         let v1 = v1.iappend(v2);

         assert!(v1 == vec![1,2,3,4, 5]);
    }

Remove an element from back of a vector

    extern crate idata;
    use idata::cont::IVec;

    fn main() {
         let v1 = vec![1, 2, 3, 4, 5, 6];
         let (o, v1) = v1.ipop();

         assert!(v1 == vec![1,2,3,4, 5]);
         assert!(o.unwrap() == 6);
    }

Try getting a char from top of a Chars returning the (char, remaining_chars) if possible

    extern crate idata;

    fn main() {
         let chars = "Hello world".chars();
         let (ch, chars) = idata::consume_char(chars).unwrap();

         assert!(ch == 'H');
         assert!(chars.as_str() == "ello world");
    }

Adding char to an string

    extern crate idata;
    use idata::IString;

    fn main() {
            let s = "Hello world".to_string();
            let s = s.ipush('!');

            assert!(s == "Hello world!");
    }

Adding string to an string

    extern crate idata;
    use idata::IString;

    fn main() {
            let s = "Hello world".to_string();
            let s = s.iappend("!!!");

            assert!(s == "Hello world!!!");
    }

Removing last char to a String

    extern crate idata;
    use idata::IString;
        fn main() {
        let s = "Hello world!".to_string();
        let s = s.ipop().unwrap();
            assert!(s == "Hello world");
    }

Recursive simulation with TCO

We cannot use SSA in rust combined with a for loop

It fits fine with recursion, but...alloc

Rust doesn't have TCO (tail call optimization) in recursion.

In some cases it could be expensive and even dangerous

One option, could be to use next "trampolin"

    extern crate idata;
    use idata::tc::*;

    fn main() {
            let (sum, _) = tail_call((0, 0), |(acc, counter)| {
                if counter < 101 {
                    TailCall::Call((acc + counter, counter + 1))
                } else {
                    TailCall::Return((acc, counter))
                }
            });
            assert!(sum == 5050);
    }
Commit count: 9

cargo fmt