toolkit
toolkit copied to clipboard
Some fun, simple, examples for common scripts and stuff!
Here's the one posed by Virtus Lab:
Imagine you have a common business problem to solve. You need to write a program that fetches text from a web service and then creates a file in the local filesystem with content and path based on the downloaded information.
Scala Toolkit is getting a bunch of tutorials / examples, we can cheat by copying those and then slapping on a parTraverse
π
- https://github.com/scalacenter/docs.scala-lang/issues/8
βHands On Scalaβ (Haoyi's book) is a good source to raid for ideas on this, IMO
Is this gist of mine a good idea?
I felt the urge of being able to do
$ echo -e "foo\nbar" | mkString --prefix '{' --suffix '}' --delimiter "~"
{foo~bar}
with huge files, so I leveraged fs2 and ScalaNative to overkill the task π
had a quick go at the virtuslab example for the fun of it: https://gist.github.com/Koroeskohr/c62025c48fc629658e92aad82786cd39
had a quick go at the virtuslab example for the fun of it: https://gist.github.com/Koroeskohr/c62025c48fc629658e92aad82786cd39
Thanks @Koroeskohr! I'll create an examples folder and add it.
I have some troubles running it though, I'm getting this π running it with "3.2.2". Is it just me? π€
scala.MatchError: [Ljava.lang.String;@32fe9d0a (of class [Ljava.lang.String;)
at Hello$.run$$anonfun$1$$anonfun$2(c62025c48fc629658e92aad82786cd39-main/virtuslab-example.scala:16)
at >>$extension @ org.typelevel.keypool.KeyPool$Builder.keepRunning$1(KeyPool.scala:370)
at run$ @ Hello$.run(c62025c48fc629658e92aad82786cd39-main/virtuslab-example.scala:10)
at clear @ org.http4s.client.middleware.Retry$.retryLoop$1(Retry.scala:90)
at clear @ org.http4s.client.middleware.Retry$.retryLoop$1(Retry.scala:90)
at clear @ org.http4s.client.middleware.Retry$.retryLoop$1(Retry.scala:90)
at >>$extension @ org.typelevel.keypool.KeyPool$Builder.keepRunning$1(KeyPool.scala:370)
at run$ @ Hello$.run(c62025c48fc629658e92aad82786cd39-main/virtuslab-example.scala:10)
at clear @ org.http4s.client.middleware.Retry$.retryLoop$1(Retry.scala:90)
at clear @ org.http4s.client.middleware.Retry$.retryLoop$1(Retry.scala:90)
at flatMap @ org.typelevel.keypool.KeyPool$.put(KeyPool.scala:277)
at void @ org.typelevel.keypool.KeyPool$.reap(KeyPool.scala:187)
at map @ org.typelevel.keypool.KeyPool$.take$$anonfun$2$$anonfun$2$$anonfun$1$$anonfun$3$$anonfun$1(KeyPool.scala:310)
at get @ org.typelevel.keypool.KeyPool$.take$$anonfun$2$$anonfun$2$$anonfun$1$$anonfun$3(KeyPool.scala:305)
I'll create an examples folder and add it.
Alternatively, let's consider setting up a website with mdoc-checked examples. It will be easier to browse maybe? Edit: and also enables us to add exposition.
https://typelevel.org/sbt-typelevel/site.html
I'll create an examples folder and add it.
Alternatively, let's consider setting up a website with mdoc-checked examples. It will be easier to browse maybe? Edit: and also enables us to add exposition.
https://typelevel.org/sbt-typelevel/site.html
Yep, I was just thinking that having a site might be ideal
@TonioGela i copied the virtuslab one-for-one that doesn't work either, I can polish it so that it's runnable, probably better
@TonioGela updated it
Thanks @Koroeskohr, I'll add it to the site once published :)
@TonioGela continueing here about the Pokemon example:
Well, how about a stuffed example that:
accepts from command line a pokemon kind writes a csv with all results (restricted to a few properties)
Will it be too long? OFC I don't know if that search-ish API exists [EDIT] there's https://pokeapi.co/docs/v2#types :D
I think this is good! I should probably have it's separate page because it'll be a "big" example, but honestly it showcases some cool stuff.
is this one good enough as an example: https://github.com/lichess-org/scalachess/pull/352#issuecomment-1398911858?
This was used to convert a text file in one format to another format. We use the result for testing in scalachess.
is this one good enough as an example: lichess-org/scalachess#352 (comment)?
This was used to convert a text file in one format to another format. We use the result for testing in scalachess.
It's short and pleasant, very task focused, and I like it.
I'll maybe replace the function raw2Perft
with something like this π that is immutable (needs some checking since I wrote it w/o testing it), but that's just a suggestion
def raw2Perft(id: Long, raw: String): String =
val list = raw.split(";").zipWithIndex.map {
case (epd, 0) => s"epd ${epd}"
case (s, i) => s"perft $i ${s.split(" ")(1)}"
}
list.mkString(s"id $id\n", "\n", "")
If you open a PR to add the example may I ask you to add the Scala 2 version too? :)
@TonioGela thanks for your feedback and suggestion. I will open a pull request this week (with Scala 2 version ;) of course).
@TonioGela continueing here about the Pokemon example:
Well, how about a stuffed example that:
accepts from command line a pokemon kind writes a csv with all results (restricted to a few properties)
Will it be too long? OFC I don't know if that search-ish API exists [EDIT] there's https://pokeapi.co/docs/v2#types :D
I think this is good! I should probably have it's separate page because it'll be a "big" example, but honestly it showcases some cool stuff.
Sounds fun! I can do it, it sounds good for practising some Scala.
Edit: Oh, this API is a bit complex to process. :(
you might enjoy or be inspired by looking at what we're doing at https://github.com/scalacenter/docs.scala-lang/pull/7 βΒ we're still making adjustments to the texts of the sections, but the table of contents isn't likely to change much further before initial publication
you might enjoy or be inspired by looking at what we're doing at scalacenter/docs.scala-lang#7 β we're still making adjustments to the texts of the sections, but the table of contents isn't likely to change much further before initial publication
The most interesting part is that it advertises tests too, and atm we don't showcase any example that contains tests. It may be worth investing time in showing people how fun is to write them with munit-effect π€
I use scala-cli for testing (with cats-effect) now, but I use weaver not munit-effect π
I'm 100% down to make an example about unit testing with munit-effect (tho I have more experience with weaver), but I've thought about it for a while now and can't figure out any compelling problem/example that shows it off lol.
Maybe something like an UserList
that has some methods. And the test suite will test those functions?
EDIT: might also be a cool way to show off Validated
from cats.
I'm 100% down to make an example about unit testing with munit-effect (tho I have more experience with weaver), but I've thought about it for a while now and can't figure out any compelling problem/example that shows it off lol.
The famous Pokemon example that I suggested to you some time ago had a bonus point section that asked to reduce the usage of the external APIs caching them. A good example might be writing something that relies on an external API memoizing its results and checking that the effective API gets called just once. Probably it's a bit convoluted though.
The Toolkit section in the Scala documentation has finally landed here. It will be great to port it using the typelevel toolkit creating a whole new section on the site!
@TonioGela continueing the JSON derived example discussion here, how about:
- Write `Author`, `Post` and `Comment` case classes, with automatic derivation
- Create a stream of authors and...`print` them out?
- Maybe chuck in an unit test in there?
I have few lichess related examples, one is simpler than the other:
-
Streaming chess games: stream games in
ndjson
format, parse, handle some edge cases. -
Download chess puzzles: stream a zip file, unzip on the fly, parse csv data and do some filter.
But I'm not sure we want to flood our examples with lichess π