Skip to content

Commit

Permalink
update docs for hashmaps
Browse files Browse the repository at this point in the history
  • Loading branch information
mattwparas committed Dec 22, 2023
1 parent 017647d commit 86f465f
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 66 deletions.
88 changes: 26 additions & 62 deletions crates/steel-core/src/primitives/hashmaps.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::stop;
use crate::{core::utils::declare_const_ref_functions, gc::Gc};
use crate::{
rvals::{Result, SteelVal},
steel_vm::builtin::BuiltInModule,
};
use crate::{steel_vm::builtin::DocTemplate, stop};
use im_rc::HashMap;

use crate::primitives::VectorOperations;
Expand All @@ -14,9 +14,6 @@ declare_const_ref_functions!(
HM_CONSTRUCT => hm_construct,
HM_INSERT => steel_hash_insert,
HM_GET => steel_hash_ref,
HM_VALUES_TO_LIST => values_to_list,
HM_KEYS_TO_VEC => keys_to_vector,
HM_VALUES_TO_VEC => values_to_vector,
HM_CLEAR => clear,
HM_EMPTY => hm_empty,
HM_UNION => hm_union,
Expand All @@ -34,13 +31,9 @@ pub(crate) fn hashmap_module() -> BuiltInModule {
.register_native_fn_definition(HASH_LENGTH_DEFINITION)
.register_native_fn_definition(HASH_CONTAINS_DEFINITION)
.register_native_fn_definition(KEYS_TO_LIST_DEFINITION)
.register_value("hash-keys->vector", HM_KEYS_TO_VEC)
.register_value_with_doc(
"hash-values->list",
HM_VALUES_TO_LIST,
HASH_VALUES_TO_LIST_DOC,
)
.register_value("hash-values->vector", HM_VALUES_TO_VEC)
.register_native_fn_definition(KEYS_TO_VECTOR_DEFINITION)
.register_native_fn_definition(VALUES_TO_LIST_DEFINITION)
.register_native_fn_definition(VALUES_TO_VECTOR_DEFINITION)
.register_value("hash-clear", HM_CLEAR)
.register_value("hash-empty?", HM_EMPTY)
.register_value("hash-union", HM_UNION);
Expand Down Expand Up @@ -247,59 +240,30 @@ pub fn keys_to_list(hashmap: &Gc<HashMap<SteelVal, SteelVal>>) -> Result<SteelVa
Ok(SteelVal::ListV(hashmap.keys().cloned().collect()))
}

const HASH_VALUES_TO_LIST_DOC: DocTemplate<'static> = DocTemplate {
signature: "(hash-values->list? map) -> (listof any/c)?",
params: &["map : hash?"],
description: r#"Returns the values of the given hash map as a list"#,
examples: &[(
"> (hash-values->list? (hash 'a 10 'b 20) 'a)",
r#"=> '(10 20)"#,
)],
};

