all repos — min @ 7d7a3e1fd8cd94f92d8e3b412929374c1422d9b9

A small but practical concatenative programming language.

Added dsort operator to sort dictionaries by key.
h3rald h3rald@h3rald.com
Sun, 06 Aug 2017 13:04:37 +0200
commit

7d7a3e1fd8cd94f92d8e3b412929374c1422d9b9

parent

cac4ce181c8497945ce8d792e8474ea0eddb1393

M Min_DeveloperGuide.htmMin_DeveloperGuide.htm

@@ -6111,12 +6111,40 @@

<pre><code>(("a1" true) ("a2" "aaa") ("a3" false)) 'a2 dhas? </code></pre></div></div> +<p><a id="op-dkeys"></a> +<span class="reference-title">dkeys</span></p> + +<div class="operator"><p><span class="kwd"> <span class="kwd">dict</span> <strong>&rArr;</strong> (<span class="kwd">string</span><span class="kwd"><sub>*</sub></span>)</span></p> + +<p>Returns a quotation containing all the keys of dictionary <span class="kwd">dict</span>.</p></div> + <p><a id="op-dset"></a> <span class="reference-title">dset</span></p> <div class="operator"><p><span class="kwd"> <span class="kwd">dict<sub>1</sub></span> <span class="kwd">a</span> <span class="kwd">&apos;sym</span> <strong>&rArr;</strong> <span class="kwd">dict<sub>2</sub></span></span></p> <p>Sets the value of the <span class="kwd">&apos;sym</span> of <span class="kwd">dict<sub>1</sub></span> to <span class="kwd">a</span>, and returns the modified copy of the dictionary <span class="kwd">dict<sub>2</sub></span>.</p></div> + +<p><a id="op-dsort"></a> +<span class="reference-title">dsort</span></p> + +<div class="operator"><p><span class="kwd"> <span class="kwd">dict<sub>1</sub></span> <strong>&rArr;</strong> <span class="kwd">dict<sub>2</sub></span></span></p> + +<p>Returns a new dictionary <span class="kwd">dict<sub>2</sub></span> containing all elements of <span class="kwd">dict<sub>1</sub></span> sorted by key in ascending order.</p> + +<div class="sidebar"><p>Example</p> + +<p>The following program leaves <code>(("a" 342) ("b" true) ("c" -4) ("d" 22))</code> on the stack:</p> + +<pre><code>(("b" true) ("d" 22) ("c" -4) ("a" 342)) dsort +</code></pre></div></div> + +<p><a id="op-dvalues"></a> +<span class="reference-title">dvalues</span></p> + +<div class="operator"><p><span class="kwd"> <span class="kwd">dict</span> <strong>&rArr;</strong> (<span class="kwd">a<sub>*</sub></span>)</span></p> + +<p>Returns a quotation containing all the values of dictionary <span class="kwd">dict</span>.</p></div> <p><a id="op-filter"></a> <span class="reference-title">filter</span></p>

@@ -6198,13 +6226,6 @@

<div class="operator"><p><span class="kwd"> <span class="kwd">quot<sub>1</sub></span> <span class="kwd">a</span> <span class="kwd">int</span> <strong>&rArr;</strong> <span class="kwd">quot<sub>2</sub></span></span></p> <p>Inserts <span class="kwd">a</span> as the value of the <em>n<sup>th</sup></em> element <span class="kwd">quot<sub>1</sub></span> (zero-based), and returns the modified copy of the quotation <span class="kwd">quot<sub>2</sub></span>.</p></div> - -<p><a id="op-keys"></a> -<span class="reference-title">keys</span></p> - -<div class="operator"><p><span class="kwd"> <span class="kwd">quot</span> <strong>&rArr;</strong> (<span class="kwd">string</span><span class="kwd"><sub>*</sub></span>)</span></p> - -<p>Returns a quotation containing all the keys of dictionary <span class="kwd">dict</span>.</p></div> <p><a id="op-map"></a> <span class="reference-title">map</span></p>

@@ -6342,13 +6363,6 @@ <p>The following program leaves <code>(1 3 5 7 9 13 16)</code> on the stack:</p>

