ssub function

This commit is contained in:
John Kerl 2020-09-22 21:21:18 -04:00
parent dd9f065243
commit acf4dbda45
6 changed files with 63 additions and 0 deletions

View file

@ -341,6 +341,10 @@ var BUILTIN_FUNCTION_LOOKUP_TABLE = []FunctionInfo{
name: "?:",
ternaryFunc: TernaryShortCircuitPlaceholder,
},
{
name: "ssub",
ternaryFunc: types.MlrvalSsub,
},
// Variadic built-in functions
{

View file

@ -14,6 +14,10 @@ func GetTypeName(mvtype MVType) string {
}
// ----------------------------------------------------------------
func (this *Mlrval) IsErrorOrAbsent() bool {
return this.mvtype == MT_ERROR || this.mvtype == MT_ABSENT
}
func (this *Mlrval) IsError() bool {
return this.mvtype == MT_ERROR
}
@ -34,6 +38,10 @@ func (this *Mlrval) IsString() bool {
return this.mvtype == MT_STRING
}
func (this *Mlrval) IsStringOrVoid() bool {
return this.mvtype == MT_STRING || this.mvtype == MT_VOID
}
func (this *Mlrval) IsInt() bool {
return this.mvtype == MT_INT
}

View file

@ -2,6 +2,7 @@ package types
import (
"math"
"strings"
"time"
"miller/lib"
@ -1704,3 +1705,28 @@ func NumericAscendingComparator(ma *Mlrval, mb *Mlrval) int {
func NumericDescendingComparator(ma *Mlrval, mb *Mlrval) int {
return NumericAscendingComparator(mb, ma)
}
// ================================================================
func MlrvalSsub(ma, mb, mc *Mlrval) Mlrval {
if ma.IsErrorOrAbsent() {
return *ma
}
if mb.IsErrorOrAbsent() {
return *mb
}
if mb.IsErrorOrAbsent() {
return *mc
}
if !ma.IsStringOrVoid() {
return MlrvalFromError()
}
if !mb.IsStringOrVoid() {
return MlrvalFromError()
}
if !mc.IsStringOrVoid() {
return MlrvalFromError()
}
return MlrvalFromString(
strings.Replace(ma.printrep, mb.printrep, mc.printrep, 1),
)
}

View file

@ -230,3 +230,5 @@ echo; run_mlr --from u/s.dkvp put 'if (NR == 1) { $z = 100 } else { $z = 900 }'
echo; run_mlr --from u/s.dkvp put 'if (NR == 1) { $z = 100 } elif (NR == 2) { $z = 200 }'
echo; run_mlr --from u/s.dkvp put 'if (NR == 1) { $z = 100 } elif (NR == 2) { $z = 200 } else { $z = 900 }'
echo; run_mlr --from u/s.dkvp put 'if (NR == 1) { $z = 100 } elif (NR == 2) { $z = 200 } elif (NR == 3) { $z = 300 } else { $z = 900 }'
echo x=eeee | run_mlr put '$y=ssub($x, "e", "X")'

View file

@ -2416,3 +2416,7 @@ a=pan,b=pan,i=1,x=0.3467901443380824,y=0.7268028627434533,z=100
a=eks,b=pan,i=2,x=0.7586799647899636,y=0.5221511083334797,z=200
a=wye,b=wye,i=3,x=0.20460330576630303,y=0.33831852551664776,z=300
a=eks,b=wye,i=4,x=0.38139939387114097,y=0.13418874328430463,z=900
----------------------------------------------------------------
mlr put $y=ssub($x, "e", "X")
x=eeee,y=Xeee

View file

@ -352,3 +352,22 @@ mlr --json --from u/needs-sorting.json sort-within-records
"b": 2,
"c": 3
}
----------------------------------------------------------------
mlr --json --from u/needs-regularize.json regularize
{
"b": 2,
"c": 3,
"a": 1
}
{
"b": 2,
"c": 3,
"a": 1
}
{
"d": 4,
"a": 1,
"b": 2,
"c": 3
}