Skip to content

Instantly share code, notes, and snippets.

@modernserf
Last active December 27, 2020 21:54
Show Gist options
  • Save modernserf/90100a0fdfeff76d775a6f85e2180cf2 to your computer and use it in GitHub Desktop.
Save modernserf/90100a0fdfeff76d775a6f85e2180cf2 to your computer and use it in GitHub Desktop.
Syntax bestiary

Simple value literals

Numbers

syntax examples languages
decimal literals -123.45 (most languages)
scientific notation 1.23e-2
prefix hexadecimal 0xDEADBEEF
prefix binary 0b1110
prefix octal 0o777
unix-style octal 0777 C, JS (deprecated)
radix prefix 16rDEADBEEF Smalltalk

Strings

syntax examples languages
single quote 'a string' JS, Python, Ruby, Smalltalk
double quote "a string" JS, Python, Ruby*, Swift, Rust, Elixir*, Clojure
backtick `a string` JS*
sigils %q(foo bar baz) Ruby
... %Q(foo bar baz) Ruby*
... ~s(foo bar baz) Elixir*

* - Allows interpolation

Interpolation

syntax examples languages
sigil only $value PHP, Dart
escaped expr ${value} JS, PHP, Dart
... #{value} Ruby
... $(value) Julia
... \(value) Swift

Multiline strings

syntax examples languages
backticks
`
 line 1
 line 2
 `
JS*
EOL backslash
"line 1 \
line 2"
JS
triple quotes
"""
line 1
line 2
"""
Python*, Elixir*
heredoc
<<HEREDOC
line 1
line 2
HEREDOC
Ruby*
implicit concatenation
"line 1"
"line 2"
C, Python

* - Preserves newlines

Chars

syntax examples languages
single quote 'A' Rust
question mark ?A Elixir
backslash \A Clojure

Regular expressions

syntax examples languages
Perl-style /foo.+b[ar]$/ JS, Ruby
tagged string r"foo.+b[ar]$" Python, Julia
... #"foo.+b[ar]$" Clojure
sigil ~r/foo.+b[ar]$/ Elixir

Interned strings (symbols, keywords, etc)

syntax examples languages
colon :foo Ruby, Clojure, Elixir, Julia
single quote 'foo Scheme, Clojure*, Scala

Other stringlike values

syntax examples languages notes
char list 'a list of chars' Elixir linked list of chars
used for backwards compatibility with older Erlang code

Composite value literals

homogeneous heterogeneous
by order List Tuple
by name Dictionary Record

Tuples

syntax examples languages
parens (foo, bar) Elm, Python*, Julia, Rust, Swift
curly {foo, bar} Elixir
multiple return,
but no tuple value type
return (foo, bar) Go*
idiomatically uses lists [foo, bar] JS, Clojure, PHP, Ruby*

* - parens/brackets optional

Lists

alt. "vectors", "arrays"

syntax examples languages
brackets [1, 2, 3] JS, Ruby, Python, Elixir, Elm, Clojure*, PHP†
braces {1, 2, 3} Lua†, Java, C
tagged braces []int{1, 2, 3} Go
macro vec![1, 2, 3] Rust

* - commas optional † - syntactic sugar for dictionary with integer keys

access by index

syntax examples languages
subscript x[1] (most languages)

slices

syntax examples languages
colon range x[start:end] Python, Go
subscript with range object x[start..end] Ruby

cons-truction

syntax examples languages
cons operator first :: second :: rest Elm
wrapped cons [first, second | rest] Elixir
spread [first, second, ...rest] JS
... [first, second, *rest] Ruby, Python

NOTE: 'spread' can go anywhere in list; 'wrapped cons' can only go in last position

Other listlike literals

syntax examples languages notes
quoted sexpr '(foo bar) Clojure used for representing syntax, not (normal) data
set #{ 1 2 3 } Clojure
set with type hint var x : Set = [1,2,3] Swift
array (not linked list) [| 1, 2, 3 |] Reason, OCaml contiguous memory, O(1) lookup
byte array <<1, 2, 3>> Elixir contiguous memory, O(1) lookup, bytes only
matrix [1 2; 3 4] Julia multidimensional array

Records

alt. structs

syntax examples languages
tagged Point{ x: 123, y: 456 } Rust, Go
positional point p = { 123, 456 } C
designated initializer { .x = 123, .y = 456 } C
extensible records { x = 123, y = 456 } Elm
constructor function Point(x: 123, y: 456) Swift, Ruby
idiomatically uses dicts JS, Python, Clojure, Elixir

static field access

syntax examples languages
dots point.x C, Go, Rust, Swift, JS, Ruby, Elm, Elixir
dict lookup only Python, Clojure

Dictionaries

alt. map, hash, associative array, table

syntax examples languages
braces {"x": 123, "y": 456} JS, Python
braces + bare {x: 123, y: 456} JS, Ruby
braces + equals {x=123, y=456} Lua
braces + arrow {:x => 123, :y => 456} Ruby
... %{:x => 123, :y => 456} Elixir
braces + alternate kv {:x 123 :y 456} Clojure
square + arrow ["x" => 123, "y" => 456] PHP
square + colon ["x": 123, "y": 456] Swift

access by key

syntax examples languages
subscript x[key] JS, Python, Ruby, Lua, Elixir, PHP, Swift

spread

syntax examples languages
same as list { ...rest } JS
double-splat { **rest } Ruby, Python

Other key-value structures

Elixir keyword lists

[x: 123, y: 456] = [{:x, 123}, {:y, 456}] lookup is O(n)


Typed structures (e.g. classes, enums, variants)

Functions & Blocks

Operators

Patterns & assignment

Modules

Comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment