Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
armanbilge opened this issue Feb 24, 2023 · 22 comments
Open

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

armanbilge opened this issue Feb 24, 2023 · 22 comments
Labels
documentation Improvements or additions to documentation good first issue Good for newcomers help wanted Extra attention is needed

Comments

@armanbilge
Copy link
Member

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 😝

@armanbilge armanbilge added documentation Improvements or additions to documentation good first issue Good for newcomers help wanted Extra attention is needed labels Feb 24, 2023
@SethTisue
Copy link
Member

SethTisue commented Feb 24, 2023

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

@TonioGela
Copy link
Member

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
Copy link

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

@TonioGela
Copy link
Member

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
Copy link
Member Author

armanbilge commented Feb 25, 2023

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
Copy link
Member

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
Copy link

@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
Copy link

@TonioGela updated it

@TonioGela
Copy link
Member

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

@TonioGela TonioGela mentioned this issue Mar 3, 2023
@zetashift
Copy link
Contributor

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

@TonioGela TonioGela pinned this issue Mar 13, 2023
@lenguyenthanh
Copy link
Member

lenguyenthanh commented Mar 13, 2023

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.

@TonioGela
Copy link
Member

TonioGela commented Mar 14, 2023

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
Copy link
Member

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

@Tarbetu
Copy link

Tarbetu commented Mar 30, 2023

@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
Copy link
Member

SethTisue commented Mar 30, 2023

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

@TonioGela
Copy link
Member

TonioGela commented Mar 30, 2023

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
Copy link
Member

I use scala-cli for testing (with cats-effect) now, but I use weaver not munit-effect 😂

@zetashift
Copy link
Contributor

zetashift commented Apr 6, 2023

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
Copy link
Member

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
Copy link
Member

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
Copy link
Contributor

@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
Copy link
Member

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 😂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation good first issue Good for newcomers help wanted Extra attention is needed
Projects
Development

No branches or pull requests

7 participants