Polyglot CheatSheet - List/Vector/Array
Updated: 2020-10-11
Create: Empty
Hack
Hack Array:
$v = vec[]
Hack Collection:
$v = Vector{}
PHP
array()
Python
>>> [[0] * 4] * 4
[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
Create: Literal
Hack
$v = vec[1, 2, 3];
Java
String[] optypes = new String[]{"categorical", "continuous", "ordinal"};
Ruby
>> numbers = ['zero', 'one', 'two']
=> ["zero", "one", "two"]
Create: From Existing
Hack
$v = vec($container);
Get By Index
Ruby
>> numbers[1]
=> "one"
Basic Operations
Javascript
insert, front
array.unshift();
insert, back
array.push();
delete, back
array.pop();
delete, front
array.shift();
Add Elements
Hack
$v[] = 4;
Python
list.append()
Java
list.push()
Javascript
list.push();
Ruby
>> numbers.push('three', 'four')
=> ["zero", "one", "two", "three", "four"]
Remove Element
Javascript
Delete without changing indices of other elements(leaving a hole)
delete array[i];
Delete and shift other elements forward(modifying array in place:
var index = array.indexOf(5);
if (index > -1) {
array.splice(index, 1);
}
Delete by filtering
keys.filter((key) => key != "foo");
Ruby
>> numbers.drop(2)
=> ["two", "three", "four"]
Exists
Hack
Hack Array:
C\contains_key($v, 1)
or
C\contains($v, 3)
Python
Use in
:
>>> a = [1, 2, 3]
>>> 1 in a
True
>>> 4 in a
False
Javascript
Use includes
:
arr.includes(element);
Count Elements
Hack
C\count($v)
Type Signature
Hack
vec<TValue>
Type Test
Hack
is_vec($v)
Join As String
Python
join
is a method of string instead of a list, since any iterable can be joined.
>>> "|".join(["a", "b", "c"])
'a|b|c'
Scala
Use mkString
scala> List("a","b","c").mkString("|")
res1: String = a|b|c
Java 8+
String joined = String.join(",", Arrays.asList("a", "b", "c"));
or
String joined = Arrays.asList("a", "b", "c").stream().collect(Collectors.joining(","));
Manipulate before joining, e.g. to Uppercase
String joined = Arrays.asList("a", "b", "c")
.stream()
.map(String::toUpperCase)
.collect(Collectors.joining(","));
PHP
implode
Join Two Arrays
javascript
[1].concat([2])[(1, 2)];
IndexOf
var array = [2, 5, 9];
var index = array.indexOf(5);
Subarray
Scala
arr.drop(2)
Python
arr[2:]
Javascript
arr.slice();
Convert To Other Collections
Hack
vec to Vector
new Vector($v)
vec to Map
new Map($v)
vec to Set
new Set($v)
vec to array
Arrays::fromVec($v)
Deconstruct
Javascript
const [a, b, c] = str.split("-");
PHP
list($a, $b, $c) = Str\split(",", $str);
Max/Min
Java
Java 8+
int[] a = new int[]{1,2,3};
int min = Arrays.stream(a).min().getAsInt();
Javascript
Math.max
and Math.min
do not work on arrays, for example Math.max(1,2,3,4)
. To get the max/min of an array, use apply
Math.max.apply(Math, [1, 2, 3, 4]);
Math.min.apply(Math, [1, 2, 3, 4]);
Clone
Java
List<Integer> copy = new ArrayList<>(origin)
Javascript
const clone = originArray.slice(0);
Java
System.out.println(Arrays.toString(arr));
Flatten
Python
There's no flatten builtin, but can use list comprehension:
flat_list = [item for sublist in nestedlist for item in sublist]
which means:
for sublist in nestedlist:
for item in sublist:
flat_list.append(item)
zip
Python
>>> keys = ['a', 'b', 'c']
>>> values = ['1', '2', '3']
>>> list(zip(keys, values))
[('a', '1'), ('b', '2'), ('c', '3')]
Javascript
const a = [1, 2, 3]
const b = ['A', 'B', 'C']
a.map((v, i) => [v, b[i]]
// [[1, 'A'], [2, 'B'], [3, 'C']]