All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.skillw.asahi.internal.namespacing.prefix.lang.math.Number.kt Maven / Gradle / Ivy

The newest version!
package com.skillw.asahi.internal.namespacing.prefix.lang.math

import com.skillw.asahi.api.annotation.AsahiPrefix
import com.skillw.asahi.api.prefixParser
import com.skillw.asahi.api.quest
import com.skillw.asahi.util.cast
import com.skillw.pouvoir.util.format
import kotlin.math.absoluteValue

/**
 * @className Number
 *
 * @author Glom
 * @date 2023/1/14 0:31 Copyright 2024 Glom.
 */


@AsahiPrefix(["abs"], "lang")
private fun abs() = prefixParser {
    val number = quest()
    result { number.get().absoluteValue }
}

@AsahiPrefix(["ceil"], "lang")
private fun ceil() = prefixParser {
    val number = quest()
    result { kotlin.math.ceil(number.get()) }
}

@AsahiPrefix(["floor"], "lang")
private fun floor() = prefixParser {
    val number = quest()
    result { kotlin.math.floor(number.get()) }
}

@AsahiPrefix(["format"], "lang")
private fun format() = prefixParser {
    val number = quest()
    val format = quest()
    result { number.get().format(format.get()) }
}

@AsahiPrefix(["max"], "lang")
private fun max() = prefixParser {
    val value = if (peek() == "[")
        quest>()
    else {
        val a = quest()
        expect("to")
        val b = quest()
        result { a.get() to b.get() }
    }
    result {
        when (val values = value.get()) {
            is List<*> -> {
                val numbers = values.map { it.cast() }
                numbers.maxOf { it }
            }

            is Pair<*, *> -> {
                val a = values.first.cast()
                val b = values.second.cast()
                kotlin.math.max(a, b)
            }

            else -> 0.0
        }
    }
}

@AsahiPrefix(["min"], "lang")
private fun min() = prefixParser {
    val value = if (peek() == "[")
        quest>()
    else {
        val a = quest()
        expect("to")
        val b = quest()
        result { a.get() to b.get() }
    }
    result {
        when (val values = value.get()) {
            is List<*> -> {
                val numbers = values.map { it.cast() }
                numbers.minOf { it }
            }

            is Pair<*, *> -> {
                val a = values.first.cast()
                val b = values.second.cast()
                kotlin.math.min(a, b)
            }

            else -> 0.0
        }
    }
}

@AsahiPrefix(["round"], "lang")
private fun round() = prefixParser {
    val x = quest()
    result { kotlin.math.round(x.get()).toInt() }
}

@AsahiPrefix(["range"], "lang")
private fun range() = prefixParser> {
    val from = quest()
    expect("to", "~", "..")
    val to = quest()
    result {
        val a = from.get()
        val b = to.get()
        a..b
    }
}

@AsahiPrefix(["number"], "lang")
private fun number() = prefixParser {
    val number = quest()
    result { number.get() }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy