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

Add JavaScript cheatsheet #274

Draft
wants to merge 24 commits into
base: main
Choose a base branch
from

Conversation

nino
Copy link
Contributor

@nino nino commented Sep 17, 2023

#414

I started adapting the Elixir cheatsheet for JavaScript/TypeScript. Still far from perfect, but it might be ready for a review 🤞

@nino nino marked this pull request as ready for review April 27, 2024 20:36
Copy link
Member

@lpil lpil left a 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; }
Copy link
Member

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

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

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

Copy link

@josealonso josealonso Sep 24, 2024

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here too

}
```

### Referencing functions
Copy link
Member

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- 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.
Copy link
Member

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

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

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

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

@lpil lpil marked this pull request as draft July 25, 2024 10:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants