Skip to content

Commit 6b4877f

Browse files
Automated commit of generated code
1 parent 6ed2f0c commit 6b4877f

File tree

4 files changed

+1277
-0
lines changed
  • core/generated-sources/src

4 files changed

+1277
-0
lines changed

core/generated-sources/src/main/kotlin/org/jetbrains/kotlinx/dataframe/api/first.kt

Lines changed: 321 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,32 +24,181 @@ import kotlin.reflect.KProperty
2424

2525
// region DataColumn
2626

27+
/**
28+
* Returns the first value in this [DataColumn].
29+
*
30+
* See also [firstOrNull], [last], [take], [takeLast].
31+
*
32+
* @return The first value in this [DataColumn].
33+
*
34+
* @throws [IndexOutOfBoundsException] if the [DataColumn] is empty.
35+
*/
2736
public fun <T> DataColumn<T>.first(): T = get(0)
2837

38+
/**
39+
* Returns the first value in this [DataColumn]. If the [DataColumn] is empty, returns `null`.
40+
*
41+
* See also [first], [last], [take], [takeLast].
42+
*
43+
* @return The first value in this [DataColumn], or `null` if the [DataColumn] is empty.
44+
*/
2945
public fun <T> DataColumn<T>.firstOrNull(): T? = if (size > 0) first() else null
3046

47+
/**
48+
* Returns the first value in this [DataColumn] that matches the given [predicate].
49+
*
50+
* ### Example
51+
* ```kotlin
52+
* // In a DataFrame of financial transactions sorted by time,
53+
* // find the amount of the first transaction over 100 euros
54+
* df.amount.first { it > 100 }
55+
* ```
56+
*
57+
* See also [firstOrNull], [last], [take], [takeLast].
58+
*
59+
* @param [predicate] A lambda expression used to get the first value
60+
* that satisfies a condition specified in this expression.
61+
* This predicate takes a value from the [DataColumn] as an input
62+
* and returns `true` if the value satisfies the condition or `false` otherwise.
63+
*
64+
* @return The first value in this [DataColumn] that matches the given [predicate].
65+
*
66+
* @throws [NoSuchElementException] if the [DataColumn] contains no elements matching the [predicate]
67+
* (including the case when the [DataColumn] is empty).
68+
*/
3169
public fun <T> DataColumn<T>.first(predicate: (T) -> Boolean): T = values.first(predicate)
3270

71+
/**
72+
* Returns the first value in this [DataColumn] that matches the given [predicate].
73+
* Returns `null` if the [DataColumn] contains no elements matching the [predicate]
74+
* (including the case when the [DataColumn] is empty).
75+
*
76+
* ### Example
77+
* ```kotlin
78+
* // In a DataFrame of financial transactions sorted by time,
79+
* // find the amount of the first transaction over 100 euros,
80+
* // or 'null' if there is no such transaction
81+
* df.amount.firstOrNull { it > 100 }
82+
* ```
83+
*
84+
* See also [first], [last], [take], [takeLast].
85+
*
86+
* @param [predicate] A lambda expression used to get the first value
87+
* that satisfies a condition specified in this expression.
88+
* This predicate takes a value from the [DataColumn] as an input
89+
* and returns `true` if the value satisfies the condition or `false` otherwise.
90+
*
91+
* @return The first value in this [DataColumn] that matches the given [predicate],
92+
* or `null` if the [DataColumn] contains no elements matching the [predicate].
93+
*/
3394
public fun <T> DataColumn<T>.firstOrNull(predicate: (T) -> Boolean): T? = values.firstOrNull(predicate)
3495

3596
// endregion
3697

3798
// region DataFrame
3899

100+
/**
101+
* Returns the first [row][DataRow] in this [DataFrame].
102+
*
103+
* See also [firstOrNull][DataFrame.firstOrNull],
104+
* [last][DataFrame.last],
105+
* [take][DataFrame.take],
106+
* [takeWhile][DataFrame.takeWhile],
107+
* [takeLast][DataFrame.takeLast].
108+
*
109+
* @return A [DataRow] containing the first row in this [DataFrame].
110+
*
111+
* @throws NoSuchElementException if the [DataFrame] contains no rows.
112+
*/
39113
public fun <T> DataFrame<T>.first(): DataRow<T> {
40114
if (nrow == 0) {
41115
throw NoSuchElementException("DataFrame has no rows. Use `firstOrNull`.")
42116
}
43117
return get(0)
44118
}
45119

