From 86f465ff0f6131ab44e155a081d556368b975ac2 Mon Sep 17 00:00:00 2001 From: mattwparas Date: Fri, 22 Dec 2023 15:35:21 -0800 Subject: [PATCH] update docs for hashmaps --- crates/steel-core/src/primitives/hashmaps.rs | 88 ++++++-------------- docs/src/builtins/steel_base.md | 16 +++- docs/src/builtins/steel_hash.md | 16 +++- 3 files changed, 54 insertions(+), 66 deletions(-) diff --git a/crates/steel-core/src/primitives/hashmaps.rs b/crates/steel-core/src/primitives/hashmaps.rs index 0e60fa2f6..23af28eb5 100644 --- a/crates/steel-core/src/primitives/hashmaps.rs +++ b/crates/steel-core/src/primitives/hashmaps.rs @@ -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; @@ -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, @@ -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); @@ -247,59 +240,30 @@ pub fn keys_to_list(hashmap: &Gc>) -> Result = 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 { - 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::>(); - // 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>) -> Result { + Ok(SteelVal::ListV(hashmap.values().cloned().collect())) } -pub fn keys_to_vector(args: &[SteelVal]) -> Result { - 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>) -> Result { + VectorOperations::vec_construct_iter_normal(hashmap.keys().cloned()) } -pub fn values_to_vector(args: &[SteelVal]) -> Result { - 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>) -> Result { + VectorOperations::vec_construct_iter_normal(hashmap.values().cloned()) } pub fn clear(args: &[SteelVal]) -> Result { @@ -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()), @@ -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()), diff --git a/docs/src/builtins/steel_base.md b/docs/src/builtins/steel_base.md index 32c57eebd..527eef7e7 100644 --- a/docs/src/builtins/steel_base.md +++ b/docs/src/builtins/steel_base.md @@ -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 @@ -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** @@ -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** diff --git a/docs/src/builtins/steel_hash.md b/docs/src/builtins/steel_hash.md index 9c90de365..7139a0148 100644 --- a/docs/src/builtins/steel_hash.md +++ b/docs/src/builtins/steel_hash.md @@ -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**