all repos — min @ 2c158ab453f39f67b22ba5d21201a3118b0a7f31

A small but practical concatenative programming language.

Merge pull request #2 from h3rald/master

..
Yanis Zafirópulos 1265028+drkameleon@users.noreply.github.com
Fri, 22 Jan 2021 09:02:55 +0100
commit

2c158ab453f39f67b22ba5d21201a3118b0a7f31

parent

8b2f844b9c02ee443c6dd1c196c853218ab3642f

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

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

@@ -135,4 +135,52 @@ 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.finalize("num")
M site/contents/reference-num.mdsite/contents/reference-num.md

@@ -55,4 +55,13 @@ {#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}}. #}
M tests/num.mintests/num.min

@@ -32,6 +32,14 @@ (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 + (0 :c (c 10 <) (c succ @c) while c 10 ==) assert