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]
fn new() -> List<A>
Create an empty list
fn cons(&mut self, item: A)
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);
fn append(&mut self, item: List<A>)
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);
fn null(&self) -> bool
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());
fn head(&mut self) -> Option<A>
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));
fn tail(self) -> List<A>
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);
fn uncons(self) -> Option<(A, List<A>)>
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]
impl<A: Clone> Clone for List<A>
[src]
fn clone(&self) -> List<A>
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0
Performs copy-assignment from source
. Read more
impl<A: Eq> Eq for List<A>
[src]
impl<A: PartialEq> PartialEq for List<A>
[src]
fn eq(&self, __arg_0: &List<A>) -> bool
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, __arg_0: &List<A>) -> bool
This method tests for !=
.
impl<A: Ord> Ord for List<A>
[src]
fn cmp(&self, __arg_0: &List<A>) -> Ordering
This method returns an Ordering
between self
and other
. Read more
impl<A: PartialOrd> PartialOrd for List<A>
[src]
fn partial_cmp(&self, __arg_0: &List<A>) -> Option<Ordering>
This method returns an ordering between self
and other
values if one exists. Read more
fn lt(&self, __arg_0: &List<A>) -> bool
This method tests less than (for self
and other
) and is used by the <
operator. Read more
fn le(&self, __arg_0: &List<A>) -> bool
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
fn gt(&self, __arg_0: &List<A>) -> bool
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
fn ge(&self, __arg_0: &List<A>) -> bool
This method tests greater than or equal to (for self
and other
) and is used by the >=
operator. Read more