typelevel / toolkit

Quickstart your next app with the Typelevel Toolkit!
https://typelevel.org/toolkit
Apache License 2.0
92 stars 9 forks source link

Some fun, simple, examples for common scripts and stuff! #5

Open armanbilge opened 1 year ago

armanbilge commented 1 year ago

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 😝

SethTisue commented 1 year ago

β€œHands On Scala” (Haoyi's book) is a good source to raid for ideas on this, IMO

TonioGela commented 1 year ago

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 πŸ˜‡

Koroeskohr commented 1 year ago

had a quick go at the virtuslab example for the fun of it: https://gist.github.com/Koroeskohr/c62025c48fc629658e92aad82786cd39

TonioGela commented 1 year ago

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)
armanbilge commented 1 year ago

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

TonioGela commented 1 year ago

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

Koroeskohr commented 1 year ago

@TonioGela i copied the virtuslab one-for-one that doesn't work either, I can polish it so that it's runnable, probably better

Koroeskohr commented 1 year ago

@TonioGela updated it

TonioGela commented 1 year ago

Thanks @Koroeskohr, I'll add it to the site once published :)

zetashift commented 1 year ago

@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.

lenguyenthanh commented 1 year ago

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.

TonioGela commented 1 year ago

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? :)

lenguyenthanh commented 1 year ago

@TonioGela thanks for your feedback and suggestion. I will open a pull request this week (with Scala 2 version ;) of course).

Tarbetu commented 1 year ago

@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. :(

SethTisue commented 1 year ago

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

TonioGela commented 1 year ago

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 πŸ€”

lenguyenthanh commented 1 year ago

I use scala-cli for testing (with cats-effect) now, but I use weaver not munit-effect πŸ˜‚

zetashift commented 1 year ago

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.

TonioGela commented 1 year ago

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.

TonioGela commented 1 year ago

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!

zetashift commented 8 months ago

@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? 
lenguyenthanh commented 7 months ago

I have few lichess related examples, one is simpler than the other:

But I'm not sure we want to flood our examples with lichess πŸ˜‚