Programming Search⌘K

Collection operations

The daily operations over collections: sort, search, filter, map, reduce, group, zip, flatten and the combinatorial tasks.

Concepts

  • Comparator

    A comparator is a function that gets two values and tells which one comes first. A key function gets one value and returns the value to compare. Sorts, sorted maps and heaps use them.

    core
  • Comprehension

    A comprehension is an expression that builds a collection from a source, an optional filter and an expression for each item. It does the work of a loop that appends to a new list.

    core
  • In-place and copying operations

    An in-place operation changes the collection that it gets. A copying operation leaves it alone and returns a new collection. The name and the return value tell which one a function is.

    core
  • Stable sort

    A stable sort keeps items with equal keys in the order that they had before the sort. Stability lets a program sort by several keys in several passes, and it makes the result predictable.

    core

Tasks

  • Count the items of each kind

    Count how many times each distinct item is in a list of integers, and give the counts in ascending order of the item.

    10 languagesintro
  • Filter a list

    Keep the items of a list that pass a test, in their input order, and do not remove items from a list while a loop goes over it.

    10 languagesintro
  • Find the largest element

    Find the largest item of a non-empty list of integers, and know what each language does for an empty list.

    10 languagesintro
  • Flatten a nested list

    Join a list of lists of integers into one list, one level deep, with the items in order.

    10 languagesintro
  • Group items by a key

    Group a list of words by their length, with the groups in ascending order of the length and the words of each group in input order.

    10 languagesintro
  • Iterate in chunks

    Split a list of integers into consecutive chunks of a fixed size, where the last chunk can be shorter.

    10 languagesintro
  • Map over a list

    Make a new list with the result of a function for each item of a list, in the same order, and know when the map runs.

    10 languagesintro
  • Partition a list

    Split a list into the items that pass a test and the items that do not, here the odd and the even integers, and keep the order of the items in each part.

    10 languagesintro
  • Reduce a list to a value

    Combine the items of a list into one value with an accumulator, here the sum of a list of integers, and give an initial value for the empty list.

    10 languagesintro
  • Rotate a list

    Move the first k items of a list to its end, keep the order of the other items, and make a k that is larger than the list work.

    10 languagesintro
  • Sort a list

    Sort a list of integers in ascending numeric order, and do not compare the numbers as text.

    10 languagesintro
  • Sort with a custom key

    Sort a list of words by a key of two parts, the length and then the word, and return a new list.

    10 languagesintro
  • Zip two lists

    Pair the items of two lists of integers at the same index, and stop at the end of the shorter list.

    10 languagesintro
  • Compute the Cartesian product

    Make each tuple that takes one item from each of several lists of integers, in the order of nested loops.

    10 languagescore
  • Compute the combinations of a list

    Make each choice of k items of a list of integers, with the items in list order, in the lexicographic order of the positions.

    10 languagescore
  • Compute the permutations of a list

    Make each order of the items of a list of integers, in the lexicographic order of the positions.

    10 languagescore
  • Remove duplicates from a list

    Remove the repeated items from a list and keep the first copy of each item in its input order, and remove records that have the same key.

    10 languagescore
  • Search a sorted list

    Find the first index of a value in a sorted list of integers with a binary search, or give -1 when the value is not in the list.

    10 languagescore
  • Sort a list of records

    Sort a list of records by one field, then by a second field in the opposite direction, with a stable sort so that records with equal keys keep their input order.

    10 languagescore

Patterns

  • Collection pipeline

    Write a computation over a collection as a sequence of operations such as filter, map, sort and reduce. Each operation takes a collection from the step before and gives a collection to the next step.

    core

Techniques

  • Decorate, sort, undecorate

    Compute the sort key of each item once, sort the pairs of key and item, and then remove the keys. The sort then does not compute a slow key again at each comparison.

    advanced