Struct functils::list::List [] [src]

pub struct List<A> { /* fields omitted */ }

The basic data structure used in functional programming. A list acts like an array. You can't do indexing on it, but you can add too and grow it by using cons or you can create a List with the list!() macro.

Methods

impl<A> List<A>
[src]

Create an empty list

Add a new item to the front of the list

Example

use functils::list::List;

// []
let mut list1 = List::new();

// [ 1 ]
list1.cons(1);

// [ 2 1 ]
list1.cons(2);

println!("{}",list1);

Given another List, combine them together.

Example

use functils::list::List;

let mut list1 = List::new();
let mut list2 = List::new();
let mut list_cmp = List::new();

// list1 = [ 1 ]
list1.cons(1);

// list2 = [ 3 ]
list2.cons(3);

// list_cmp = [ 1 3 ]
list_cmp.cons(3);
list_cmp.cons(1);

// list1 = [ 1 3 ] and now list2 is gone
list1.append(list2);

assert_eq!(list1, list_cmp);

If the List is empty return true, else false

Example

use functils::list::List;

let mut list1 = List::new();

assert!(list1.null());
list1.cons(1);
assert!(!list1.null());

Returns the first element of a List if it exists this removes it from the List.

Example

use functils::list::List;

let mut list1 = List::new();
list1.cons(1);
list1.cons(2);

assert_eq!(list1.head(), Some(2));

Tail returns a modified version of a list and consumes the original. It returns a List with all but the first element in it.

Example

use functils::list::List;

let mut list1 = List::new();
let mut list_cmp = List::new();
list1.cons(1);
list1.cons(2);
list1.cons(3);
list_cmp.cons(1);
list_cmp.cons(2);

// [ 2 1 ] == [ 2 1]
assert_eq!(list1.tail(), list_cmp);

Grabs the head of a list and the tail returned as a tuple. If the head doesn't exist return None.

Example

use functils::list::List;

let mut list1 = List::new();
list1.cons(3);
list1.cons(2);
list1.cons(1);

let mut list_cmp = List::new();
list_cmp.cons(3);
list_cmp.cons(2);

assert_eq!( list1.uncons(), Some((1,list_cmp)) );

let empty: List<String> = List::new();

assert_eq!( empty.uncons(), None );

Trait Implementations

impl<A: Debug> Debug for List<A>
[src]

Formats the value using the given formatter.

impl<A: Clone> Clone for List<A>
[src]

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

impl<A: Eq> Eq for List<A>
[src]

impl<A: PartialEq> PartialEq for List<A>
[src]

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

impl<A: Ord> Ord for List<A>
[src]

This method returns an Ordering between self and other. Read more

impl<A: PartialOrd> PartialOrd for List<A>
[src]

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

impl<A: Display> Display for List<A>
[src]

Formats the value using the given formatter. Read more