Programming language cheatsheet
Table of Contents
- 1. Cheatsheet: Array
- 2. Cheatsheet: Map, Hash, Object, Dict
- 3. Cheatsheet: Sets
- 4. Cheatsheet: Iteration
- 5. Cheatsheet: Reflection
- 6. Concurrency
- 7. Cheatsheet: Cross-language Glossary
- 8. Cheatsheet: Monads
- 9. bash's heredoc and herestring syntax
- 10. bash: slugify - creating a URL-friendly string
- 11. Related notes
I work with many programming languages, it is sometimes hard to remember the names basic stuff as they differ just a little bit across languages.
1. Cheatsheet: Array
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
What | JavaScript |
---|---|
Documentation | Array (MDN) |
Creating a new array from two arrays | const newArray = array1.concat(array2); |
Append an array to an existing one (destructively) | array1.push(...array2); |
Add an element to the end of an array (destructively) | .push() |
Remove the last element of an array (destructively) | .pop() |
Add an element to the beginning of an array (destructively) | .unshift() |
Remove the first element of an array (destructively) | .shift() |
Remove an item at index (destructively) | .splice(i, 1) |
2. Cheatsheet: Map, Hash, Object, Dict
This one is annoying because each language have different names for the abstract data type and then have even more names for a bunch of variants.
Name | Language(s) |
---|---|
map | js |
dict | python |
object | js |
hash | ruby |
hash table | lisp |
3. Cheatsheet: Sets
What | JavaScript | Python | Lisp | Ruby |
---|---|---|---|---|
Creating an empty set | new Set() |
Set() |
'() or (make-hash-table) |
4. Cheatsheet: Iteration
In javascript, the for ... of
syntax does not work for object, as
they are not iterable.
To get something you can iterate on, you can use one of these static methods:
Object.entries(...)
,Object.keys(...)
, orObject.values(...)
Example:
const obj = { a: 1, b: 2, c: 3 }; for(let [key, value] of Object.entries(obj)) ...
5. Cheatsheet: Reflection
5.1. Check if a variable is a string
const isString = x => typeof x === 'string' || x instanceof String;
isinstance(x, str)
(stringp x)
5.2. Get the object's class
x.constructor.name
x.__class__
x.__class__.__name__
(class-of x) (class-name (class-of x))
5.3. JavaScript specific
5.3.1. Check if something is a DOM element
const isElement = x => x.tagName || x.nodeName;
6. Concurrency
6.1. Cheatsheet: Run something with a delay
const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
(defun delay (seconds function) (bt:make-thread (lambda () (sleep seconds) (funcall function))))
7. Cheatsheet: Cross-language Glossary
N.B. Those terms are not always a perfect match.
Javascript | Python | Lisp | Ruby |
---|---|---|---|
default function parameters | default argument values | optional parameters | arguments with default expression are optional |
throw | raise | signal or error | raise |
destructuring assignment | destructuring-bind | pattern matching + pin operator | |
spread syntax | unpacking | apply | splat operator |
rest parameters | packing | rest parameters | |
call-next-method | argument forwarding | ||
structural matching | pattern matching | ||
rightward assignment | |||
destructuring method | |||
∄ | pin operator | ||
8. Cheatsheet: Monads
Monads allow ordered computation within functional programming
8.1. Interface
- unit / constructor / lift / pure
- bind / flatMap
8.2. Laws
- left identity
bind(unit(x), f) ≍ f(x)
- right identity
bind(m, unit(x)) ≍ m
- associativity
flatMap(flatMap(m, f), g) ≍ flatMap(m, x => flatMap(x))
8.3. Common types of monads
- maybe, option, nullable
- option, either
- exceptions
- list, array
- state
- io
- reader monad
- monadic parser [combinator]
- free monads
- AFAIU, they just build up stuff, they don't do
8.4. See also
- Arrows are a generalization of monads
- arrows provide a referentially transparent way of expressing relationships between logical steps in a computation.
- Unlike monads, arrows don't limit steps to having one and only one input.
9. bash's heredoc and herestring syntax
cat <<EOF as is EOF
cat <<-EOF <<- removes leading tabs but not spaces EOF
10. bash: slugify - creating a URL-friendly string
slugify () { echo "$1" | iconv -t ascii//TRANSLIT | sed -r 's/[~\^]+//g; s/[^a-zA-Z0-9]+/-/g; s/^-+\|-+$//g' | tr A-Z a-z }