Skip to content
/ idata Public

small tools to reduce mutability and program more functionally with Rust

License

Notifications You must be signed in to change notification settings

jleahred/idata

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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);
    }

About

small tools to reduce mutability and program more functionally with Rust

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages