Skip to content

Commit 815885a

Browse files
authored
Merge pull request #1066 from vsbogd/sort-docs
Sort docs
2 parents 41894e5 + 4c827c0 commit 815885a

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
lines changed

lib/src/metta/runner/stdlib/stdlib.metta

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,6 +1264,12 @@ Possible pragmas:
12641264
(@param "Atoms to be placed inside expression instead of {}")))
12651265
(@return "Expression with replaced {} with atoms"))
12661266

1267+
(@doc sort-strings
1268+
(@desc "Sorts expression which contains strings in alphabet order")
1269+
(@params (
1270+
(@param "List of strings")))
1271+
(@return "Sorted list of strings"))
1272+
12671273
(@doc sealed
12681274
(@desc "Replaces all occurrences of any var inside atom (second argument) by unique variable, except list of variables to ignore (first argument). Can be used to create a locally scoped variables")
12691275
(@params (

lib/src/metta/runner/stdlib/string.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use hyperon_atom::*;
2+
use hyperon_atom::gnd::GroundedFunctionAtom;
23
use crate::metta::*;
34
use crate::metta::text::Tokenizer;
45
use hyperon_atom::gnd::str::*;
@@ -60,13 +61,32 @@ impl CustomExecute for FormatArgsOp {
6061
}
6162
}
6263

64+
fn sort_strings(args: &[Atom]) -> Result<Vec<Atom>, ExecError> {
65+
let arg_error = "sort-strings expects expression with strings as a first argument";
66+
let list = TryInto::<&ExpressionAtom>::try_into(args.get(0).ok_or(arg_error)?).map_err(|_| arg_error)?;
67+
let mut strings = Vec::<&str>::with_capacity(list.children().len());
68+
for s in list.children() {
69+
let s = Atom::as_gnd::<Str>(s).ok_or(arg_error)?;
70+
strings.push(s.as_str());
71+
}
72+
strings.sort();
73+
let sorted: Vec::<Atom> = strings.into_iter()
74+
.map(|s| Atom::gnd(Str::from_string(s.into()))).collect();
75+
Ok(vec![Atom::expr(sorted)])
76+
}
77+
6378
pub(super) fn register_context_independent_tokens(tref: &mut Tokenizer) {
6479
let println_op = Atom::gnd(PrintlnOp{});
6580
tref.register_token(regex(r"println!"), move |_| { println_op.clone() });
6681
let format_args_op = Atom::gnd(FormatArgsOp{});
6782
tref.register_token(regex(r"format-args"), move |_| { format_args_op.clone() });
6883
tref.register_token(regex(r#"(?s)^".*"$"#),
6984
|token| { let mut s = String::from(token); s.remove(0); s.pop(); Atom::gnd(Str::from_string(s)) });
85+
tref.register_function(GroundedFunctionAtom::new(
86+
r"sort-strings".into(),
87+
expr!("->" "Expression" "Expression"),
88+
sort_strings
89+
));
7090
}
7191

7292
#[cfg(test)]

lib/tests/test_stdlib.metta

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,6 @@
131131
(issue911 A B)
132132
(= (isa911 $x $y) (match &self (issue911 $x $y) True))
133133
!(assertEqual ((isa911 $a B) $a) (True A))
134+
135+
; Test sort-strings function
136+
!(assertEqual (sort-strings ("c" "b" "a" "c")) ("a" "b" "c" "c"))

mkdocs.metta

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,12 @@
4545
(= (document-module $title $mod)
4646
(let $_ (import! &self $mod)
4747
(let $helps (collapse (help-md-space! (module-space-no-deps (mod-space! $mod))))
48-
(let $text (foldl-atom $helps "" $r $i (format-args "{}{}" ($r $i)))
48+
(let $sorted (sort-strings $helps)
49+
(let $text (foldl-atom $sorted "" $r $i (format-args "{}{}" ($r $i)))
4950
(let $md (format-args "# {}\n{}" ($title $text))
5051
(let $path (format-args "./docs/{}.md" ($mod))
5152
(let $file (file-open! $path "wct")
52-
(file-write! $file $md) )))))))
53+
(file-write! $file $md) ))))))))
5354

5455
!(document-module "Standard library" corelib)
5556
!(document-module "File Input/Output" fileio)

0 commit comments

Comments
 (0)