Discussion
Loading...

Post

Log in
  • About
  • Code of conduct
  • Privacy
  • Users
  • Instances
  • About Bonfire
Michael Kohl 馃嚘馃嚬馃嚬馃嚟
Michael Kohl 馃嚘馃嚬馃嚬馃嚟
@citizen428@chaos.social  路  activity timestamp 6 days ago

Occasionally, people new to #gleam get confused by the `use` expression. In a recent discussion on Discord I made a color-coded graphic for one user, which helped in making them click. I figured, why not share it here too, in case it helps someone else.

https://tour.gleam.run/advanced-features/use/

Use - The Gleam Language Tour

An interactive introduction and reference to the Gleam programming language.
A screenshot of a color-coded desugaring of the following use expression:

use x <- some_fun(42) x * x

The desugared form is 

some_fun(42, fn(x) { x * x })

In both cases the x is light green (variable binding), 42 is light blue (argument), some_fun is yellow (function name) and the expression "x * x" is purple (callback body).

There's also a textual description:

How it works: The use expression takes everything after it and wraps it in an anonymous function fn(x) {... }, which is then passed as the final argument to the function on the right-hand side of <-. This eliminates callback nesting without adding language complexity!
A screenshot of a color-coded desugaring of the following use expression: use x <- some_fun(42) x * x The desugared form is some_fun(42, fn(x) { x * x }) In both cases the x is light green (variable binding), 42 is light blue (argument), some_fun is yellow (function name) and the expression "x * x" is purple (callback body). There's also a textual description: How it works: The use expression takes everything after it and wraps it in an anonymous function fn(x) {... }, which is then passed as the final argument to the function on the right-hand side of <-. This eliminates callback nesting without adding language complexity!
A screenshot of a color-coded desugaring of the following use expression: use x <- some_fun(42) x * x The desugared form is some_fun(42, fn(x) { x * x }) In both cases the x is light green (variable binding), 42 is light blue (argument), some_fun is yellow (function name) and the expression "x * x" is purple (callback body). There's also a textual description: How it works: The use expression takes everything after it and wraps it in an anonymous function fn(x) {... }, which is then passed as the final argument to the function on the right-hand side of <-. This eliminates callback nesting without adding language complexity!
  • Copy link
  • Flag this post
  • Block

Kakapo Social

Kakapo Social: About 路 Code of conduct 路 Privacy 路 Users 路 Instances
Bonfire social 路 1.0.1-beta.22 no JS en
Automatic federation enabled
Log in
  • Explore
  • About
  • Members
  • Code of Conduct