// values as list
pub fn values_to_list(args: &[SteelVal]) -> Result<SteelVal> {
if args.len() != 1 {
stop!(ArityMismatch => "hm-values->list takes 1 argument")
}

let hashmap = &args[0];

if let SteelVal::HashMapV(hm) = hashmap {
// let keys = hm.values().cloned().collect::<Vec<SteelVal>>();
// ListOperations::built_in_list_func_flat(&keys)
Ok(SteelVal::ListV(hm.values().cloned().collect()))
} else {
stop!(TypeMismatch => "hm-values->list takes a hashmap")
}
/// Returns the values of the given hash map as a list
///
/// (hash-values->list? map) -> (listof any/c)?
///
/// map: hash?
///
/// # Examples
/// ```scheme
/// > (hash-values->list? (hash 'a 10 'b 20) 'a)",
/// r#"=> '(10 20)",
/// ```
#[steel_derive::function(name = "hash-values->list")]
pub fn values_to_list(hashmap: &Gc<HashMap<SteelVal, SteelVal>>) -> Result<SteelVal> {
Ok(SteelVal::ListV(hashmap.values().cloned().collect()))
}

pub fn keys_to_vector(args: &[SteelVal]) -> Result<SteelVal> {
if args.len() != 1 {
stop!(ArityMismatch => "hm-keys->vector takes 1 argument")
}

let hashmap = &args[0];

if let SteelVal::HashMapV(hm) = hashmap {
VectorOperations::vec_construct_iter_normal(hm.keys().cloned())
} else {
stop!(TypeMismatch => "hm-keys->vector takes a hashmap")
}
#[steel_derive::function(name = "hm-keys->vector")]
pub fn keys_to_vector(hashmap: &Gc<HashMap<SteelVal, SteelVal>>) -> Result<SteelVal> {
VectorOperations::vec_construct_iter_normal(hashmap.keys().cloned())
}

pub fn values_to_vector(args: &[SteelVal]) -> Result<SteelVal> {
if args.len() != 1 {
stop!(ArityMismatch => "hm-values->vector takes 1 argument")
}

let hashmap = &args[0];

if let SteelVal::HashMapV(hm) = hashmap {
VectorOperations::vec_construct_iter_normal(hm.values().cloned())
} else {
stop!(TypeMismatch => "hm-values->vector takes a hashmap")
}
#[steel_derive::function(name = "hm-values->vector")]
pub fn values_to_vector(hashmap: &Gc<HashMap<SteelVal, SteelVal>>) -> Result<SteelVal> {
VectorOperations::vec_construct_iter_normal(hashmap.values().cloned())
}

pub fn clear(args: &[SteelVal]) -> Result<SteelVal> {
Expand Down Expand Up @@ -524,7 +488,7 @@ mod hashmap_tests {
})
.into(),
)];
let res = keys_to_vector(&args);
let res = steel_keys_to_vector(&args);
let expected = im_rc::vector![
SteelVal::StringV("foo".into()),
SteelVal::StringV("bar".into()),
Expand Down Expand Up @@ -580,7 +544,7 @@ mod hashmap_tests {
})
.into(),
)];
let res = values_to_vector(&args);
let res = steel_values_to_vector(&args);
let expected = im_rc::vector![
SteelVal::StringV("bar".into()),
SteelVal::StringV("baz".into()),
Expand Down
16 changes: 14 additions & 2 deletions docs/src/builtins/steel_base.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,18 @@ Gets the `key` from the given `map`. Returns #false if the key does not exist.
> (hash-try-get (hash 'a 10 'b 20) 'b) ;; => 20
> (hash-try-get (hash 'a 10 'b 20) 'does-not-exist) ;; => #false
```
### **hash-values->list**
Returns the values of the given hash map as a list

(hash-values->list? map) -> (listof any/c)?

map: hash?

#### Examples
```scheme
> (hash-values->list? (hash 'a 10 'b 20) 'a)",
r#"=> '(10 20)",
```
### **input-port?**
Checks if a given value is an input port

Expand Down Expand Up @@ -659,9 +671,7 @@ of the string
### **hash-clear**
### **hash-empty?**
### **hash-get**
### **hash-keys->vector**
### **hash-union**
### **hash-values->vector**
### **hash?**
### **hashset**
### **hashset->list**
Expand All @@ -671,6 +681,8 @@ of the string
### **hashset-insert**
### **hashset-length**
### **hashset-subset?**
### **hm-keys->vector**
### **hm-values->vector**
### **inspect-bytecode**
### **instant/elapsed**
### **instant/now**
Expand Down
16 changes: 14 additions & 2 deletions docs/src/builtins/steel_hash.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,22 @@ Gets the `key` from the given `map`. Returns #false if the key does not exist.
> (hash-try-get (hash 'a 10 'b 20) 'b) ;; => 20
> (hash-try-get (hash 'a 10 'b 20) 'does-not-exist) ;; => #false
```
### **hash-values->list**
Returns the values of the given hash map as a list

(hash-values->list? map) -> (listof any/c)?

map: hash?

#### Examples
```scheme
> (hash-values->list? (hash 'a 10 'b 20) 'a)",
r#"=> '(10 20)",
```
### **%keyword-hash**
### **hash-clear**
### **hash-empty?**
### **hash-get**
### **hash-keys->vector**
### **hash-union**
### **hash-values->vector**
### **hm-keys->vector**
### **hm-values->vector**

0 comments on commit 86f465f

Please sign in to comment.