<pre><code>(1 9 5 13 16 3 7) '&gt; sort </code></pre></div></div> - -<p><a id="op-values"></a> -<span class="reference-title">values</span></p> - -<div class="operator"><p><span class="kwd"> <span class="kwd">dict</span> <strong>&rArr;</strong> (<span class="kwd">a<sub>*</sub></span>)</span></p> - -<p>Returns a quotation containing all the values of dictionary <span class="kwd">dict</span>.</p></div> <a name="<code>io</code>-Module"></a> <h3><code>io</code> Module<a href="#document-top" title="Go to top"></a></h3>
M lib/min_seq.nimlib/min_seq.nim

@@ -334,15 +334,20 @@ let k = vals[0]

let d = vals[1] i.push i.ddel(d, k) - def.symbol("keys") do (i: In): + def.symbol("dkeys") do (i: In): let vals = i.expect("dict") let d = vals[0] i.push i.keys(d) - def.symbol("values") do (i: In): + def.symbol("dvalues") do (i: In): let vals = i.expect("dict") let d = vals[0] i.push i.values(d) + + def.symbol("dsort") do (i: In): + let vals = i.expect("dict") + var d = vals[0] + i.push d.qVal.sortedByIt(it.qVal[0].getString).newVal(i.scope) def.sigil("?") do (i: In): i.push("dhas?".newSym)
M min.vimmin.vim

@@ -11,7 +11,7 @@

setl iskeyword=@,36-39,+,-,/,*,.,:,~,!,48-57,60-65,94-95,192-255 setl iskeyword+=^ -syntax keyword minDefaultSymbol ! != $ & ' * + # - % ^ -> . .. / : < <= == => =~ > >= @ ROOT aes all? and any? append apply args ask atime bind bool boolean? call call! capitalize case cd chmod choose clear-stack cleave column-print concat confirm cons cp cpu crypto ctime datetime ddel debug decode define defined? delete dget dhas? dictionary? dip dir? dirname div dprint dprint! dset dup encode env? error eval even? exists? exit expect fappend fatal find file? filename filter first flatten float float? foreach fperms fread from-json format-error fs fsize fstats ftype fwrite get gets get-env get-stack hardlink harvest hidden? id if import in? indent indexof info insert int integer? interpolate interval io join keep keys length linrec load load-symbol logic loglevel loglevel? lowercase ls ls-r map map-reduce match md5 mkdir mod module module-symbols module-sigils mtime mv newline nip not notice now num number? odd? opts os over partition password pick pop popd pred prepend print print! prompt publish puts puts! put-env q quotation? quote quote-bind quote-define random raise reduce regex reject remove remove-symbol repeat replace rest reverse rm rmdir run save-symbol scope scope? seal search seq set set-stack sha1 sha224 sha256 sha384 sha512 shorten sigils sip size sleep slice sort source split spread stack startup stored-symbols str string string? strip succ sum swap swons symbols symlink symlink? sys system take tap tap! tformat time timeinfo times timestamp titleize to-json to-timestamp try dequote uppercase unzip values version warn when which while with xor zip +syntax keyword minDefaultSymbol ! != $ & ' * + # - % ^ -> . .. / : < <= == => =~ > >= @ ROOT aes all? and any? append apply args ask atime bind bool boolean? call call! capitalize case cd chmod choose clear-stack cleave column-print concat confirm cons cp cpu crypto ctime datetime ddel debug decode define defined? delete dget dhas? dkeys dictionary? dip dir? dirname div dprint dprint! dset dsort dup dvalues encode env? error eval even? exists? exit expect fappend fatal find file? filename filter first flatten float float? foreach fperms fread from-json format-error fs fsize fstats ftype fwrite get gets get-env get-stack hardlink harvest hidden? id if import in? indent indexof info insert int integer? interpolate interval io join keep length linrec load load-symbol logic loglevel loglevel? lowercase ls ls-r map map-reduce match md5 mkdir mod module module-symbols module-sigils mtime mv newline nip not notice now num number? odd? opts os over partition password pick pop popd pred prepend print print! prompt publish puts puts! put-env q quotation? quote quote-bind quote-define random raise reduce regex reject remove remove-symbol repeat replace rest reverse rm rmdir run save-symbol scope scope? seal search seq set set-stack sha1 sha224 sha256 sha384 sha512 shorten sigils sip size sleep slice sort source split spread stack startup stored-symbols str string string? strip succ sum swap swons symbols symlink symlink? sys system take tap tap! tformat time timeinfo times timestamp titleize to-json to-timestamp try dequote uppercase unzip version warn when which while with xor zip syntax match minDefaultSigil ;\<[:@'~!?$%&$=<>#^*#+/]; contained
M site/contents/_includes/_reference-seq.mdsite/contents/_includes/_reference-seq.md

@@ -38,8 +38,24 @@ > >

> > (("a1" true) ("a2" "aaa") ("a3" false)) 'a2 dhas? #} +{#op||dkeys||{{d}}||({{s}}{{0p}})|| +Returns a quotation containing all the keys of dictionary {{d}}. #} + {#op||dset||{{d1}} {{any}} {{sl}}||{{d2}}|| Sets the value of the {{sl}} of {{d1}} to {{any}}, and returns the modified copy of the dictionary {{d2}}. #} + +{#op||dsort||{{d1}}||{{d2}}|| +> Returns a new dictionary {{d2}} containing all elements of {{d1}} sorted by key in ascending order. +> +> > %sidebar% +> > Example +> > +> > The following program leaves `(("a" 342) ("b" true) ("c" -4) ("d" 22))` on the stack: +> > +> > (("b" true) ("d" 22) ("c" -4) ("a" 342)) dsort#} + +{#op||dvalues||{{d}}||({{a0p}})|| +Returns a quotation containing all the values of dictionary {{d}}. #} {#op||filter||{{q1}} {{q2}}||{{q3}}|| > Returns a new quotation {{q3}} containing all elements of {{q1}} that satisfy predicate {{q2}}.

@@ -93,9 +109,6 @@ Returns {{t}} if {{any}} is contained in {{q}}, {{f}} otherwise.#}

{#op||insert||{{q1}} {{any}} {{i}}||{{q2}}|| Inserts {{any}} as the value of the _n^th_ element {{q1}} (zero-based), and returns the modified copy of the quotation {{q2}}. #} - -{#op||keys||{{q}}||({{s}}{{0p}})|| -Returns a quotation containing all the keys of dictionary {{d}}. #} {#op||map||{{q1}} {{q2}}||{{q3}}|| Returns a new quotation {{q3}} obtained by applying {{q2}} to each element of {{q1}}.#}

@@ -177,6 +190,3 @@ > >

> > The following program leaves `(1 3 5 7 9 13 16)` on the stack: > > > > (1 9 5 13 16 3 7) '> sort #} - -{#op||values||{{d}}||({{a0p}})|| -Returns a quotation containing all the values of dictionary {{d}}. #}
M tests/seq.mintests/seq.min

@@ -39,9 +39,11 @@ ((("a" 1)("b" 2)("c" 3)) ' :dict dict 5 'b dset 7 %d =newdict newdict (("a" 1)("b" 5)("c" 3)("d" 7)) == dict (("a" 1)("b" 2)("c" 3)) == and) assert

((("a" 1)("b" 2)("c" 3)) ' :dict dict 'b ddel =newdict newdict (("a" 1)("c" 3)) == dict (("a" 1)("b" 2)("c" 3)) == and) assert - ((("a" 1)("b" 2)("c" 3)) keys ("a" "b" "c") ==) assert + ((("a" 1)("b" 2)("c" 3)) dkeys ("a" "b" "c") ==) assert - ((("a" 1)("b" 2)("c" 3)) values (1 2 3) ==) assert + ((("a" 1)("b" 2)("c" 3)) dvalues (1 2 3) ==) assert + + ((("a" 4) ("c" 8) ("f" 2) ("b" 0)) dsort (("a" 4)("b" 0)("c" 8)("f" 2)) ==) assert ((3 4 7 2 4 6 5 6) '> sort (2 3 4 4 5 6 6 7) ==) assert