From 0af5feb0aad579b1d06b6a2f83d566ea0a27c330 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Ko=C5=82aczkowski?= Date: Tue, 23 Jul 2024 16:21:19 +0200 Subject: [PATCH] Rename list to vector, add join, read_words, read_resource_words fns --- src/context.rs | 50 ++++++++++++++++++++++++++++++++++++++++++------- src/workload.rs | 9 ++++++++- 2 files changed, 51 insertions(+), 8 deletions(-) diff --git a/src/context.rs b/src/context.rs index f35c4d3..77fca57 100644 --- a/src/context.rs +++ b/src/context.rs @@ -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, VmError> { +pub fn vector(len: usize, generator: Function) -> Result, 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) @@ -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 { + 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 { let mut file = File::open(filename).expect("no such file"); @@ -1064,12 +1079,26 @@ pub fn read_to_string(filename: &str) -> io::Result { /// Reads a file into a vector of lines. pub fn read_lines(filename: &str) -> io::Result> { - 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> { + 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) } @@ -1089,3 +1118,10 @@ pub fn read_resource_lines(path: &str) -> io::Result> { .map(|s| s.to_string()) .collect_vec()) } + +pub fn read_resource_words(path: &str) -> io::Result> { + Ok(read_resource_to_string(path)? + .split(|c: char| !c.is_alphabetic()) + .map(|s| s.to_string()) + .collect_vec()) +} diff --git a/src/workload.rs b/src/workload.rs index 26ceeee..5b90965 100644 --- a/src/workload.rs +++ b/src/workload.rs @@ -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) @@ -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 @@ -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"], @@ -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();