Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion ndc_lib/src/stdlib/sequence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ mod inner {
)
}

//// Returns al prefixes of a sequence, each as a list.
/// Returns al prefixes of a sequence, each as a list.
pub fn prefixes(seq: &mut Sequence) -> Value {
// Special case for string which is more efficient and doesn't produce lists of characters
if let Sequence::String(string) = &seq {
Expand Down Expand Up @@ -676,6 +676,23 @@ mod inner {

Ok(Value::list(out))
}

/// Split the input sequence into evenly sized chunks. If the input length of the sequence
/// is not dividable by the chunk_size the last chunk will contain fewer elements.
pub fn chunks(seq: &mut Sequence, chunk_size: usize) -> anyhow::Result<Value> {
if chunk_size == 0 {
return Err(anyhow!("chunk size must be non-zero"));
}

let iter = mut_seq_to_iterator(seq);

Ok(Value::list(
iter.chunks(chunk_size)
.into_iter()
.map(|chunk| Value::list(chunk.collect_vec()))
.collect_vec(),
))
}
}

fn fold_iterator(
Expand Down
5 changes: 5 additions & 0 deletions ndc_lib/src/stdlib/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ mod inner {
format!("{left}{right}")
}

/// Returns the provided value as a string
pub fn string(value: &Value) -> String {
format!("{value}")
}

/// Returns the Unicode code point of a 1-length string.
pub fn ord(string: &str) -> anyhow::Result<i64> {
if string.chars().count() == 1 {
Expand Down