Skip to content

Commit

Permalink
Added chunk to list
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-mcdaniel committed May 4, 2024
1 parent 79ef81c commit ae6eb2a
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/binary/init/list_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ pub fn get() -> Expression {
"get the tail of a list"),
String::from("head") => Expression::builtin("head", head,
"get the head of a list"),
String::from("chunk") => Expression::builtin("chunk", chunk,
"chunk a list into lists of n elements"),
String::from("cons") => Expression::builtin("cons", cons,
"prepend an element to a list"),
String::from("append") => Expression::builtin("append", append,
Expand All @@ -36,6 +38,7 @@ pub fn get() -> Expression {
"split a list at a given index"),
String::from("nth") => Expression::builtin("nth", nth,
"get the nth element of a list"),

})
.into()
}
Expand Down Expand Up @@ -80,6 +83,41 @@ fn head(args: Vec<Expression>, env: &mut Environment) -> Result<Expression, Erro
}
}

fn chunk(args: Vec<Expression>, env: &mut Environment) -> Result<Expression, Error> {
if args.len() != 2 {
return Err(Error::CustomError(
"chunk requires exactly two arguments".to_string(),
));
}
let n = args[0].eval(env)?;
let list = args[1].eval(env)?;
if let Expression::Integer(n) = n {
if let Expression::List(list) = list {
let mut result = vec![];
let mut chunk = vec![];
for item in list {
chunk.push(item);
if chunk.len() == n as usize {
result.push(Expression::List(chunk));
chunk = vec![];
}
}
if !chunk.is_empty() {
result.push(Expression::List(chunk));
}
Ok(Expression::List(result))
} else {
Err(Error::CustomError(
"chunk requires a list as its second argument".to_string(),
))
}
} else {
Err(Error::CustomError(
"chunk requires an integer as its first argument".to_string(),
))
}
}

fn cons(args: Vec<Expression>, env: &mut Environment) -> Result<Expression, Error> {
if args.len() != 2 {
return Err(Error::CustomError(
Expand Down

0 comments on commit ae6eb2a

Please sign in to comment.