Skip to content

Commit 7b21815

Browse files
authored
Merge pull request #439 from zetashift/add-typelevel-toolkit-post
Add typelevel toolkit blog post
2 parents e011f1c + 39f3c53 commit 7b21815

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed

_data/authors.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,3 +413,6 @@ matthicks:
413413
full_name: "Matt Hicks"
414414
twitter: "darkfrog26"
415415
github: "darkfrog26"
416+
zetashift:
417+
full_name: "Rishad Sewnarain"
418+
github: "zetashift"
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
---
2+
layout: post
3+
title: Typelevel Toolkit
4+
category: technical
5+
6+
meta:
7+
nav: blog
8+
author: zetashift
9+
pygments: true
10+
---
11+
12+
Getting started in the wondrous world of functional programming using [Typelevel libraries](https://typelevel.org/projects) can be daunting. Before you can even write your first pure "Hello, World!" you'll need to install a Java runtime, editor tooling and build tools. Then you'll need to setup some project using [sbt](https://www.scala-sbt.org/) or [mill](https://github.com/com-lihaoyi/mill). As an added consequence, after all the setup, the idea of using these battle-tested libraries for small scripts will seem like a chore. This is where [Typelevel Toolkit](https://typelevel.org/toolkit/) comes in. It provides an easy start for beginning and experienced developers with Scala and functional programming.
13+
14+
# scala-cli to the rescue
15+
16+
[scala-cli](https://scala-cli.virtuslab.org/) is a command-line interface to quickly develop and experiment with Scala, it's even on track to [becoming the new scala command](https://docs.scala-lang.org/sips/scala-cli.html). The interface has a lot of advantages, but one of the most important ones is that it makes learning, developing and building Scala scripts and small applications friction-less and easy to use.
17+
18+
You can get `scala-cli` by following the installation instructions described here: [https://scala-cli.virtuslab.org/docs/overview#installation](https://scala-cli.virtuslab.org/docs/overview#installation). The great part here is that once you have `scala-cli` installed, it will take care of the rest: Java runtimes, editor tooling, compilation targets(including a native target!) and you can even use dependencies in your scripts!
19+
20+
## An example of setting up your script
21+
22+
First off, let's create a new directory that will contain our script(s).
23+
24+
```sh
25+
mkdir myscript && cd myscript
26+
```
27+
28+
Now we can use `scala-cli` to create all the files necessary for editor tooling to work:
29+
30+
```sh
31+
scala-cli setup-ide .
32+
```
33+
34+
Finally, let's create a Scala file and try to compile it.
35+
36+
```sh
37+
touch Main.scala
38+
scala-cli compile --watch Main.scala
39+
```
40+
41+
42+
The last command also includes a `--watch` flag, so we can hack on our script and `scala-cli` will try to compile the file on every save.
43+
This creates a very nice feedback loop!
44+
45+
# Putting the fun in functional
46+
47+
[Typelevel Toolkit](https://typelevel.org/toolkit/) uses `scala-cli` and Typelevel libraries to provide a runway for your next Scala script or command-line interface. With a single line, Typelevel Toolkit gives you:
48+
- [Cats Effect](https://typelevel.org/cats-effect/) a production proven pure asynchronous runtime.
49+
- [fs2](https://fs2.io) an amazing streaming I/O library.
50+
- [http4s-ember-client](https://http4s.org/v0.23/docs/client.html) for a full-fledged HTTP client.
51+
- [circe](https://circe.github.io/circe/) for dealing with JSON.
52+
- [MUnit](https://github.com/typelevel/munit-cats-effect) an unit testing library and an integration to easily unit test pure functional programs.
53+
- [fs2-data-csv](https://fs2-data.gnieh.org/documentation/csv/) for handling CSV files.
54+
- [decline](https://ben.kirw.in/decline/effect.html) a composable commandline parser and it's integration with Cats Effect.
55+
56+
Typelevel Toolkit shines with `scala-cli`, but it can also be used by sbt or mill if that is preferred.
57+
More concretely this means your next ad-hoc script won't be Bash or Python spaghetti, but Scala code that can be a joy to hack on as time goes on, without the boilerplate.
58+
59+
You can use the toolkit by using a
60+
[scala-cli directive](https://scala-cli.virtuslab.org/docs/guides/using-directives)
61+
62+
```scala
63+
//> using dep "org.typelevel::toolkit::0.0.4"
64+
```
65+
66+
This will pull in the `typelevel/toolkit` dependency and then you're just an import away from your first pure functional "Hello, World!":
67+
68+
```scala
69+
import cats.effect.*
70+
71+
object Hello extends IOApp.Simple {
72+
def run = IO.println("Hello, World!")
73+
}
74+
```
75+
76+
You can compile and run this program by using a single command: `scala-cli run Main.scala`.
77+
78+
79+
A "Hello, World!" is only the start, the goal here is to make functional programming friendly and practical. As such, Typelevel toolkit comes with [examples](https://typelevel.org/toolkit/examples.html) that introduces beginners on how one can use the included libraries to achieve common tasks.
80+
81+
82+
For the full list of libraries included in Typelevel Toolkit, please see the overview: [https://typelevel.org/toolkit/#overview](https://typelevel.org/toolkit/#overview). If you feel like anything is missing, [join the discussion](https://github.com/typelevel/toolkit/issues/1).
83+
84+
# We can have nice things
85+
86+
Typelevel libraries are production-proven, well tested, build upon rock solid semantics, and almost all have Scala 3 support.
87+
However their entry-point is higher than your usual scripting language. Pure functional programming has a reputation of being hard to learn, and Typelevel Toolkit is a way to play in that world, without learning an entire ecosystem first.
88+
89+
The libraries in the toolkit compliment each other and target common usecases, thus providing a coherent mini-ecosystem, that scales extremely well, thanks to Cats Effect. **With support for other runtimes besides the JVM**. This means that your scripts can run in a [JavaScript environment](https://scala-cli.virtuslab.org/docs/guides/scala-js), thanks to [scala-js](https://www.scala-js.org/). Or you can use [scala-native](https://github.com/scala-native/scala-native) to get a [native binary](https://scala-cli.virtuslab.org/docs/guides/scala-native), just like Rust and Go!
90+
91+
`scala-cli` again, makes things easy for us by having simple commands to compile to a certain target:
92+
93+
```sh
94+
# To compile to JavaScript
95+
scala-cli Main.scala --js
96+
97+
# To target native
98+
scala-cli Main.scala --native
99+
```
100+
101+
If you just want to explore Typelevel Toolkit, you can quickly open a REPL using the following command:
102+
103+
```sh
104+
scala-cli repl --dep "org.typelevel::toolkit::0.0.4"
105+
```
106+
107+
With `scala-cli`, there a few other cool things you get here:
108+
- [export current build into sbt or mill](https://scala-cli.virtuslab.org/docs/guides/sbt-mill)
109+
- [create and share gists](https://scala-cli.virtuslab.org/docs/cookbooks/gists)
110+
- [package your script using GraalVM](https://scala-cli.virtuslab.org/docs/cookbooks/native-images)
111+
112+
# Summary
113+
114+
`scala-cli` is great. It's easy to get started with and great to use. Typelevel Toolkit leverages its versatility and provides a "pure functional standard library" in a single directive. This will enable you to create and develop scripts fast with high refactorability, an awesome developer experience and lots of functions that compose well! All those benefits while remaining beginner-friendly.

0 commit comments

Comments
 (0)