GithubHelp home page GithubHelp logo

jleahred / idata Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 1.0 9 KB

small tools to reduce mutability and program more functionally with Rust

License: GNU Lesser General Public License v3.0

Rust 100.00%
rust mutability functional-programming

idata's Introduction

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

idata's People

Contributors

jleahred avatar

Watchers

 avatar  avatar

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.