roc/www/content/index.md
Richard Feldman 1993cd336b
Add @pmarreck to sponsors
Thank you so much, Peter! 💜

Signed-off-by: Richard Feldman <oss@rtfeldman.com>
2024-09-12 09:11:04 -04:00

28 KiB
Raw Blame History

Roc

A fast, friendly, functional language.

credits = List.map songs \song ->
    "Performed by $(song.artist)"

Try Roc

You can try Roc using this read-eval-print loop (REPL), which is running in your browser in WebAssembly.

Shift-Enter adds a newline.

Try entering 0.1 + 0.2

Enter an expression to evaluate, or a definition (like x = 1) to use later.
»

Examples

Roc is a young language. It doesn't even have a numbered release yet, just nightly builds!

However, it can already be used for several things if you're up for being an early adopter—
with all the bugs and missing features which come with that territory.

Here are some examples of how it can be used today.

Command-Line Interfaces

main =
    Stdout.line! "Hello!"

You can use Roc to create scripts and command-line interfaces (CLIs). The compiler produces binary executables, so Roc programs can run on devices that don't have Roc itself installed.

As an example, the HTML for this website is generated using a simple Roc script. You can see the code for it in the main Roc code repository.

If youre looking for a starting point for building a command-line program in Roc, basic-cli is a popular platform to check out.

Web Servers

handleReq = \request ->
    Task.ok { body:  }

You can also build web servers in Roc. basic-webserver is a platform with a simple interface: you write a function which takes a Request, does some I/O, and returns a Response.

Behind the scenes, it uses Rust's high-performance hyper and tokio libraries to execute your Roc function on incoming requests.

For database access, roc-pg lets you access a PostgreSQL database—with your Roc types checked against the types in your database's schema.

Embedding

fn = require("foo.roc");
log(`Roc says ${fn()}`);

You can call Roc functions from other languages. There are several basic examples of how to call Roc functions from Python, Node.js, Swift, WebAssembly, and JVM languages.

Any language that supports C interop can call Roc functions, using similar techniques to the ones found in these examples.

Most of those are minimal proofs of concept, but roc-esbuild is a work in progress that's used at Vendr to call Roc functions from Node.js.

Other Examples

You can find more use cases and examples on the examples page!

Code Sample with Explanations

Here's a code sample that shows a few different aspects of Roc:

  • File I/O and HTTP requests
  • Pattern matching for error handling
  • JSON deserialization via type inference
  • Common syntax sugar: pipelines, the ! operator, and string interpolation

The tutorial introduces these gradually and in more depth, but this gives a brief overview.

Sponsors

We are very grateful for our corporate sponsors! They are: Tweede golf, ohne-makler, and Decem.

If you would like your organization to become an official sponsor of Roc's development, please DM Richard Feldman on Zulip!

We'd also like to express our gratitude to our generous individual sponsors! A special thanks to those sponsoring $25/month or more:

Thank you all for your contributions! Roc would not be what it is without your generosity. 💜

We are currently trying to raise $4,000 USD/month in donations to fund one longtime Roc contributor to continue his work on Roc full-time. We are a small group trying to do big things, and every donation helps! You can donate using:

All donations go through the Roc Programming Language Foundation, a registered US 501(c)(3) nonprofit organization, which means these donations are tax-exempt in the US.