Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
Quafadas committed Jan 2, 2025
1 parent 913e8f4 commit a8dc2be
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
12 changes: 7 additions & 5 deletions scautable/src/csv.scala
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@ object CSV:

// type Result = ReplaceOneType[InputTuple, EmptyTuple, "col1", Boolean]

type ReplaceOneTypeAtName[N <: Tuple, StrConst <: String, T <: Tuple, Head <: Tuple, A] <: Tuple = (T, N) match
type ReplaceOneTypeAtName[N <: Tuple, StrConst <: String, T <: Tuple, A] <: Tuple = (T, N) match
case (EmptyTuple, _) => EmptyTuple
case (_, EmptyTuple) => EmptyTuple
case (nameHead *: nameTail, typeHead *: typeTail) =>
IsMatch[nameHead, StrConst] match
case true => A *: typeTail
case false =>
typeHead *: ReplaceOneTypeAtName[nameTail, StrConst, typeTail, Head, A]
typeHead *: ReplaceOneTypeAtName[nameTail, StrConst, typeTail, A]

// match
// case EmptyTuple => T *: A *: StrConst *: EmptyTuple
Expand Down Expand Up @@ -153,9 +153,11 @@ object CSV:
(fct(tup) *: tup.toTuple).withNames[Concat[S, K1]]
}

inline def forceColumnType[S <: String, A] = {
itr.map(_.asInstanceOf[NamedTuple[K1, ReplaceOneTypeAtName[K1, S, V1, A]]])
}


inline def mapColumn[S <: String, B, A](fct: B => A)(using ev: IsColumn[S, K1] =:= true, s: ValueOf[S])= {
inline def mapColumn[S <: String, B, A](fct: B => A)(using ev: IsColumn[S, K1] =:= true, s: ValueOf[S]): Iterator[NamedTuple[K1, ReplaceOneTypeAtName[K1, S, V1, A]]]= {
import scala.compiletime.ops.string.*
val headers = constValueTuple[K1].toList.map(_.toString())
type temp = "TEMP_COLUMN"
Expand All @@ -171,7 +173,7 @@ object CSV:
val tup = x.toTuple
val mapped = fct(tup(idx).asInstanceOf[B])
val (head, tail) = x.toTuple.splitAt(idx)
(head ++ mapped *: tail.tail).withNames[K1].asInstanceOf[NamedTuple[K1,ReplaceOneTypeAtName[K1, S, V1, EmptyTuple, A]]]
(head ++ mapped *: tail.tail).withNames[K1].asInstanceOf[NamedTuple[K1,ReplaceOneTypeAtName[K1, S, V1, A]]]
}
}

Expand Down
10 changes: 10 additions & 0 deletions scautable/test/jvm/src/testJvm.scala
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,16 @@ class CSVSuite extends munit.FunSuite:
assertEquals(out.last.col2Renamed, "6")
}

test("force column type") {
val csv: CsvIterator[("col1", "col2", "col3")] = CSV.absolutePath(Generated.resourceDir0 + "simple.csv")

val renamed: Iterator[(col1 : "col1", col2Renamed : String, col3 : "col3")]= csv.drop(1).renameColumn["col2", "col2Renamed"].forceColumnType["col2Renamed", String]
val out = renamed.toArray
assertEquals(out.head.col2Renamed, "2")
assertEquals(out.tail.head.col2Renamed, "4")
assertEquals(out.last.col2Renamed, "6")
}

test("map column") {
def csv = CSV.absolutePath(Generated.resourceDir0 + "simple.csv")
def csvDrop1 = csv.drop(1)
Expand Down

0 comments on commit a8dc2be

Please sign in to comment.