Programming language cheatsheet

Table of Contents

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(...), or
  • Object.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. Cheatcheet: 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 paramteters packing rest parameters  
    call-next-method argument forwarding
  structural matching   pattern matching
      rightward assignment
      destructuring method
    pin operator
       

8. bash's heredoc and herestring syntax

cat <<EOF
  as is
EOF
cat <<-EOF
                <<- removes leading tabs
        but not
    spaces
EOF

9. 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
}

10. Related notes

Created: 2024-04-06 Sat 00:49