-
-
Notifications
You must be signed in to change notification settings - Fork 186
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
Add JavaScript cheatsheet #274
base: main
Are you sure you want to change the base?
Conversation
b1e3f8a
to
a393b13
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you! Sorry for taking so long, you've been at the end of the inbox for far too long and I'm only just getting through all the notifications
* @param {string} str String passed to quux | ||
* @returns {string} An unprocessed string | ||
*/ | ||
function quux(string) { return str; } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you remove all the foobar references please
|
||
#### Gleam | ||
|
||
Gleam's functions are declared using a syntax similar to Rust. Gleam's anonymous |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove the Rust reference please
} | ||
|
||
let mul = fn(x, y) { x * y } | ||
mul(1, 2) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code implies you can have top level expressions as if Gleam were a scripting language, but this is not the case
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Although it's on the gleam tour already, I think it's worth repeating there is no explicit return, because many JS developers might not know other functional language.
|
||
let add2 = fn(x) { add(x, 2) } | ||
// is equivalent to: | ||
let add2 = add(_, 2) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here too
} | ||
``` | ||
|
||
### Referencing functions |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can remove this section as JS programmers wouldn't expect anything else
### Notes on operators | ||
|
||
- JavaScript operators are short-circuiting as in Gleam. | ||
- Gleam's `/` operator always returns an integer. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Gleam's `/` operator always returns an integer. | |
- Gleam's `/` operator always returns an int. |
### Tuples | ||
|
||
Tuples are very useful in Gleam as they're the only collection data type that | ||
allows mixed types in the collection. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Custom types permit this too
|
||
#### Gleam | ||
|
||
Gleam has a "cons" operator that works for lists destructuring and pattern |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't have a cons operator, we have a list-prepend syntax
import gleam/dict | ||
|
||
dict.from_list([#("key1", "value1"), #("key2", "value2")]) | ||
dict.from_list([#("key1", "value1"), #("key2", 2)]) // Type error! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like scripting
"hello, world" | ||
|> string.uppercase | ||
|> string.repeat(2) // defaults to piping into the first argument | ||
|> string.split(_, ",") // you can use _ to specify the argument to pipe into |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be good to pipe into the second position as the first is the default
#414
I started adapting the Elixir cheatsheet for JavaScript/TypeScript. Still far from perfect, but it might be ready for a review 🤞