120+
/**
121+
* Returns the first [row][DataRow] in this [DataFrame]. If the [DataFrame] does not contain any rows, returns `null`.
122+
*
123+
* See also [first][DataFrame.first],
124+
* [last][DataFrame.last],
125+
* [take][DataFrame.take],
126+
* [takeWhile][DataFrame.takeWhile],
127+
* [takeLast][DataFrame.takeLast].
128+
*
129+
* @return A [DataRow] containing the first row in this [DataFrame], or `null` if the [DataFrame] is empty.
130+
*/
46131
public fun <T> DataFrame<T>.firstOrNull(): DataRow<T>? = if (nrow > 0) first() else null
47132

133+
/**
134+
* Returns the first [row][DataRow] in this [DataFrame] that satisfies the given [predicate].
135+
*
136+
* The [predicate] is a [RowFilter][org.jetbrains.kotlinx.dataframe.RowFilter] — a lambda that receives each [DataRow][org.jetbrains.kotlinx.dataframe.DataRow] as both `this` and `it`
137+
* and is expected to return a [Boolean] value.
138+
*
139+
* It allows you to define conditions using the row's values directly,
140+
* including through [extension properties][org.jetbrains.kotlinx.dataframe.documentation.ExtensionPropertiesAPIDocs] for convenient and type-safe access.
141+
*
142+
* This can include [column groups][org.jetbrains.kotlinx.dataframe.columns.ColumnGroup] and nested columns.
143+
*
144+
* ### Example
145+
* ```kotlin
146+
* // In a DataFrame of financial transactions sorted by time,
147+
* // find the first transaction with amount over 100 euros
148+
* df.first { amount > 100 }
149+
* ```
150+
*
151+
* See also [firstOrNull][DataFrame.firstOrNull],
152+
* [last][DataFrame.last],
153+
* [take][DataFrame.take],
154+
* [takeWhile][DataFrame.takeWhile],
155+
* [takeLast][DataFrame.takeLast].
156+
*
157+
* @param [predicate] A [row filter][RowFilter] used to get the first value
158+
* that satisfies a condition specified in this filter.
159+
*
160+
* @return A [DataRow] containing the first row that matches the given [predicate].
161+
*
162+
* @throws [NoSuchElementException] if the [DataFrame] contains no rows matching the [predicate].
163+
*/
48164
public inline fun <T> DataFrame<T>.first(predicate: RowFilter<T>): DataRow<T> =
49165
rows().first {
50166
predicate(it, it)
51167
}
52168

