GithubHelp home page GithubHelp logo

Comments (4)

nrinaudo avatar nrinaudo commented on June 17, 2024

I'll see about adding some documentation for that (it might take a while though, I'm in the middle of writing documentation for kantan.regex and I can only do that for so long :) ) but in the meantime, to answer your question: Result is a monad, and all kantan.csv result types are type aliases for Result. DecodeResult, ReadResult... are all monads. They have, in fact, monad instances for scalaz and cats. So, yes, they can be used in for comprehensions.

This is, for example, how DecodeResult.sequence is implemented.

That being said, using for-comprehensions isn't something that one should do for its own sake. I assume that you have a use case in mind? If so, could you explain that use case and I'll see whether it's supported and, if so, show you some code that solves your issue.

from kantan.csv.

schrepfler avatar schrepfler commented on June 17, 2024

I was thinking some use case where I could go over the lines as some case class made out of strings, potentially do some transformations and then constructing a map with the yielded values.

from kantan.csv.

nrinaudo avatar nrinaudo commented on June 17, 2024

I think I see what you mean, but for-comprehensions are not really the right tool for that - you don't know how many lines there are in your CSV input, so you can't really write one entry in the for-comprehension per line.

First, let's get you an instance of CsvReader into a case class of strings (a Tuple2[String, String]) and then transform these values (by swapping fields):

import kantan.csv.ops._

// This is of type CsvReader[ReadResult[(String, String)]]
val rows = file.asCsvReader[(String, String)](',', false).mapResult(_.swap)

I'm assuming you care about safety, otherwise you'd just get an unsafe CsvReader and your issue would become trivial. So, if you want safety, you probably want the end result of your code to be of type ReadResult[Map[String, String]], where, in my example, the map key corresponds to the first field in the tuple and the map value to the second field. So, first, let's turn our CsvReader[ReadResult[(String, String)]] into a ReadResult[List[(String, String)]] with Result.sequence:

import kantan.codecs._

// This is of type ReadResult[List[(String, String)]]
val decodedRows = Result.sequence(rows.toList)

Now that we have that, we only need to turn our inner List[(String, String)] into a Map[String, String], which turns out to be very simple:

// This is of type ReadResult[Map[String, String]]
decodeRows.map(Map.apply)

You could also do the whole thing in a streaming fashion - load one row, put it into a map, load the next row... but that might be overkill: you intend to end up with your whole CSV in memory anyway.

from kantan.csv.

schrepfler avatar schrepfler commented on June 17, 2024

Thanks for the well thought out reason. Documentation quality I'd say! 👍

from kantan.csv.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.