Skip to content

Conversation

@zetashift
Copy link
Contributor

@zetashift zetashift commented Mar 10, 2023

Anybody else know something more exciting to do than printing out the parsed lines of a CSV?

@TonioGela
Copy link
Member

Anybody else know something more exciting to do than printing out the parsed lines of a CSV?

Adding/removing a field and printing that out? Calculating and printing the max/min/mean for a specific column?

* [Circe] and http4s integration
* [Decline Effect]
* [Munit Cats Effect]
* [fs2-data-csv]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wasn't this the line you added in another PR? :D

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes that's me haha!
But somehow a second fs2-data mention got sneaked in:
image

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lol I've just realized it's a duplicate

@zetashift
Copy link
Contributor Author

Anybody else know something more exciting to do than printing out the parsed lines of a CSV?

Adding/removing a field and printing that out? Calculating and printing the max/min/mean for a specific column?

Well this is awkward, I don't know how to do aggregation on columns, I want to print out the mean of the ages column using fs2. Do I use a fold? :P

@TonioGela
Copy link
Member

Anybody else know something more exciting to do than printing out the parsed lines of a CSV?

Adding/removing a field and printing that out? Calculating and printing the max/min/mean for a specific column?

Well this is awkward, I don't know how to do aggregation on columns, I want to print out the mean of the ages column using fs2. Do I use a fold? :P

I'll say something like

val (a,b) = listOfNumbers.foldLeft((0,0)){ case ((s, n), x) => (s + x, n + 1) }
a/b

Comment on lines +238 to +253
val meanIO =
input
.foldMap(p => (p.age.getOrElse(0), 1))
.compile
.lastOrError
.map((sum, count) => sum / count)

input
.evalTap(p =>
IO.println(
s"${p.firstName} is taking flight: ${p.flightNumber} to ${p.destination}"
)
)
.compile
.drain >> meanIO.flatMap(mean =>
IO.println(s"The mean age of the passengers is $mean")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there is any way to do this in one run I'd love to know!
The only thing I could think of is chucking a IO.println and flatmapping in the foldMap

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am a bit late on that, but can't you put the evalTap right before the foldMap, and then compile and run

.flatMap { (sum, count) =>
  IO.println(s"The mean age of the passengers is ${sum / count}")
}

?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@satabin feel free to propose a neater implementation. Now it should be easier as toolkit contains fs2-data-csv too :D

@TonioGela TonioGela merged commit cad6ea5 into typelevel:main Mar 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants