@@ -11,6 +11,7 @@ import org.jetbrains.kotlinx.dataframe.DataColumn
11
11
import org.jetbrains.kotlinx.dataframe.DataFrame
12
12
import org.jetbrains.kotlinx.dataframe.DataRow
13
13
import org.jetbrains.kotlinx.dataframe.Predicate
14
+ import org.jetbrains.kotlinx.dataframe.annotations.DataSchema
14
15
import org.jetbrains.kotlinx.dataframe.columns.ColumnAccessor
15
16
import org.jetbrains.kotlinx.dataframe.columns.ColumnGroup
16
17
import org.jetbrains.kotlinx.dataframe.columns.ColumnPath
@@ -23,6 +24,7 @@ import org.jetbrains.kotlinx.dataframe.columns.SingleColumn
23
24
import org.jetbrains.kotlinx.dataframe.columns.renamedReference
24
25
import org.jetbrains.kotlinx.dataframe.columns.toColumnSet
25
26
import org.jetbrains.kotlinx.dataframe.documentation.AccessApi
27
+ import org.jetbrains.kotlinx.dataframe.documentation.ColumnExpression
26
28
import org.jetbrains.kotlinx.dataframe.documentation.DocumentationUrls
27
29
import org.jetbrains.kotlinx.dataframe.documentation.LineBreak
28
30
import org.jetbrains.kotlinx.dataframe.hasNulls
@@ -41,7 +43,7 @@ import org.jetbrains.kotlinx.dataframe.impl.columns.top
41
43
import org.jetbrains.kotlinx.dataframe.impl.columns.transform
42
44
import org.jetbrains.kotlinx.dataframe.impl.columns.transformSingle
43
45
import org.jetbrains.kotlinx.dataframe.impl.columns.tree.dfs
44
- import org.jetbrains.kotlinx.dataframe.documentation.*
46
+ import org.jetbrains.kotlinx.dataframe.io.read
45
47
import kotlin.reflect.KProperty
46
48
import kotlin.reflect.KType
47
49
import kotlin.reflect.typeOf
@@ -196,8 +198,97 @@ internal interface ColumnsSelectionDslLink
196
198
/* * @include [CommonColumnSelectionDocs] */
197
199
public interface ColumnsSelectionDsl <out T > : ColumnSelectionDsl <T >, SingleColumn <DataRow <T >> {
198
200
199
- public fun <C > ColumnSet<C>.first (condition : ColumnFilter <C >): SingleColumn <C > =
200
- transform { listOf (it.first(condition)) }.single()
201
+ /* *
202
+ * ## First
203
+ * Returns the first column in this [ColumnSet] that adheres to the given [condition\].
204
+ *
205
+ * For example:
206
+ *
207
+ * {@includeArg [Examples]}
208
+ *
209
+ * @param [condition\] The [ColumnFilter] condition that the column must adhere to.
210
+ * @return A [SingleColumn] containing the first column that adheres to the given [condition\].
211
+ * @see [last\]
212
+ */
213
+ private interface CommonFirstDocs {
214
+
215
+ /* * Examples key */
216
+ interface Examples
217
+ }
218
+
219
+ /* *
220
+ * @include [CommonFirstDocs]
221
+ * @arg [CommonFirstDocs.Examples]
222
+ * `df.`[select][select]` { `[first][first]` { it.`[name][ColumnReference.name]`().`[startsWith][String.startsWith]`("year") } }`
223
+ *
224
+ * `df.`[select][select]` { myColumnGroup.`[first][first]`() }`
225
+ */
226
+ public fun <C > ColumnSet<C>.first (condition : ColumnFilter <C > = { true }): SingleColumn <C > =
227
+ children { condition(it.cast()) }[0 ].cast()
228
+
229
+ /* *
230
+ * @include [CommonFirstDocs]
231
+ * @arg [CommonFirstDocs.Examples]
232
+ * `df.`[select][select]` { "myColumnGroup".`[first][first]` { it.`[name][ColumnReference.name]`().`[startsWith][String.startsWith]`("year") } }`
233
+ */
234
+ public fun String.first (condition : ColumnFilter <* > = { true }): SingleColumn <* > =
235
+ toColumnAccessor().first(condition)
236
+
237
+ /* *
238
+ * @include [CommonFirstDocs]
239
+ * @arg [CommonFirstDocs.Examples]
240
+ * `df.`[select][select]` { Type::myColumnGroup.`[first][first]` { it.`[name][ColumnReference.name]`().`[startsWith][String.startsWith]`("year") } }`
241
+ */
242
+ public fun <C > KProperty<C>.first (condition : ColumnFilter <C >): SingleColumn <* > =
243
+ toColumnAccessor().first(condition)
244
+
245
+
246
+ /* *
247
+ * ## Last
248
+ * Returns the last column in this [ColumnSet] that adheres to the given [condition\].
249
+ *
250
+ * For example:
251
+ *
252
+ * {@includeArg [Examples]}
253
+ *
254
+ * @param [condition\] The [ColumnFilter] condition that the column must adhere to.
255
+ * @return A [SingleColumn] containing the last column that adheres to the given [condition\].
256
+ * @see [first\]
257
+ */
258
+ private interface CommonLastDocs {
259
+
260
+ /* * Examples key */
261
+ interface Examples
262
+ }
263
+
264
+ /* *
265
+ * @include [CommonLastDocs]
266
+ * @arg [CommonLastDocs.Examples]
267
+ * `df.`[select][select]` { `[last][last]` { it.`[name][ColumnReference.name]`().`[startsWith][String.startsWith]`("year") } }`
268
+ *
269
+ * `df.`[select][select]` { myColumnGroup.`[first][last]`() }`
270
+ */
271
+ public fun <C > ColumnSet<C>.last (condition : ColumnFilter <C > = { true }): SingleColumn <C > =
272
+ children { condition(it.cast()) }
273
+ .transform { listOf (it.last()) }
274
+ .single()
275
+ .cast()
276
+
277
+ /* *
278
+ * @include [CommonLastDocs]
279
+ * @arg [CommonLastDocs.Examples]
280
+ * `df.`[select][select]` { "myColumnGroup".`[last][last]` { it.`[name][ColumnReference.name]`().`[startsWith][String.startsWith]`("year") } }`
281
+ */
282
+ public fun String.last (condition : ColumnFilter <* > = { true }): SingleColumn <* > =
283
+ toColumnAccessor().last(condition)
284
+
285
+ /* *
286
+ * @include [CommonLastDocs]
287
+ * @arg [CommonLastDocs.Examples]
288
+ * `df.`[select][select]` { Type::myColumnGroup.`[last][last]` { it.`[name][ColumnReference.name]`().`[startsWith][String.startsWith]`("year") } }`
289
+ */
290
+ public fun <C > KProperty<C>.last (condition : ColumnFilter <C >): SingleColumn <* > =
291
+ toColumnAccessor().last(condition)
201
292
202
293
public fun <C > ColumnSet<C>.single (condition : ColumnFilter <C >): SingleColumn <C > =
203
294
transform { listOf (it.single(condition)) }.single()
@@ -592,57 +683,47 @@ public interface ColumnsSelectionDsl<out T> : ColumnSelectionDsl<T>, SingleColum
592
683
593
684
/* *
594
685
* @include [CommonColsOfDocs]
595
- * Get sub-columns of the column with this name by [type] without a filter.
686
+ * Get sub-columns of the column with this name by [type] with a [ filter] .
596
687
* For example:
597
688
*
598
689
* `df.`[select][DataFrame.select]` { "myColumnGroup".`[colsOf][colsOf]`(`[typeOf][typeOf]`<`[Int][Int]`>()) }`
599
690
*
600
- * @include [CommonColsOfDocs.Return]
601
- */
602
- public fun String.colsOf (type : KType ): ColumnSet <Any ?> = toColumnAccessor().colsOf(type)
603
-
604
- /* *
605
- * @include [CommonColsOfDocs]
606
- * Get sub-columns of the column with this name by [type] with a [filter].
607
- * For example:
608
- *
609
691
* `df.`[select][DataFrame.select]` { "myColumnGroup".`[colsOf][colsOf]`(`[typeOf][typeOf]`<`[Int][Int]`>()) { it: `[DataColumn][DataColumn]`<`[Int][Int]`> -> it.`[size][DataColumn.size]` > 10 } }`
610
692
*
611
693
* @include [CommonColsOfDocs.FilterParam]
612
- * @include [CommonColsOfDocs.ReturnFiltered ]
694
+ * @include [CommonColsOfDocs.Return ]
613
695
*/
614
- public fun <C > String.colsOf (type : KType , filter : (DataColumn <C >) -> Boolean ): ColumnSet <Any ?> =
696
+ public fun <C > String.colsOf (type : KType , filter : (DataColumn <C >) -> Boolean = { true } ): ColumnSet <Any ?> =
615
697
toColumnAccessor().colsOf(type, filter)
616
698
617
699
/* *
618
700
* @include [CommonColsOfDocs]
619
- * Get sub-columns of the column this [KProperty Accessor][KProperty] points to by [type] without a filter.
701
+ * Get sub-columns of the column this [KProperty Accessor][KProperty] points to by [type] with or without [ filter] .
620
702
* For example:
621
703
*
622
704
* `df.`[select][DataFrame.select]` { Type::myColumnGroup.`[colsOf][colsOf]`(`[typeOf][typeOf]`<`[Int][Int]`>()) }`
623
705
*
624
- * @include [CommonColsOfDocs.Return]
625
- */
626
- public fun KProperty <* >.colsOf (type : KType ): ColumnSet <Any ?> = toColumnAccessor().colsOf(type)
627
-
628
- /* *
629
- * @include [CommonColsOfDocs]
630
- * Get sub-columns of the column this [KProperty Accessor][KProperty] points to by [type] with a [filter].
631
- * For example:
632
- *
633
706
* `df.`[select][DataFrame.select]` { Type::myColumnGroup.`[colsOf][colsOf]`(`[typeOf][typeOf]`<`[Int][Int]`>()) { it: `[DataColumn][DataColumn]`<`[Int][Int]`> -> it.`[size][DataColumn.size]` > 10 } }`
634
707
*
635
708
* @include [CommonColsOfDocs.FilterParam]
636
- * @include [CommonColsOfDocs.ReturnFiltered ]
709
+ * @include [CommonColsOfDocs.Return ]
637
710
*/
638
- public fun <C > KProperty <* >.colsOf (type : KType , filter : (DataColumn <C >) -> Boolean ): ColumnSet <Any ?> =
711
+ public fun <C > KProperty <* >.colsOf (type : KType , filter : (DataColumn <C >) -> Boolean = { true } ): ColumnSet <Any ?> =
639
712
toColumnAccessor().colsOf(type, filter)
640
713
}
641
714
642
715
/* *
643
- * @include [ColumnExpression]
716
+ * @include [ColumnExpression.CommonDocs ]
644
717
*
645
- * TODO
718
+ * For example:
719
+ *
720
+ * `df.`[groupBy][DataFrame.groupBy]` { `[expr][expr]` { firstName.`[length][String.length]` + lastName.`[length][String.length]` } `[named][named]` "nameLength" }`
721
+ *
722
+ * `df.`[sortBy][DataFrame.sortBy]` { `[expr][expr]` { name.`[length][String.length]` }.`[desc][SortDsl.desc]`() }`
723
+ *
724
+ * @param [name] The name the temporary column. Will be empty by default.
725
+ * @include [Infer.Param] By default: [Nulls][Infer.Nulls].
726
+ * @param [expression] An [AddExpression] to define what each new row of the temporary column should contain.
646
727
*/
647
728
public inline fun <T , reified R > ColumnsSelectionDsl<T>.expr (
648
729
name : String = "",
@@ -699,85 +780,54 @@ internal interface ColsOf
699
780
*/
700
781
private interface CommonColsOfDocs {
701
782
702
- /* * @return A [ColumnSet] containing the columns of given type. */
703
- interface Return
704
-
705
783
/* * @return A [ColumnSet] containing the columns of given type that were included by [filter\]. */
706
- interface ReturnFiltered
784
+ interface Return
707
785
708
- /* * @param [filter\] a filter function that takes a column of type [C\] and returns `true` if the column should be included. */
786
+ /* * @param [filter\] an optional filter function that takes a column of type [C\] and returns `true` if the column should be included. */
709
787
interface FilterParam
710
788
}
711
789
712
790
/* *
713
791
* @include [CommonColsOfDocs]
714
- * Get (sub-)columns by [type] without a filter.
792
+ * Get (sub-)columns by [type] with or without [ filter] .
715
793
* For example:
716
794
*
717
795
* `df.`[select][DataFrame.select]` { `[colsOf][colsOf]`(`[typeOf][typeOf]`<`[Int][Int]`>()) }`
718
796
*
719
- * `df.`[select][DataFrame.select]` { myColumnGroup.`[colsOf][colsOf]`(`[typeOf][typeOf]`<`[Int][Int]`>()) }`
720
- *
721
- * @include [CommonColsOfDocs.Return]
722
- */
723
- public fun ColumnSet <* >.colsOf (type : KType ): ColumnSet <Any ?> = colsOf(type) { true }
724
-
725
- /* *
726
- * @include [CommonColsOfDocs]
727
- * Get (sub-)columns by a given type without a filter.
728
- * For example:
729
- *
730
- * `df.`[select][DataFrame.select]` { `[colsOf][colsOf]`<`[Int][Int]`>() }`
731
- *
732
- * `df.`[select][DataFrame.select]` { myColumnGroup.`[colsOf][colsOf]`<`[Int][Int]`>() }`
733
- *
734
- * @include [CommonColsOfDocs.Return]
735
- */
736
- public inline fun <reified C > ColumnSet <* >.colsOf (): ColumnSet <C > = colsOf(typeOf<C >()) as ColumnSet <C >
737
-
738
- /* *
739
- * @include [CommonColsOfDocs]
740
- * Get (sub-)columns by [type] with [filter].
741
- * For example:
742
- *
743
- * `df.`[select][DataFrame.select]` { `[colsOf][colsOf]`(`[typeOf][typeOf]`<`[Int][Int]`>()) { it: `[DataColumn][DataColumn]`<`[Int][Int]`> -> it.`[size][DataColumn.size]` > 10 } }`
744
- *
745
797
* `df.`[select][DataFrame.select]` { myColumnGroup.`[colsOf][colsOf]`(`[typeOf][typeOf]`<`[Int][Int]`>()) { it: `[DataColumn][DataColumn]`<`[Int][Int]`> -> it.`[size][DataColumn.size]` > 10 } }`
746
798
*
799
+ * `df.`[select][DataFrame.select]` { myColumnGroup.`[colsOf][colsOf]`(`[typeOf][typeOf]`<`[Int][Int]`>()) }`
800
+ *
747
801
* @include [CommonColsOfDocs.FilterParam]
748
- * @include [CommonColsOfDocs.ReturnFiltered ]
802
+ * @include [CommonColsOfDocs.Return ]
749
803
*/
750
- public fun <C > ColumnSet <* >.colsOf (type : KType , filter : (DataColumn <C >) -> Boolean ): ColumnSet <C > =
804
+ public fun <C > ColumnSet <* >.colsOf (type : KType , filter : (DataColumn <C >) -> Boolean = { true } ): ColumnSet <C > =
751
805
colsInternal { it.isSubtypeOf(type) && filter(it.cast()) } as ColumnSet <C >
752
806
753
807
/* *
754
808
* @include [CommonColsOfDocs]
755
- * Get (sub-)columns by a given type with filter.
809
+ * Get (sub-)columns by a given type with or without [ filter] .
756
810
* For example:
757
811
*
758
- * `df.`[select][DataFrame.select]` { `[colsOf][colsOf]`<`[Int][Int]`> { it.`[size][DataColumn.size]` > 10 } }`
812
+ * `df.`[select][DataFrame.select]` { `[colsOf][colsOf]`<`[Int][Int]`>() }`
759
813
*
760
814
* `df.`[select][DataFrame.select]` { myColumnGroup.`[colsOf][colsOf]`<`[Int][Int]`> { it.`[size][DataColumn.size]` > 10 } }`
761
815
*
816
+ * `df.`[select][DataFrame.select]` { myColumnGroup.`[colsOf][colsOf]`<`[Int][Int]`>() }`
817
+ *
762
818
* @include [CommonColsOfDocs.FilterParam]
763
- * @include [CommonColsOfDocs.ReturnFiltered ]
819
+ * @include [CommonColsOfDocs.Return ]
764
820
*/
765
821
public inline fun <reified C > ColumnSet <* >.colsOf (noinline filter : (DataColumn <C >) -> Boolean = { true }): ColumnSet <C > =
766
822
colsOf(typeOf<C >(), filter)
767
823
768
824
/* TODO: [Issue: #325, context receiver support](https://github.com/Kotlin/dataframe/issues/325)
769
825
context(ColumnsSelectionDsl)
770
- public inline fun <reified C> KProperty<*>.colsOf(noinline filter: (DataColumn<C>) -> Boolean): ColumnSet<Any?> =
826
+ public inline fun <reified C> KProperty<*>.colsOf(noinline filter: (DataColumn<C>) -> Boolean = { true } ): ColumnSet<Any?> =
771
827
colsOf(typeOf<C>(), filter)
772
828
773
829
context(ColumnsSelectionDsl)
774
- public inline fun <reified C> KProperty<*>.colsOf(): ColumnSet<Any?> =
775
- colsOf(typeOf<C>())
776
-
777
- context(ColumnsSelectionDsl)
778
- public inline fun <reified C> String.colsOf(noinline filter: (DataColumn<C>) -> Boolean): ColumnSet<Any?> =
830
+ public inline fun <reified C> String.colsOf(noinline filter: (DataColumn<C>) -> Boolean = { true }): ColumnSet<Any?> =
779
831
colsOf(typeOf<C>(), filter)
780
832
781
- context(ColumnsSelectionDsl)
782
- public inline fun <reified C> String.colsOf(): ColumnSet<Any?> =
783
- colsOf(typeOf<C>()) */
833
+ */
0 commit comments