<commit_before>use std::fmt;

use std::rc::Rc;

pub struct SList<T> {
    head: Rc<Node<T>>
}

impl<T: Clone> SList<T> {
    pub fn head(obj: T) -> SList<T> {
        SList {
            head: Rc::new(Node {
                next: None,
                value: obj,
            })
        }
    }

    pub fn plus(&self, obj: T) -> SList<T> {
        SList {
            head: Rc::new(Node {
                next: Some(self.head.clone()),
                value: obj
            })
        }
    }

    pub fn inner(&self) -> &T {
        &self.head.value
    }

    pub fn to_vec(&self) -> Vec<T> {
        let mut ret = Vec::new();
        let mut val = &self.head;
        loop {
            ret.push(val.value.clone());
            if let Some(ref next) = val.next {
                val = next;
            } else {
                break;
            }
        }
        ret.reverse();
        ret
    }
}

impl<T: fmt::Display> fmt::Display for SList<T>
where T: Clone + fmt::Debug {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:?}", self.to_vec())
    }
}

#[derive(Debug)]
struct Node<T> {
    next: Option<Rc<Node<T>>>,
    value: T,
}<commit_msg>replace nice to_vec with iterator based solution<commit_after>use std::fmt;

use std::rc::Rc;

pub struct SList<T> {
    head: Rc<Node<T>>
}

impl<T: Clone> SList<T> {
    pub fn head(obj: T) -> SList<T> {
        SList {
            head: Rc::new(Node {
                next: None,
                value: obj,
            })
        }
    }

    pub fn plus(&self, obj: T) -> SList<T> {
        SList {
            head: Rc::new(Node {
                next: Some(self.head.clone()),
                value: obj
            })
        }
    }

    pub fn inner(&self) -> &T {
        &self.head.value
    }

    pub fn to_vec(&self) -> Vec<T> {
        let mut ret: Vec<T> = self.iter().collect();
        ret.reverse();
        ret
    }

    pub fn iter(&self) -> SListIter<T> {
        SListIter {
            next: Some(self.head.clone())
        }
    }
}

pub struct SListIter<T> {
    next: Option<Rc<Node<T>>>,
}

impl<T: Clone> Iterator for SListIter<T> {
    type Item = T;

    fn next(&mut self) -> Option<Self::Item> {
        if self.next.is_none() {
            return None;
        }
        let list = self.next.as_ref().unwrap().clone();
        self.next = list.next.clone();
        Some(list.value.clone())
    }
}

impl<T: fmt::Display> fmt::Display for SList<T>
where T: Clone + fmt::Debug {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:?}", self.to_vec())
    }
}

#[derive(Debug)]
struct Node<T> {
    next: Option<Rc<Node<T>>>,
    value: T,
}
{'fraction_non_alphanumeric': 0.16802973977695168, 'fraction_numerical': 0.0, 'mean_word_length': 1.5751196172248805, 'pattern_counts': {'":': 0, '<': 40, '<?xml version=': 0, '>': 52, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1620306', 'n_tokens_mistral': 961, 'n_tokens_neox': 946, 'n_words': 208}