Skip to content

Commit

Permalink
allow default values for get with fill
Browse files Browse the repository at this point in the history
  • Loading branch information
kaikalii committed Jan 11, 2024
1 parent 99ac39f commit 3de1aa6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/algorithm/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,15 @@ impl Value {
}
/// Get a value from a map array
pub fn get(&self, key: &Value, env: &Uiua) -> UiuaResult<Value> {
with_pair(self, env, |pair| pair.get(key))?.ok_or_else(|| env.error("Key not found in map"))
let value = with_pair(self, env, |pair| pair.get(key))?;
if let Some(value) = value {
Ok(value)
} else {
match env.box_fill() {
Ok(fill) => Ok(fill.0),
Err(e) => Err(env.error(format!("Key not found in map{e}"))),
}
}
}
/// Check if a map array contains a key
pub fn has_key(&self, key: &Value, env: &Uiua) -> UiuaResult<bool> {
Expand Down
5 changes: 5 additions & 0 deletions src/primitive/defs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1880,6 +1880,11 @@ primitive!(
/// ex: # Experimental!
/// : map 1_2 3_4
/// : (⋅⋅0|get) has,, 5 .
/// You can provide a default value with [fill].
/// ex: # Experimental!
/// : map 1_2 3_4
/// : ⬚0get 1 .
/// : ⬚0get 5 :
(2, Get, Map, "get"),
/// Remove the value corresponding to a key from a map array
///
Expand Down

0 comments on commit 3de1aa6

Please sign in to comment.