Skip to content

Commit 6632fc1

Browse files
committed
Rename new() into newValue for AddDataRow receiver. Add KDocs and fibonacci sample.
1 parent d88275a commit 6632fc1

File tree

7 files changed

+59
-11
lines changed

7 files changed

+59
-11
lines changed

core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/api/add.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,18 @@ public fun <T> DataFrame<T>.addAll(dataFrames: Iterable<AnyFrame>): DataFrame<T>
8181
// region Create and add a single column
8282

8383
/**
84-
* Receiver that is used in [add] and [update] operations to access new (added or updated) column value in preceding row
84+
* Receiver that is used in [add] and [update] operations to access new (added or updated) column value in preceding row.
8585
*/
8686
public interface AddDataRow<out T> : DataRow<T> {
8787

88-
public fun <C> AnyRow.new(): C
88+
/**
89+
* Returns a new value that was already computed for some preceding row during current [add] or [update] column operation.
90+
*
91+
* Can be used to compute series of values with recurrence relations, e.g. fibonacci.
92+
*
93+
* @throws IndexOutOfBoundsException when called on a successive row that doesn't have new value yet
94+
*/
95+
public fun <C> AnyRow.newValue(): C
8996
}
9097

9198
public typealias AddExpression<T, C> = Selector<AddDataRow<T>, C>

core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/columns/constructors.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ internal class AddDataRowImpl<T>(index: Int, owner: DataFrame<T>, private val co
4646
DataRowImpl<T>(index, owner),
4747
AddDataRow<T> {
4848

49-
override fun <C> AnyRow.new() = container[index] as C
49+
override fun <C> AnyRow.newValue() = container[index] as C
5050
}
5151

5252
@PublishedApi

core/src/test/kotlin/org/jetbrains/kotlinx/dataframe/puzzles/HardTests.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ class HardTests {
1818
val y = columnOf(1, 2, 0, 1, 2, 3, 4, 0, 1, 2).named("Y")
1919

2020
df.mapToColumn("Y") {
21-
if (it[x] == 0) 0 else (prev()?.new() ?: 0) + 1
21+
if (it[x] == 0) 0 else (prev()?.newValue() ?: 0) + 1
2222
} shouldBe y
2323

2424
df.mapToColumn("Y") {
25-
if (it["X"] == 0) 0 else (prev()?.new() ?: 0) + 1
25+
if (it["X"] == 0) 0 else (prev()?.newValue() ?: 0) + 1
2626
} shouldBe y
2727
}
2828

docs/StardustDocs/topics/add.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,19 @@ df.add("year of birth") { 2021 - "age"<Int>() }
4242

4343
See [row expressions](DataRow.md#row-expressions)
4444

45+
You can use `newValue()` function to access value that was already calculated for preceding row. It is helpful for recurrent computations:
46+
47+
<!---FUN addRecurrent-->
48+
49+
```kotlin
50+
df.add("fibonacci") {
51+
if(index() < 2) 1
52+
else prev()!!.newValue<Int>() + prev()!!.prev()!!.newValue<Int>()
53+
}
54+
```
55+
56+
<!---END-->
57+
4558
**Create and add several columns to `DataFrame`:**
4659

4760
```kotlin

examples/jupyter-notebooks/puzzles/40 puzzles.ipynb

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1985,7 +1985,11 @@
19851985
{
19861986
"cell_type": "code",
19871987
"execution_count": 37,
1988-
"metadata": {},
1988+
"metadata": {
1989+
"pycharm": {
1990+
"name": "#%%\n"
1991+
}
1992+
},
19891993
"outputs": [
19901994
{
19911995
"data": {
@@ -2022,7 +2026,7 @@
20222026
],
20232027
"source": [
20242028
"df.mapToColumn(\"Y\") {\n",
2025-
" if(it.X == 0) 0 else (prev()?.new() ?: 0) + 1\n",
2029+
" if(it.X == 0) 0 else (prev()?.newValue() ?: 0) + 1\n",
20262030
"}"
20272031
]
20282032
},
@@ -3094,9 +3098,13 @@
30943098
},
30953099
{
30963100
"cell_type": "markdown",
3097-
"metadata": {},
3101+
"metadata": {
3102+
"pycharm": {
3103+
"name": "#%% md\n"
3104+
}
3105+
},
30983106
"source": [
3099-
"The DataFrame looks much better now!"
3107+
"The DataFrame looks much better now!\n"
31003108
]
31013109
}
31023110
],
@@ -3118,4 +3126,4 @@
31183126
},
31193127
"nbformat": 4,
31203128
"nbformat_minor": 1
3121-
}
3129+
}

tests/src/test/kotlin/org/jetbrains/kotlinx/dataframe/api/add.kt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.jetbrains.kotlinx.dataframe.api
22

3+
import io.kotest.assertions.throwables.shouldThrow
34
import io.kotest.matchers.shouldBe
45
import org.junit.Test
56

@@ -9,8 +10,17 @@ class AddTests {
910
fun `add with new`() {
1011
val x by columnOf(7, 2, 0, 3, 4, 2, 5, 0, 3, 4)
1112
val df = dataFrameOf(x)
12-
val added = df.add("Y") { if (x() == 0) 0 else (prev()?.new() ?: 0) + 1 }
13+
val added = df.add("Y") { if (x() == 0) 0 else (prev()?.newValue() ?: 0) + 1 }
1314
val expected = listOf(1, 2, 0, 1, 2, 3, 4, 0, 1, 2)
1415
added["Y"].values() shouldBe expected
1516
}
17+
18+
@Test
19+
fun `throw for newValue at the next row`() {
20+
val x by columnOf(7, 2, 0, 3, 4, 2, 5, 0, 3, 4)
21+
val df = dataFrameOf(x)
22+
shouldThrow<IndexOutOfBoundsException> {
23+
df.add("y") { next()?.newValue() ?: 1 }
24+
}
25+
}
1626
}

tests/src/test/kotlin/org/jetbrains/kotlinx/dataframe/samples/api/Modify.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,16 @@ class Modify : TestBase() {
692692
// SampleEnd
693693
}
694694

695+
@Test
696+
fun addRecurrent() {
697+
// SampleStart
698+
df.add("fibonacci") {
699+
if(index() < 2) 1
700+
else prev()!!.newValue<Int>() + prev()!!.prev()!!.newValue<Int>()
701+
}
702+
// SampleEnd
703+
}
704+
695705
@Test
696706
fun addExisting() {
697707
// SampleStart

0 commit comments

Comments
 (0)