toolkit icon indicating copy to clipboard operation
toolkit copied to clipboard

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

Open armanbilge opened this issue 2 years ago β€’ 22 comments

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

armanbilge avatar Feb 24 '23 18:02 armanbilge

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

SethTisue avatar Feb 24 '23 18:02 SethTisue

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

TonioGela avatar Feb 24 '23 18:02 TonioGela

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

Koroeskohr avatar Feb 25 '23 08:02 Koroeskohr

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)

TonioGela avatar Feb 25 '23 09:02 TonioGela

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

armanbilge avatar Feb 25 '23 09:02 armanbilge

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 avatar Feb 25 '23 09:02 TonioGela

@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 avatar Feb 25 '23 09:02 Koroeskohr

@TonioGela updated it

Koroeskohr avatar Feb 25 '23 10:02 Koroeskohr

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

TonioGela avatar Feb 25 '23 17:02 TonioGela

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

zetashift avatar Mar 11 '23 17:03 zetashift

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.

lenguyenthanh avatar Mar 13 '23 20:03 lenguyenthanh

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 avatar Mar 14 '23 10:03 TonioGela

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

lenguyenthanh avatar Mar 14 '23 12:03 lenguyenthanh

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

Tarbetu avatar Mar 30 '23 09:03 Tarbetu

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

SethTisue avatar Mar 30 '23 09:03 SethTisue

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

TonioGela avatar Mar 30 '23 09:03 TonioGela

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

lenguyenthanh avatar Mar 30 '23 10:03 lenguyenthanh

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.

zetashift avatar Apr 06 '23 12:04 zetashift

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 avatar Apr 11 '23 07:04 TonioGela

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 avatar May 18 '23 16:05 TonioGela

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

zetashift avatar Nov 13 '23 21:11 zetashift

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

lenguyenthanh avatar Nov 25 '23 10:11 lenguyenthanh