Skip to content

Commit

Permalink
Add .range function
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgio committed Nov 23, 2024
1 parent b9c5df1 commit 5d04351
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
22 changes: 22 additions & 0 deletions stdlib/src/main/kotlin/eu/iamgio/quarkdown/stdlib/Math.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package eu.iamgio.quarkdown.stdlib
import eu.iamgio.quarkdown.function.reflect.annotation.Name
import eu.iamgio.quarkdown.function.value.BooleanValue
import eu.iamgio.quarkdown.function.value.NumberValue
import eu.iamgio.quarkdown.function.value.ObjectValue
import eu.iamgio.quarkdown.function.value.data.Range
import kotlin.math.PI
import kotlin.math.pow

Expand All @@ -22,6 +24,7 @@ val Math: Module =
::cos,
::tan,
::isEven,
::range,
)

// Basic operations
Expand Down Expand Up @@ -105,3 +108,22 @@ fun tan(x: Number) = NumberValue(kotlin.math.tan(x.toFloat()))
*/
@Name("iseven")
fun isEven(x: Number) = BooleanValue(x.toInt() % 2 == 0)

/**
* Creates a range of numbers, which can also be iterated through.
* The behavior of an open range is delegated to the consumer.
* For instance, using a left-open range with [forEach] will make the loop start from 1.
* The difference between this function and the built-in `..` operator is that the latter
* does not allow for dynamic evaluation, hence both ends must be literals.
* This function allows evaluating ends dynamically: for instance, `.range from:{1} to:{.sum {1} {2}}`.
* Floating-point numbers are truncated to integers.
* @property start start of the range (inclusive). If `null`, the range is infinite on the left end
* @property end end of the range (inclusive). If `null`, the range is infinite on the right end. [end] > [start]
*/
fun range(
@Name("from") start: Number? = null,
@Name("to") end: Number? = null,
): ObjectValue<Range> =
ObjectValue(
Range(start?.toInt(), end?.toInt()),
)
12 changes: 10 additions & 2 deletions test/src/test/kotlin/eu/iamgio/quarkdown/test/FullPipelineTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,14 @@ class FullPipelineTest {
assertEquals("<p><strong>N:</strong> 1</p><p><strong>N:</strong> 2</p><p><strong>N:</strong> 3</p>", it)
}

execute(".foreach {2..4}\n **N:** .1") {
assertEquals("<p><strong>N:</strong> 2</p><p><strong>N:</strong> 3</p><p><strong>N:</strong> 4</p>", it)
}

execute(".foreach {.range from:{2} to:{4}}\n **N:** .1") {
assertEquals("<p><strong>N:</strong> 2</p><p><strong>N:</strong> 3</p><p><strong>N:</strong> 4</p>", it)
}

execute(
"""
## Title
Expand All @@ -623,7 +631,7 @@ class FullPipelineTest {

execute(
"""
.foreach {..2}
.foreach {.range to:{.sum {1} {1}}}
## Hello .1
.foreach {..1}
**Hi**!
Expand All @@ -646,7 +654,7 @@ class FullPipelineTest {
execute(
"""
.foreach {..2}
.foreach {..2}
.foreach {.range to:{2}}
.foreach {..2}
## Title 2
# Title 1
Expand Down

0 comments on commit 5d04351

Please sign in to comment.