all repos — min @ 6dc986dd5e3d6d76c8fcbd95dd7f99a17c9f2cb0

A small but practical concatenative programming language.

Merge branch 'master' into next
h3rald h3rald@h3rald.com
Fri, 22 Jan 2021 20:55:18 +0000
commit

6dc986dd5e3d6d76c8fcbd95dd7f99a17c9f2cb0

parent

25a958f3c3158b53a3dd64da7a18e489d9454735

3 files changed, 101 insertions(+), 1 deletions(-)

jump to
M minpkg/lib/min_num.nimminpkg/lib/min_num.nim

@@ -135,4 +135,73 @@ i.push c.int.newVal

else: i.push c.newVal + def.symbol("product") do (i: In): + var s: MinValue + i.reqQuotationOfNumbers s + var c = 1.float + var isInt = true + for n in s.qVal: + if n.isFloat: + isInt = false + c = c * n.floatVal + else: + c = c * n.intVal.float + if isInt: + i.push c.int.newVal + else: + i.push c.newVal + + def.symbol("avg") do (i: In): + var s: MinValue + i.reqQuotationOfNumbers s + var c = 0.float + for n in s.qVal: + if n.isFloat: + c = + n.floatVal + else: + c = c + n.intVal.float + c = c / len(s.qVal).float + i.push c.newVal + + def.symbol("med") do (i: In): + var s: MinValue + i.reqQuotationOfNumbers s + let first = s.qVal[(s.qVal.len-1) div 2] + let second = s.qVal[((s.qVal.len-1) div 2)+1] + if s.qVal.len mod 2 == 1: + i.push first + else: + if first.isFloat: + if second.isFloat: + i.push ((first.floatVal+second.floatVal)/2).newVal + else: + i.push ((first.floatVal+second.intVal.float)/2).newVal + else: + if second.isFloat: + i.push ((first.intVal.float+second.floatVal)/2).newVal + else: + i.push ((first.intVal+second.intVal).float/2).newVal + + def.symbol("range") do (i: In): + var s: MinValue + i.reqQuotationOfNumbers s + var a = s.qVal[0] + var b = s.qVal[1] + var step = 1.newVal + var res = newSeq[MinValue](0) + if len(s.qVal)==3: + a = s.qVal[0] + b = s.qVal[1] + step = s.qVal[2] + var j = a + if a.intVal < b.intVal: + while j.intVal <= b.intVal: + res.add j + j = (j.intVal + step.intVal).newVal + else: + while j.intVal >= b.intVal: + res.add j + j = (j.intVal - step.intVal).newVal + i.push res.newVal + def.finalize("num")
M site/contents/reference-num.mdsite/contents/reference-num.md

@@ -55,4 +55,16 @@ {#op||succ||{{i1}}||{{i2}}||

Returns the successor of {{i1}}.#} {#op||sum||{{q}}||{{i}}|| -Returns the sum of all items of {{q}}. {{q}} is a quotation of integers. #}+Returns the sum of all items of {{q}}. {{q}} is a quotation of integers. #} + +{#op||product||{{q}}||{{i}}|| +Returns the product of all items of {{q}}. {{q}} is a quotation of integers. #} + +{#op||avg||{{q}}||{{n}}|| +Returns the average of the items of {{q}}. #} + +{#op||med||{{q}}||{{n}}|| +Returns the median of the items of {{q}}. #} + +{#op||range||{{q}}||{{q}}|| +Takes a quotation of integers in the form of {{start}}, {{end}} (,{{step}}), generates the sequence and returns the resulting quotation of integers. #}
M tests/num.mintests/num.min

@@ -32,6 +32,25 @@ (1000 random 1000 <) assert

((1 2 3 4 5) sum 15 ==) assert + ((1 2 3 4 5) product 120 ==) assert + + ((1 2 3 4 5) avg 3.0 ==) assert + ((1 2 3 4 5 6) avg 3.5 ==) assert + + ((1 3 5 7) med 4.0 ==) assert + ((1 3 5 7 9) med 5 ==) assert + + ((1 5) range (1 2 3 4 5) ==) assert + ((5 1) range (5 4 3 2 1) ==) assert + ((4 7) range (4 5 6 7) ==) assert + ((7 4) range (7 6 5 4) ==) assert + ((1 6 2) range (1 3 5) ==) assert + ((1 6 3) range (1 4) ==) assert + ((0 6 2) range (0 2 4 6) ==) assert + ((6 1 2) range (6 4 2) ==) assert + ((6 1 3) range (6 3) ==) assert + ((6 0 2) range (6 4 2 0) ==) assert + (0 :c (c 10 <) (c succ @c) while c 10 ==) assert