Skip to content

Commit

Permalink
Rename list to vector, add join, read_words, read_resource_words fns
Browse files Browse the repository at this point in the history
  • Loading branch information
pkolaczk committed Jul 23, 2024
1 parent dab01a9 commit 0af5feb
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 8 deletions.
50 changes: 43 additions & 7 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1012,10 +1012,10 @@ pub fn clamp_int(value: i64, min: i64, max: i64) -> i64 {
value.clamp(min, max)
}

pub fn list(seed: i64, len: usize, generator: Function) -> Result<Vec<Value>, VmError> {
pub fn vector(len: usize, generator: Function) -> Result<Vec<Value>, VmError> {
let mut result = Vec::with_capacity(len);
for i in 0..len {
let value = generator.call((hash2(seed, i as i64),))?;
let value = generator.call((i as i64,))?;
result.push(value);
}
Ok(result)
Expand Down Expand Up @@ -1052,6 +1052,21 @@ pub fn hash_select(i: i64, collection: &[Value]) -> &Value {
&collection[hash_range(i, collection.len() as i64) as usize]
}

/// Joins all strings in vector with given separator
pub fn join(collection: &[Value], separator: &str) -> Result<String, VmError> {
let mut result = String::new();
let mut first = true;
for v in collection {
let v = v.clone().into_string()?;
if !first {
result.push_str(separator);
}
result.push_str(v.borrow_ref()?.as_str());
first = false;
}
Ok(result)
}

/// Reads a file into a string.
pub fn read_to_string(filename: &str) -> io::Result<String> {
let mut file = File::open(filename).expect("no such file");
Expand All @@ -1064,12 +1079,26 @@ pub fn read_to_string(filename: &str) -> io::Result<String> {

/// Reads a file into a vector of lines.
pub fn read_lines(filename: &str) -> io::Result<Vec<String>> {
let file = File::open(filename).expect("no such file");
let file = File::open(filename)
.map_err(|e| io::Error::new(e.kind(), format!("Failed to open file {filename}: {e}")))?;
let buf = BufReader::new(file);
let result = buf
.lines()
.map(|l| l.expect("Could not parse line"))
.collect();
buf.lines().try_collect()
}

/// Reads a file into a vector of words.
pub fn read_words(filename: &str) -> io::Result<Vec<String>> {
let file = File::open(filename)
.map_err(|e| io::Error::new(e.kind(), format!("Failed to open file {filename}: {e}")))?;
let buf = BufReader::new(file);
let mut result = Vec::new();
for line in buf.lines() {
let line = line?;
let words = line
.split(|c: char| !c.is_alphabetic())
.map(|s| s.to_string())
.filter(|s| !s.is_empty());
result.extend(words);
}
Ok(result)
}

Expand All @@ -1089,3 +1118,10 @@ pub fn read_resource_lines(path: &str) -> io::Result<Vec<String>> {
.map(|s| s.to_string())
.collect_vec())
}

pub fn read_resource_words(path: &str) -> io::Result<Vec<String>> {
Ok(read_resource_to_string(path)?
.split(|c: char| !c.is_alphabetic())
.map(|s| s.to_string())
.collect_vec())
}
9 changes: 8 additions & 1 deletion src/workload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ impl Program {
let mut latte_module = Module::with_crate("latte");
latte_module.function(&["blob"], context::blob).unwrap();
latte_module.function(&["text"], context::text).unwrap();
latte_module.function(&["list"], context::list).unwrap();
latte_module.function(&["vector"], context::vector).unwrap();

latte_module
.function(&["now_timestamp"], context::now_timestamp)
Expand Down Expand Up @@ -177,6 +177,7 @@ impl Program {

latte_module.inst_fn("clamp", context::clamp_float).unwrap();
latte_module.inst_fn("clamp", context::clamp_int).unwrap();
latte_module.inst_fn("join", context::join).unwrap();

let mut fs_module = Module::with_crate("fs");
fs_module
Expand All @@ -185,6 +186,9 @@ impl Program {
fs_module
.function(&["read_lines"], context::read_lines)
.unwrap();
fs_module
.function(&["read_words"], context::read_words)
.unwrap();
fs_module
.function(
&["read_resource_to_string"],
Expand All @@ -194,6 +198,9 @@ impl Program {
fs_module
.function(&["read_resource_lines"], context::read_resource_lines)
.unwrap();
fs_module
.function(&["read_resource_words"], context::read_resource_words)
.unwrap();

let mut context = rune::Context::with_default_modules().unwrap();
context.install(&context_module).unwrap();
Expand Down

0 comments on commit 0af5feb

Please sign in to comment.