169+
/**
170+
* Returns the first [row][DataRow] in this [DataFrame] that satisfies the given [predicate].
171+
* Returns `null` if the [DataFrame] contains no rows matching the [predicate]
172+
* (including the case when the [DataFrame] is empty).
173+
*
174+
* The [predicate] is a [RowFilter][org.jetbrains.kotlinx.dataframe.RowFilter] — a lambda that receives each [DataRow][org.jetbrains.kotlinx.dataframe.DataRow] as both `this` and `it`
175+
* and is expected to return a [Boolean] value.
176+
*
177+
* It allows you to define conditions using the row's values directly,
178+
* including through [extension properties][org.jetbrains.kotlinx.dataframe.documentation.ExtensionPropertiesAPIDocs] for convenient and type-safe access.
179+
*
180+
* This can include [column groups][org.jetbrains.kotlinx.dataframe.columns.ColumnGroup] and nested columns.
181+
*
182+
* ### Example
183+
* ```kotlin
184+
* // In a DataFrame of financial transactions sorted by time,
185+
* // find the first transaction with amount over 100 euros,
186+
* // or 'null' if there is no such transaction
187+
* df.firstOrNull { amount > 100 }
188+
* ```
189+
*
190+
* See also [first][DataFrame.first],
191+
* [last][DataFrame.last],
192+
* [take][DataFrame.take],
193+
* [takeWhile][DataFrame.takeWhile],
194+
* [takeLast][DataFrame.takeLast].
195+
*
196+
* @param [predicate] A [row filter][RowFilter] used to get the first value
197+
* that satisfies a condition specified in this filter.
198+
*
199+
* @return A [DataRow] containing the first row that matches the given [predicate],
200+
* or `null` if the [DataFrame] contains no rows matching the [predicate].
201+
*/
53202
public inline fun <T> DataFrame<T>.firstOrNull(predicate: RowFilter<T>): DataRow<T>? =
54203
rows().firstOrNull {
55204
predicate(it, it)
@@ -59,26 +208,198 @@ public inline fun <T> DataFrame<T>.firstOrNull(predicate: RowFilter<T>): DataRow
59208

60209
// region GroupBy
61210

211+
/**
212+
* [Reduces][GroupByDocs.Reducing] the groups of this [GroupBy]
213+
* by taking the first [row][DataRow] from each group,
214+
* and returns a [ReducedGroupBy] containing these rows
215+
* (one [row][DataRow] per group, each [row][DataRow] is the first [row][DataRow] in its group).
216+
*
217+
* If a group in this [GroupBy] is empty,
218+
* the corresponding [row][DataRow] in the resulting [ReducedGroupBy] will contain `null` values
219+
* for all columns in the group, except the grouping key.
220+
*
221+
* ### Example
222+
* ```kotlin
223+
* // In a DataFrame of orders sorted by date and time,
224+
* // find the first order placed by each customer
225+
* df.groupBy { customerId }.first().concat()
226+
* ```
227+
*
228+
* See also [last][GroupBy.last].
229+
*
230+
* @return A [ReducedGroupBy] containing the first [row][DataRow]
231+
* (or a [row][DataRow] with `null` values, except the grouping key) from each group.
232+
*/
62233
@Interpretable("GroupByReducePredicate")
63234
public fun <T, G> GroupBy<T, G>.first(): ReducedGroupBy<T, G> = reduce { firstOrNull() }
64235

236+
/**
237+
* [Reduces][GroupByDocs.Reducing] the groups of this [GroupBy]
238+
* by taking from each group the first [row][DataRow] satisfying the given [predicate],
239+
* and returns a [ReducedGroupBy] containing these rows (one [row][DataRow] per group,
240+
* each [row][DataRow] is the first [row][DataRow] in its group that satisfies the [predicate]).
241+
*
242+
* If the group in [GroupBy] contains no matching rows,
243+
* the corresponding row in [ReducedGroupBy] will contain `null` values for all columns in the group,
244+
* except the grouping key.
245+
*
246+
* The [predicate] is a [RowFilter][org.jetbrains.kotlinx.dataframe.RowFilter] — a lambda that receives each [DataRow][org.jetbrains.kotlinx.dataframe.DataRow] as both `this` and `it`
247+
* and is expected to return a [Boolean] value.
248+
*
249+
* It allows you to define conditions using the row's values directly,
250+
* including through [extension properties][org.jetbrains.kotlinx.dataframe.documentation.ExtensionPropertiesAPIDocs] for convenient and type-safe access.
251+
*
252+
* This can include [column groups][org.jetbrains.kotlinx.dataframe.columns.ColumnGroup] and nested columns.
253+
*
254+
* ### Example
255+
* ```kotlin
256+
* // In a DataFrame of orders sorted by date and time,
257+
* // find the first order over 100 euros placed by each customer
258+
* df.groupBy { customerId }.first { total > 100 }.concat()
259+
* ```
260+
*
261+
* See also [last][GroupBy.last].
262+
*
263+
* @param [predicate] A [row filter][RowFilter] used to get the first value
264+
* that satisfies a condition specified in this filter.
265+
*
266+
* @return A [ReducedGroupBy] containing the first [row][DataRow] matching the [predicate]
267+
* (or a [row][DataRow] with `null` values, except the grouping key) from each group.
268+
*/
65269
@Interpretable("GroupByReducePredicate")
66270
public fun <T, G> GroupBy<T, G>.first(predicate: RowFilter<G>): ReducedGroupBy<T, G> = reduce { firstOrNull(predicate) }
67271

68272
// endregion
69273

70274
// region Pivot
71275

276+
/**
277+
* [Reduces][PivotDocs.Reducing] this [Pivot] by taking the first [row][DataRow] from each group,
278+
* and returns a [ReducedPivot] that contains the first [row][DataRow] from the corresponding group in each column.
279+
*
280+
* For more information about [Pivot] with examples: [See `pivot` on the documentation website.](https://kotlin.github.io/dataframe/pivot.html)
281+
*
282+
* ### Example
283+
* ```kotlin
284+
* // In a DataFrame of real estate listings sorted by price,
285+
* // find the cheapest listing for each type of property (house, apartment, etc.)
286+
* df.pivot { type }.first().values()
287+
* ```
288+
*
289+
* See also [pivot], [reduce][Pivot.reduce], [last][Pivot.last].
290+
*
291+
* @return A [ReducedPivot] containing in each column the first [row][DataRow] from the corresponding group.
292+
*/
72293
public fun <T> Pivot<T>.first(): ReducedPivot<T> = reduce { firstOrNull() }
73294

295+
/**
296+
* [Reduces][PivotDocs.Reducing] this [Pivot] by taking from each group the first [row][DataRow]
297+
* satisfying the given [predicate], and returns a [ReducedPivot] that contains the first row, matching the [predicate],
298+
* from the corresponding group in each column.
299+
*
300+
* For more information about [Pivot] with examples: [See `pivot` on the documentation website.](https://kotlin.github.io/dataframe/pivot.html)
301+
*
302+
* The [predicate] is a [RowFilter][org.jetbrains.kotlinx.dataframe.RowFilter] — a lambda that receives each [DataRow][org.jetbrains.kotlinx.dataframe.DataRow] as both `this` and `it`
303+
* and is expected to return a [Boolean] value.
304+
*
305+
* It allows you to define conditions using the row's values directly,
306+
* including through [extension properties][org.jetbrains.kotlinx.dataframe.documentation.ExtensionPropertiesAPIDocs] for convenient and type-safe access.
307+
*
308+
* This can include [column groups][org.jetbrains.kotlinx.dataframe.columns.ColumnGroup] and nested columns.
309+
*
310+
* ### Example
311+
* ```kotlin
312+
* // In a DataFrame of real estate listings sorted by price,
313+
* // find the cheapest listing for each type of property (house, apartment, etc.)
314+
* // with is not yet sold out.
315+
* df.pivot { type }.first { !soldOut }.values()
316+
* ```
317+
*
318+
* See also [pivot], [reduce][Pivot.reduce], [last][Pivot.last].
319+
*
320+
* @param [predicate] A [row filter][RowFilter] used to get the first value
321+
* that satisfies a condition specified in this filter.
322+
*
323+
* @return A [ReducedPivot] containing in each column the first [row][DataRow]
324+
* that satisfies the [predicate], from the corresponding group (or a [row][DataRow] with `null` values).
325+
*/
74326
public fun <T> Pivot<T>.first(predicate: RowFilter<T>): ReducedPivot<T> = reduce { firstOrNull(predicate) }
75327

76328
// endregion
77329

78330
// region PivotGroupBy
79331

332+
/**
333+
* [Reduces][PivotGroupByDocs.Reducing] this [PivotGroupBy] by taking the first [row][DataRow]
334+
* from each combined [pivot] + [groupBy] group, and returns a [ReducedPivotGroupBy]
335+
* that contains the first row from each corresponding group.
336+
* If any combined [pivot] + [groupBy] group in [PivotGroupBy] is empty, in the resulting [ReducedPivotGroupBy]
337+
* it will be represented by a [row][DataRow] with `null` values (except the grouping key).
338+
*
339+
* For more information about [PivotGroupBy] with examples: [See "`pivot` + `groupBy`" on the documentation website.](https://kotlin.github.io/dataframe/pivot.html#pivot-groupby)
340+
*
341+
* ### Example
342+
* ```kotlin
343+
* // In a DataFrame of real estate listings sorted by price,
344+
* // find the cheapest listing for each combination of type of property (house, apartment, etc.)
345+
* // and the city it is located in
346+
* df.pivot { type }.groupBy { city }.first().values()
347+
* ```
348+
*
349+
* See also [groupBy][Pivot.groupBy],
350+
* [pivot][GroupBy.pivot],
351+
* [reduce][PivotGroupBy.reduce],
352+
* [last][PivotGroupBy.last].
353+
*
354+
* @return A [ReducedPivotGroupBy] containing in each combination of a [groupBy] key and a [pivot] key either
355+
* the first [row][DataRow] of the corresponding [DataFrame] formed by this pivot–group pair,
356+
* or a [row][DataRow] with `null` values (except the grouping key) if this [DataFrame] is empty.
357+
*/
80358
public fun <T> PivotGroupBy<T>.first(): ReducedPivotGroupBy<T> = reduce { firstOrNull() }
81359

360+
/**
361+
* [Reduces][PivotGroupByDocs.Reducing] this [PivotGroupBy]
362+
* by taking from each combined [pivot] + [groupBy] group the first [row][DataRow] satisfying the given [predicate].
363+
* Returns a [ReducedPivotGroupBy] that contains the first row, matching the [predicate], from each corresponding group.
364+
* If any combined [pivot] + [groupBy] group in [PivotGroupBy] does not contain any rows matching the [predicate],
365+
* in the resulting [ReducedPivotGroupBy] it will be represented by a [row][DataRow] with `null` values
366+
* (except the grouping key).
367+
*
368+
* [See "`pivot` + `groupBy`" on the documentation website.](https://kotlin.github.io/dataframe/pivot.html#pivot-groupby)
369+
*
370+
* [See `pivot` on the documentation website.](https://kotlin.github.io/dataframe/pivot.html)
371+
*
372+
* [See `groupBy` on the documentation website.](https://kotlin.github.io/dataframe/groupby.html)
373+
*
374+
* The [predicate] is a [RowFilter][org.jetbrains.kotlinx.dataframe.RowFilter] — a lambda that receives each [DataRow][org.jetbrains.kotlinx.dataframe.DataRow] as both `this` and `it`
375+
* and is expected to return a [Boolean] value.
376+
*
377+
* It allows you to define conditions using the row's values directly,
378+
* including through [extension properties][org.jetbrains.kotlinx.dataframe.documentation.ExtensionPropertiesAPIDocs] for convenient and type-safe access.
379+
*
380+
* This can include [column groups][org.jetbrains.kotlinx.dataframe.columns.ColumnGroup] and nested columns.
381+
*
382+
* ### Example
383+
* ```kotlin
384+
* // In a DataFrame of real estate listings sorted by price,
385+
* // for each combination of type of property (house, apartment, etc.)
386+
* // and the city it is located in,
387+
* // find the cheapest listing that is not yet sold out
388+
* df.pivot { type }.groupBy { city }.first { !soldOut }.values()
389+
* ```
390+
*
391+
* See also [groupBy][Pivot.groupBy],
392+
* [pivot][GroupBy.pivot],
393+
* [reduce][PivotGroupBy.reduce],
394+
* [last][PivotGroupBy.last].
395+
*
396+
* @param [predicate] A [row filter][RowFilter] used to get the first value
397+
* that satisfies a condition specified in this filter.
398+
*
399+
* @return A [ReducedPivotGroupBy] containing in each combination of a [groupBy] key and a [pivot] key either
400+
* the first matching the [predicate] [row][DataRow] of the corresponding [DataFrame] formed by this pivot–group pair,
401+
* or a [row][DataRow] with `null` values if this [DataFrame] does not contain any rows matching the [predicate].
402+
*/
82403
public fun <T> PivotGroupBy<T>.first(predicate: RowFilter<T>): ReducedPivotGroupBy<T> =
83404
reduce { firstOrNull(predicate) }
84405

0 commit comments

Comments
 (0)