Skip to content

Commit e1b1ba0

Browse files
committed
changed custom functions
1 parent 443bc04 commit e1b1ba0

File tree

2 files changed

+30
-12
lines changed

2 files changed

+30
-12
lines changed

README.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,19 +126,17 @@ import com.github.murzagalin.evaluator.Function
126126

127127
object NormalDist : Function("normal_dist", 3) {
128128
override fun invoke(vararg args: Any): Any {
129-
val x = args[0]
130-
val m = args[1]
131-
val sigma = args[2]
132-
require(x is Double) { "$name argument must be a number" }
133-
require(m is Double) { "$name mean must be a number" }
134-
require(sigma is Double) { "$name sigma must be a number" }
129+
val x = args.getAsDouble(0) { "$name argument must be a number" }
130+
val m = args.getAsDouble(1) { "$name mean must be a number" }
131+
val sigma = args.getAsDouble(2) { "$name sigma must be a number" }
135132

136133
return 1.0 / sigma / sqrt(2 * PI) * exp(-1.0 / 2.0 * ((x - m) / sigma).pow(2))
137134
}
138135
}
139136
```
140137

141138
*Note:* the library checks if the number of arguments in an expression is equal to 3, otherwise it throws an exception. But you have to check the types of the arguments by yourself.
139+
Functions `getAsDouble(index, lazyMessage)` and `getAsBoolean(index, lazyMessage)` return an element at position `index`, and throw `IllegalArgumentException` with the message returned from `lazyMessage` if it has a wrong type
142140

143141
Then we add this function to the evaluator:
144142

src/commonMain/kotlin/com/github/murzagalin/evaluator/Function.kt

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,27 @@ abstract class Function(val name: String, val argsCount: IntRange) {
1111
abstract operator fun invoke(vararg args: Any): Any
1212
}
1313

14+
private fun <T> Array<T>.getAsNumber(index: Int, lazyMessage: () -> Any): Number {
15+
val e = get(index)
16+
require(e is Number, lazyMessage)
17+
18+
return e
19+
}
20+
21+
private fun <T> Array<T>.getAsDouble(index: Int, lazyMessage: () -> Any): Double {
22+
val e = get(index)
23+
require(e is Number, lazyMessage)
24+
25+
return e.toDouble()
26+
}
27+
28+
private fun <T> Array<T>.getAsBoolean(index: Int, lazyMessage: () -> Any): Boolean {
29+
val e = get(index)
30+
require(e is Boolean, lazyMessage)
31+
32+
return e
33+
}
34+
1435
abstract class OneNumberArgumentFunction(name: String, argsCount: IntRange) : Function(name, argsCount) {
1536

1637
constructor(name: String, argsCount: Int): this(name, argsCount..argsCount)
@@ -19,8 +40,9 @@ abstract class OneNumberArgumentFunction(name: String, argsCount: IntRange) : Fu
1940

2041
override fun invoke(vararg args: Any): Any {
2142
require(args.size == 1) { "$name function requires 1 argument" }
22-
val operand = args[0]
23-
require(operand is Number) { "$name is called with argument type ${Number::class.simpleName}, but supports only numbers" }
43+
val operand = args.getAsNumber(0) {
44+
"$name is called with argument type ${Number::class.simpleName}, but supports only numbers"
45+
}
2446

2547
return invokeInternal(operand)
2648
}
@@ -88,10 +110,8 @@ object DefaultFunctions {
88110

89111
val LOG = object: Function("log", 2) {
90112
override fun invoke(vararg args: Any): Any {
91-
val operand = args[0]
92-
val base = args[1]
93-
require(operand is Number) { "$name argument must be a number" }
94-
require(base is Number) { "$name base must be a number" }
113+
val operand = args.getAsNumber(0) { "$name argument must be a number" }
114+
val base = args.getAsNumber(1) { "$name base must be a number" }
95115

96116
return log(operand.toDouble(), base.toDouble())
97117
}

0 commit comments

Comments
 (0)