From 5a9b6525a2c984aebd5121424539de341e4e27a3 Mon Sep 17 00:00:00 2001 From: Celestino Amoroso Date: Fri, 10 May 2024 09:17:51 +0200 Subject: [PATCH] new function isRational() that return true is the passed value is integere or fraction --- func-base.go | 7 ++++++- utils.go | 7 +++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/func-base.go b/func-base.go index 755c47c..56e9be7 100644 --- a/func-base.go +++ b/func-base.go @@ -43,6 +43,11 @@ func isFractionFunc(ctx ExprContext, name string, args []any) (result any, err e return } +func isRationalFunc(ctx ExprContext, name string, args []any) (result any, err error) { + result = IsRational(args[0]) + return +} + func isListFunc(ctx ExprContext, name string, args []any) (result any, err error) { result = IsList(args[0]) return @@ -53,7 +58,6 @@ func isDictionaryFunc(ctx ExprContext, name string, args []any) (result any, err return } - func intFunc(ctx ExprContext, name string, args []any) (result any, err error) { if len(args) == 1 { switch v := args[0].(type) { @@ -93,6 +97,7 @@ func ImportBuiltinsFuncs(ctx ExprContext) { ctx.RegisterFunc("isString", &simpleFunctor{f: isStringFunc}, 1, 1) ctx.RegisterFunc("isFraction", &simpleFunctor{f: isFractionFunc}, 1, 1) ctx.RegisterFunc("isFract", &simpleFunctor{f: isFractionFunc}, 1, 1) + ctx.RegisterFunc("isRational", &simpleFunctor{f: isRationalFunc}, 1, 1) ctx.RegisterFunc("isList", &simpleFunctor{f: isListFunc}, 1, 1) ctx.RegisterFunc("isDictionary", &simpleFunctor{f: isDictionaryFunc}, 1, 1) ctx.RegisterFunc("isDict", &simpleFunctor{f: isDictionaryFunc}, 1, 1) diff --git a/utils.go b/utils.go index ba1ec81..837c1cf 100644 --- a/utils.go +++ b/utils.go @@ -44,6 +44,13 @@ func IsFract(v any) (ok bool) { return ok } +func IsRational(v any) (ok bool) { + if _, ok = v.(*fraction); !ok { + _, ok = v.(int64) + } + return ok +} + func IsNumber(v any) (ok bool) { return IsFloat(v) || IsInteger(v) }