\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{Xantier} \pdfinfo{ /Title (kotlin-collection-extensions.pdf) /Creator (Cheatography) /Author (Xantier) /Subject (Kotlin Collection Extensions Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{13C9D6} \definecolor{LightBackground}{HTML}{F0FBFC} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{Kotlin Collection Extensions Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{Xantier} via \textcolor{DarkBackground}{\uline{cheatography.com/38756/cs/12075/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}Xantier \\ \uline{cheatography.com/xantier} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Published 14th June, 2017.\\ Updated 14th June, 2017.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{tabularx}{17.67cm}{x{4.2175 cm} x{6.2419 cm} x{6.4106 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{17.67cm}}{\bf\textcolor{white}{Creating Collections}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{3}{x{17.67cm}}{\{\{ac\}\}{\bf{Arrays}}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} Simple Array & `val intArray: Array\textless{}Int\textgreater{} = arrayOf(1, 2, 3)` & \tn % Row Count 5 (+ 4) % Row 2 \SetRowColor{LightBackground} Copy of Array & `val copyOfArray: Array\textless{}Int\textgreater{} = \seqsplit{intArray.copyOf()`} & \tn % Row Count 9 (+ 4) % Row 3 \SetRowColor{white} Partial copy of Array & `val \seqsplit{partialCopyOfArray:} Array\textless{}Int\textgreater{} = \seqsplit{intArray.copyOfRange(0}, 2)` & \tn % Row Count 14 (+ 5) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{3}{x{17.67cm}}{\{\{ac\}\}{\bf{Lists}}} \tn % Row Count 15 (+ 1) % Row 5 \SetRowColor{white} Simple List & `val intList: List\textless{}Int\textgreater{} = listOf(1, 2, 3)` & Or `arrayListOf(1,2,3)` \tn % Row Count 18 (+ 3) % Row 6 \SetRowColor{LightBackground} Empty List & `val emptyList: List\textless{}Int\textgreater{} = emptyList()` & Or `listOf()` \tn % Row Count 21 (+ 3) % Row 7 \SetRowColor{white} List with no null elements & `val \seqsplit{listWithNonNullElements:} List\textless{}Int\textgreater{} = \seqsplit{listOfNotNull(1}, null, 3)` & same as `List(1,3)` \tn % Row Count 26 (+ 5) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{3}{x{17.67cm}}{\{\{ac\}\}{\bf{Sets}}} \tn % Row Count 27 (+ 1) % Row 9 \SetRowColor{white} Simple Set & `val aSet: Set\textless{}Int\textgreater{} = setOf(1)` & Or `hashSetOf(1)` / \seqsplit{`linkedSerOf(1)`} \tn % Row Count 30 (+ 3) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{4.2175 cm} x{6.2419 cm} x{6.4106 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{17.67cm}}{\bf\textcolor{white}{Creating Collections (cont)}} \tn % Row 10 \SetRowColor{LightBackground} Empty Set & `val emptySet: Set\textless{}Int\textgreater{} = emptySet()` & Or `setOf()` / `hashSetOf()` / \seqsplit{`linkedSetOf()`} \tn % Row Count 4 (+ 4) % Row 11 \SetRowColor{white} \mymulticolumn{3}{x{17.67cm}}{\{\{ac\}\}{\bf{Maps}}} \tn % Row Count 5 (+ 1) % Row 12 \SetRowColor{LightBackground} Simple Map & `val aMap: Map\textless{}String, Int\textgreater{} = mapOf("hi" to 1, "hello" to 2)` & Or \seqsplit{`mapOf(Pair("hi"}, 1)` / \seqsplit{`hashMapOf("hi"} to 1)` / \seqsplit{`linkedMapOf("hi"} to 1)` \tn % Row Count 11 (+ 6) % Row 13 \SetRowColor{white} Empty Map & `val emptyMap: Map\textless{}String, Int\textgreater{} = emptyMap()` & Or `mapOf()` / `hashMapOf()` / \seqsplit{`linkedMapOf()`} \tn % Row Count 15 (+ 4) % Row 14 \SetRowColor{LightBackground} \mymulticolumn{3}{x{17.67cm}}{\{\{ac\}\}{\bf{Black sheep, mutables}}} \tn % Row Count 16 (+ 1) % Row 15 \SetRowColor{white} Simple \textasciicircum{}Mutable\textasciicircum{} List & `val mutableList: MutableList\textless{}Int\textgreater{} = \seqsplit{mutableListOf(1}, 2, 3)` & \tn % Row Count 21 (+ 5) % Row 16 \SetRowColor{LightBackground} Simple \textasciicircum{}Mutable\textasciicircum{} Set & `val mutableSet: MutableSet\textless{}Int\textgreater{} = \seqsplit{mutableSetOf(1)`} & \tn % Row Count 25 (+ 4) % Row 17 \SetRowColor{white} Simple \textasciicircum{}Mutable\textasciicircum{} Map & `var mutableMap: MutableMap\textless{}String, Int\textgreater{} = \seqsplit{mutableMapOf("hi"} to 1, "hello" to 2)` & \tn % Row Count 31 (+ 6) \hhline{>{\arrayrulecolor{DarkBackground}}---} \SetRowColor{LightBackground} \mymulticolumn{3}{x{17.67cm}}{We will be using these collections throughout the cheat sheet.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{3.1293 cm} x{4.941 cm} x{3.4587 cm} x{4.941 cm} } \SetRowColor{DarkBackground} \mymulticolumn{4}{x{17.67cm}}{\bf\textcolor{white}{Operators}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Method}} & {\bf{Example}} & {\bf{Result}} & {\bf{Explanation}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{4}{x{17.67cm}}{\{\{ac\}\} {\emph{Iterables}}} \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} Plus & `intList + 1` & `{[}1, 2, 3, 1{]}` & Returns a new iterables with old values + added one \tn % Row Count 8 (+ 5) % Row 3 \SetRowColor{white} Plus \seqsplit{(Iterable)} & `intList + listOf(1, 2, 3)` & `{[}1, 2, 3, 1, 2, 3{]}` & Return a new iterables with old values + values from added iterable \tn % Row Count 14 (+ 6) % Row 4 \SetRowColor{LightBackground} Minus & `intList - 1` & `{[}2, 3{]}` & Returns a new iterables with old values - subtracted one \tn % Row Count 19 (+ 5) % Row 5 \SetRowColor{white} Minus \seqsplit{(Iterable)} & `intList - listOf(1, 2)` & `{[}3{]} & Returns a new iterables with old values - values from subtracted iterable \tn % Row Count 26 (+ 7) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{4}{x{17.67cm}}{\{\{ac\}\} {\emph{Maps}}} \tn % Row Count 27 (+ 1) % Row 7 \SetRowColor{white} Plus & `aMap + Pair("Hi", 2)` & `\{hi=1, hello=2, Goodbye=3\}` & Returns new map with old map values + new Pair. Updates value if it differs \tn % Row Count 34 (+ 7) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{3.1293 cm} x{4.941 cm} x{3.4587 cm} x{4.941 cm} } \SetRowColor{DarkBackground} \mymulticolumn{4}{x{17.67cm}}{\bf\textcolor{white}{Operators (cont)}} \tn % Row 8 \SetRowColor{LightBackground} Plus (Map) & `aMap + \seqsplit{mapOf(Pair("hello"}, 2), \seqsplit{Pair("Goodbye"}, 3)` & `\{hi=1, hello=2, Goodbye=3\}` & Returns new map with old map values + Pairs from added map. Updates values if they differ. \tn % Row Count 8 (+ 8) % Row 9 \SetRowColor{white} Minus & `aMap - Pair("Hi", 2)` & \{Hi=2\} & Takes in a key and removes if found \tn % Row Count 11 (+ 3) % Row 10 \SetRowColor{LightBackground} Minus (Map) & aMap - \seqsplit{listOf("hello"}, "hi") & `\{\}` & Takes in an iterable of keys and removes if found \tn % Row Count 16 (+ 5) % Row 11 \SetRowColor{white} \mymulticolumn{4}{x{17.67cm}}{\{\{ac\}\} {\emph{Mutables}}} \tn % Row Count 17 (+ 1) % Row 12 \SetRowColor{LightBackground} Minus Assign & \seqsplit{`mutableList} -= 2` & `{[}1, 3{]}` & Mutates the list, removes element if found. Returns boolean \tn % Row Count 22 (+ 5) % Row 13 \SetRowColor{white} Plus Assign & \seqsplit{`mutableList} += 2` & `{[}1, 3, 2{]}` & Mutates the list, adds element. Returns boolean \tn % Row Count 26 (+ 4) % Row 14 \SetRowColor{LightBackground} Minus Assign \seqsplit{(MutableMap)} & \seqsplit{`mutableMap.minusAssign("hello")`} & \{hi=1\} & Takes in key and removes if that is found from the mutated map. Returns boolean. Same as `-=` \tn % Row Count 34 (+ 8) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{3.1293 cm} x{4.941 cm} x{3.4587 cm} x{4.941 cm} } \SetRowColor{DarkBackground} \mymulticolumn{4}{x{17.67cm}}{\bf\textcolor{white}{Operators (cont)}} \tn % Row 15 \SetRowColor{LightBackground} Plus Assign \seqsplit{(MutableMap)} & \seqsplit{`mutableMap.plusAssign("Goodbye"} to 3)` & \{hi=1, Goodbye=3\} & Takes in key and adds a new pair into the mutated map. Returns boolean. Same as `+=` \tn % Row Count 7 (+ 7) \hhline{>{\arrayrulecolor{DarkBackground}}----} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{2.9646 cm} x{4.941 cm} x{3.6234 cm} x{4.941 cm} } \SetRowColor{DarkBackground} \mymulticolumn{4}{x{17.67cm}}{\bf\textcolor{white}{Transformers}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Method}} & {\bf{Example}} & {\bf{Result}} & {\bf{Explanation}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \seqsplit{Associate} & \seqsplit{`intList.associate} \{ \seqsplit{Pair(it.toString()}, it) \}` & `\{1=1, 2=2, 3=3\}` & Returns a Map containing key-value pairs created by lambda \tn % Row Count 7 (+ 5) % Row 2 \SetRowColor{LightBackground} Map & \seqsplit{`intList.map} \{ it + 1 \}` & `{[}2,3,4{]}` & Returns a new list by \seqsplit{transforming} all elements from the initial Iterable. \tn % Row Count 14 (+ 7) % Row 3 \SetRowColor{white} \seqsplit{MapNotNull} & \seqsplit{`intList.mapNotNull} \{ null \}` & `{[}{]}` & Returned list contains only elements that return as not null from the lamdba \tn % Row Count 21 (+ 7) % Row 4 \SetRowColor{LightBackground} \seqsplit{MapIndexed} & \seqsplit{`intList.mapIndexed} \{ idx, value -\textgreater{} \{\{nl\}\}~~if (idx == 0) value + 1 else value + 2 \}` & `{[}2,4,5{]}` & Returns a new list by \seqsplit{transforming} all elements from the initial Iterable. Lambda receives an index as first value, element itself as second. \tn % Row Count 33 (+ 12) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{2.9646 cm} x{4.941 cm} x{3.6234 cm} x{4.941 cm} } \SetRowColor{DarkBackground} \mymulticolumn{4}{x{17.67cm}}{\bf\textcolor{white}{Transformers (cont)}} \tn % Row 5 \SetRowColor{LightBackground} \seqsplit{MapIndexedNotNull} & \seqsplit{`intList.mapIndexedNotNull} \{ idx, value -\textgreater{} \{\{nl\}\}~~ if (idx == 0) null else value + 2 \}` & `{[}4,5{]}` & Combination of Map, MapIndexed \& \seqsplit{MapIndexedNotNull} \tn % Row Count 9 (+ 9) % Row 6 \SetRowColor{white} \seqsplit{MapKeys} & \seqsplit{`aMap.mapKeys} \{ pair -\textgreater{} pair.key + ", mate" \}` & `\{hi, mate=1, hello, mate=2\}` & Transforms all elements from a map. Receives a Pair to lambda, lamdba return value is the new key of original value \tn % Row Count 19 (+ 10) % Row 7 \SetRowColor{LightBackground} \seqsplit{MapValues} & \seqsplit{`aMap.mapValues} \{ pair -\textgreater{} pair.value + 2 \})` & `\{hi=3, hello=4\}` & Transforms all elements from a map. Receives a Pair to lambda, lamdba return value is the new value for the original key. \tn % Row Count 30 (+ 11) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{2.9646 cm} x{4.941 cm} x{3.6234 cm} x{4.941 cm} } \SetRowColor{DarkBackground} \mymulticolumn{4}{x{17.67cm}}{\bf\textcolor{white}{Transformers (cont)}} \tn % Row 8 \SetRowColor{LightBackground} \seqsplit{Reversed} & \seqsplit{`intList.reversed())`} & `{[}3,2,1{]}` & \tn % Row Count 2 (+ 2) % Row 9 \SetRowColor{white} \seqsplit{Partition} & \seqsplit{`intList.partition} \{ it \textgreater{} 2 \})` & `Pair({[}1,2{]}, {[}3{]})` & Splits collection into to based on predicate \tn % Row Count 6 (+ 4) % Row 10 \SetRowColor{LightBackground} Slice & \seqsplit{`intList.slice(1..2))`} & `{[}2,3{]}` & Takes a range from collection based on indexes \tn % Row Count 10 (+ 4) % Row 11 \SetRowColor{white} Sorted & \seqsplit{`intList.sorted())`} & `{[}1,2,3{]}` & \tn % Row Count 12 (+ 2) % Row 12 \SetRowColor{LightBackground} \seqsplit{SortedByDescending} & \seqsplit{`intList.sortedByDescending} \{ it \}` & `{[}3,2,1{]}` & Sorts descending based on what lambda returns. Lamdba receives the value itself. \tn % Row Count 19 (+ 7) % Row 13 \SetRowColor{white} \seqsplit{SortedWith} & `intList.sortedWith(Comparator\textless{}Int\textgreater{} \{ x, y -\textgreater{} \{\{nl\}\}~~when \{ \{\{nl\}\}~~ x == 2 -\textgreater{} 1 \{\{nl\}\}~~ y == 2 -\textgreater{} -1 \{\{nl\}\}~~ else -\textgreater{} y - x \{\{nl\}\}~~\} \{\{nl\}\}\})` & `{[}3,1,2{]}` & Takes in a Comparator and uses that to sort elements in Iterable. \tn % Row Count 40 (+ 21) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{2.9646 cm} x{4.941 cm} x{3.6234 cm} x{4.941 cm} } \SetRowColor{DarkBackground} \mymulticolumn{4}{x{17.67cm}}{\bf\textcolor{white}{Transformers (cont)}} \tn % Row 14 \SetRowColor{LightBackground} \seqsplit{Flatten} & \seqsplit{`listOf(intList}, \seqsplit{aSet).flatten()`} & `{[}2,3,4,1{]}` & Takes elements of all passed in collections and returns a collection with all those elements \tn % Row Count 8 (+ 8) % Row 15 \SetRowColor{white} \seqsplit{FlatMap} with just return & \seqsplit{`listOf(intList}, \seqsplit{aSet).flatMap} \{ it \}` & `{[}2,3,4,1{]}` & Used for Iterable of Iterables and Lambdas that return Iterables. Transforms elements and flattens them after \seqsplit{transformation}. \tn % Row Count 19 (+ 11) % Row 16 \SetRowColor{LightBackground} \seqsplit{FlatMap} with \seqsplit{transform} & ` \seqsplit{listOf(intList}, \seqsplit{aSet).flatMap} \{ iterable: Iterable\textless{}Int\textgreater{} -\textgreater{} \{\{nl\}\}~~iterable.map \{ it + 1 \} \{\{nl\}\} \} ` & `{[}2,3,4,2{]}` & FlatMap is often used with monadic containers to fluently handle context, errors and side effects. \tn % Row Count 30 (+ 11) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{2.9646 cm} x{4.941 cm} x{3.6234 cm} x{4.941 cm} } \SetRowColor{DarkBackground} \mymulticolumn{4}{x{17.67cm}}{\bf\textcolor{white}{Transformers (cont)}} \tn % Row 17 \SetRowColor{LightBackground} Zip & `listOf(3, \seqsplit{4).zip(intList)`} & `{[}(3,1), (4,2){]}` & Creates a list of Pairs from two Iterables. As many pairs as values in shorter of the original Iterables. \tn % Row Count 9 (+ 9) % Row 18 \SetRowColor{white} Zip with \seqsplit{predicate} & ` listOf(3, \seqsplit{4).zip(intList)} \{ firstElem, secondElem -\textgreater{} \{\{nl\}\}~~Pair(firstElem - 2, secondElem + 2) \{\{nl\}\}\} ` & `{[}(1,3), (2,4){]}` & Creates a list of Pairs from two Iterables. As many pairs as values in shorter of the original Iterables. Lambda receives both items on that index from Iterables. \tn % Row Count 23 (+ 14) % Row 19 \SetRowColor{LightBackground} Unzip & \seqsplit{`listOf(Pair("hi"}, 1), \seqsplit{Pair("hello"}, \seqsplit{2)).unzip()`} & `Pair({[}hi, hello{]}, {[}1,2{]})` & Reverses the operation from `zip`. Takes in an Iterable of Pairs and returns them as a Pair of Lists. \tn % Row Count 32 (+ 9) \hhline{>{\arrayrulecolor{DarkBackground}}----} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{3.1293 cm} x{5.1057 cm} x{3.1293 cm} x{5.1057 cm} } \SetRowColor{DarkBackground} \mymulticolumn{4}{x{17.67cm}}{\bf\textcolor{white}{Aggregators}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Method}} & {\bf{Example}} & {\bf{Result}} & {\bf{Explanation}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{4}{x{17.67cm}}{\{\{ac\}\} {\bf{Folds And Reduces}}} \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} Fold & \seqsplit{`intList.fold(10)} \{ accumulator, value -\textgreater{} \{\{nl\}\} accumulator + value \}` & `16` {\emph{(10+1+2+3)}} & Accumulates values starting with initial and applying operation from left to right. Lambda receives accumulated value and current value. \tn % Row Count 15 (+ 12) % Row 3 \SetRowColor{white} \seqsplit{FoldIndexed} & \seqsplit{`intList.foldIndexed(10)} \{ idx, accumulator, value -\textgreater{} \{\{nl\}\} if (idx == 2) accumulator else accumulator + value \}` & `13` {\emph{(10+1+2)}} & Accumulates values starting with initial and applying operation from left to right. Lambda receives index as the first value. \tn % Row Count 26 (+ 11) % Row 4 \SetRowColor{LightBackground} \seqsplit{FoldRight} & \seqsplit{`intList.foldRight(10)} \{ accumulator, value -\textgreater{} \{\{nl\}\} accumulator + value \}` & `16` {\emph{(10+3+2+1)}} & Accumulates values starting with initial and applying operation from right to left. Lambda receives accumulated value and current value. \tn % Row Count 38 (+ 12) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{3.1293 cm} x{5.1057 cm} x{3.1293 cm} x{5.1057 cm} } \SetRowColor{DarkBackground} \mymulticolumn{4}{x{17.67cm}}{\bf\textcolor{white}{Aggregators (cont)}} \tn % Row 5 \SetRowColor{LightBackground} \seqsplit{FoldRightIndexed} & \seqsplit{`intList.foldRightIndexed(10)} \{ idx, accumulator, value -\textgreater{} \{\{nl\}\} if (idx == 2) accumulator else accumulator + value \}` & `16` {\emph{(10+3+2+1)}} & \tn % Row Count 11 (+ 11) % Row 6 \SetRowColor{white} Reduce & \seqsplit{`intList.reduce} \{ accumulator, value -\textgreater{} \{\{nl\}\} accumulator + value \}` & `6` {\emph{(1+2+3)}} & Accumulates values starting with first value and applying operation from left to right. Lambda receives accumulated value and current value. \tn % Row Count 23 (+ 12) % Row 7 \SetRowColor{LightBackground} \seqsplit{ReduceRight} & \seqsplit{`intList.reduceRight} \{ accumulator, value -\textgreater{} \{\{nl\}\} accumulator + value \}` & `6` {\emph{(3+2+1)}} & Accumulates values starting with first value and applying operation from right to left. Lambda receives accumulated value and current value. \tn % Row Count 35 (+ 12) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{3.1293 cm} x{5.1057 cm} x{3.1293 cm} x{5.1057 cm} } \SetRowColor{DarkBackground} \mymulticolumn{4}{x{17.67cm}}{\bf\textcolor{white}{Aggregators (cont)}} \tn % Row 8 \SetRowColor{LightBackground} \seqsplit{ReduceIndexed} & \seqsplit{`intList.reduceIndexed} \{ idx, accumulator, value -\textgreater{} \{\{nl\}\} if (idx == 2) accumulator else accumulator + value \}` & `3` {\emph{(1+2)}} & \tn % Row Count 11 (+ 11) % Row 9 \SetRowColor{white} \seqsplit{ReduceRightIndexed} & \seqsplit{`intList.reduceRightIndexed} \{ idx, accumulator, value -\textgreater{} \{\{nl\}\} if (idx == 2) accumulator else accumulator + value \}` & `3` {\emph{(2+1)}} & \tn % Row Count 22 (+ 11) % Row 10 \SetRowColor{LightBackground} \mymulticolumn{4}{x{17.67cm}}{} \tn % Row Count 22 (+ 0) % Row 11 \SetRowColor{white} \mymulticolumn{4}{x{17.67cm}}{\{\{ac\}\}{\bf{Grouping}}} \tn % Row Count 23 (+ 1) % Row 12 \SetRowColor{LightBackground} \seqsplit{GroupBy} & \seqsplit{`intList.groupBy} \{ value -\textgreater{} 2 \}` & `\{2={[}1, 2, 3{]}\}` & Uses value returned from lamdba to group elements of the Iterable. All values whose lambda returns same key will be grouped. \tn % Row Count 34 (+ 11) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{3.1293 cm} x{5.1057 cm} x{3.1293 cm} x{5.1057 cm} } \SetRowColor{DarkBackground} \mymulticolumn{4}{x{17.67cm}}{\bf\textcolor{white}{Aggregators (cont)}} \tn % Row 13 \SetRowColor{LightBackground} \seqsplit{GroupBy} (With new \seqsplit{values)} & `intList.groupBy(\{ it \}, \{ it + 1 \})` & `\{1={[}2{]}, 2={[}3{]}, 3={[}4{]}\}` & Same as group by plus takes another lambda that can be used to transform the current value \tn % Row Count 8 (+ 8) % Row 14 \SetRowColor{white} \seqsplit{GroupByTo} & `val \seqsplit{mutableStringToListMap} = \seqsplit{mapOf("first"} to 1, "second" to 2)\{\{nl\}\}mutableStringToListMap.values.groupByTo(mutableMapOf\textless{}Int, MutableList\textless{}Int\textgreater{}\textgreater{}(), \{ value: Int -\textgreater{} value \}, \{ value -\textgreater{} value + 10 \})` & \{1={[}11{]}, 2={[}12{]}\} & Group by first lambda, modify value with second lambda, dump the values to given mutable map \tn % Row Count 25 (+ 17) % Row 15 \SetRowColor{LightBackground} \seqsplit{GroupingBy} -\textgreater{} FoldTo & \seqsplit{`intList.groupingBy} \{ it \}\{\{nl\}\}.foldTo(mutableMapOf\textless{}Int, Int\textgreater{}(), 0) \{ accumulator, element -\textgreater{} \{\{nl\}\}~~ accumulator + element \}` & `\{1=1, 2=2, 3=3\}` & Create a grouping by a lambda, fold using passed in lambda and given initial value, insert into given mutable destination object \tn % Row Count 38 (+ 13) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{3.1293 cm} x{5.1057 cm} x{3.1293 cm} x{5.1057 cm} } \SetRowColor{DarkBackground} \mymulticolumn{4}{x{17.67cm}}{\bf\textcolor{white}{Aggregators (cont)}} \tn % Row 16 \SetRowColor{LightBackground} \seqsplit{Grouping} \textgreater{} \seqsplit{Aggregate} & \seqsplit{`intList.groupingBy} \{ "key" \} \{\{nl\}\}.aggregate(\{ key, \seqsplit{accumulator:} String?, element, isFirst -\textgreater{} \{\{nl\}\} ~~ when \seqsplit{(accumulator)} \{ \{\{nl\}\} ~~~~ null -\textgreater{} "\$element" \{\{nl\}\} ~~~~ else -\textgreater{} accumulator + "\$element" \{\{nl\}\} ~~ \} \{\{nl\}\} \})` & `\{key=123\}` & Create a grouping by a lambda, aggregate each group. Lambda receives all keys, nullable accumulator and the element plus a flag if value is the first on from this group. If isFirst -{}-\textgreater{} accumulator is null. \tn % Row Count 30 (+ 30) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{3.1293 cm} x{5.1057 cm} x{3.1293 cm} x{5.1057 cm} } \SetRowColor{DarkBackground} \mymulticolumn{4}{x{17.67cm}}{\bf\textcolor{white}{Aggregators (cont)}} \tn % Row 17 \SetRowColor{LightBackground} \mymulticolumn{4}{x{17.67cm}}{} \tn % Row Count 0 (+ 0) % Row 18 \SetRowColor{white} \mymulticolumn{4}{x{17.67cm}}{\{\{ac\}\}{\bf{Aggregating}}} \tn % Row Count 1 (+ 1) % Row 19 \SetRowColor{LightBackground} Count & \seqsplit{`intList.count()`} & `3` & AKA size \tn % Row Count 3 (+ 2) % Row 20 \SetRowColor{white} Count (with \seqsplit{Lambda)} & \seqsplit{`intList.count} \{ it == 2 \})` & `1` & Count of elements satisfying the predicate \tn % Row Count 7 (+ 4) % Row 21 \SetRowColor{LightBackground} \seqsplit{Average} & \seqsplit{`intList.average()`} & `2.0` {\emph{((1+2+3)/3 = 2.0)}} & Only for numeric Iterables \tn % Row Count 11 (+ 4) % Row 22 \SetRowColor{white} Max & \seqsplit{`intList.max()`} & `3` & Maximum value in the list. Only for Iterables of \seqsplit{Comparables.} \tn % Row Count 17 (+ 6) % Row 23 \SetRowColor{LightBackground} MaxBy & \seqsplit{`intList.maxBy} \{ it * 3 \}` & `3` & Maximum value returned from lambda. Only for Lambdas returning \seqsplit{Comparables.} \tn % Row Count 24 (+ 7) % Row 24 \SetRowColor{white} \seqsplit{MaxWith} & \seqsplit{`intList.maxWith(oneOrLarger)`} & `1` & Maximum value defined by passed in Comparator \tn % Row Count 28 (+ 4) % Row 25 \SetRowColor{LightBackground} Min & \seqsplit{`intList.min()`} & `1` & Minimum value in the list. Only for Iterables of \seqsplit{Comparables.} \tn % Row Count 34 (+ 6) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{3.1293 cm} x{5.1057 cm} x{3.1293 cm} x{5.1057 cm} } \SetRowColor{DarkBackground} \mymulticolumn{4}{x{17.67cm}}{\bf\textcolor{white}{Aggregators (cont)}} \tn % Row 26 \SetRowColor{LightBackground} MinBy & \seqsplit{`intList.minBy} \{ it * 3 \}` & `1` & Minimum value returned from lambda. Only for Lambdas returning \seqsplit{Comparables.} \tn % Row Count 7 (+ 7) % Row 27 \SetRowColor{white} \seqsplit{MinWith} & \seqsplit{`intList.minWith(oneOrLarger)`} & `3` & Minimum value defined by passed in Comparator \tn % Row Count 11 (+ 4) % Row 28 \SetRowColor{LightBackground} Sum & \seqsplit{`intList.sum()`} & `6` & Summation of all values in Iterable. Only numeric Iterables. \tn % Row Count 16 (+ 5) % Row 29 \SetRowColor{white} SumBy & \seqsplit{`intList.sumBy} \{ if(it == 3) 6 else it \})` & `9` {\emph{(1+2+6)}} & Summation of values returned by passed in lambda. Only for lambdas returning numeric values. \tn % Row Count 24 (+ 8) % Row 30 \SetRowColor{LightBackground} \seqsplit{SumByDouble} & \seqsplit{`intList.sumByDouble} \{ \seqsplit{it.toDouble()} \}` & `6.0` & Summation to Double values. \seqsplit{Lambdareceives} the value and returns a Double. \tn % Row Count 31 (+ 7) \hhline{>{\arrayrulecolor{DarkBackground}}----} \SetRowColor{LightBackground} \mymulticolumn{4}{x{17.67cm}}{val oneOrLarger = Comparator\textless{}Int\textgreater{} \{ x, y -\textgreater{} \newline ~~ when\{ \newline ~~~~ x == 1 -\textgreater{} 1 \newline ~~~~ y == 1 -\textgreater{} -1 \newline ~~~~ else -\textgreater{} y - x \newline ~~ \} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}----} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{3.7881 cm} x{5.2704 cm} x{1.9764 cm} x{5.4351 cm} } \SetRowColor{DarkBackground} \mymulticolumn{4}{x{17.67cm}}{\bf\textcolor{white}{Filtering and other predicates + simple HOFs}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Method}} & {\bf{Example}} & {\bf{Result}} & {\bf{Notes}} \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} \mymulticolumn{4}{x{17.67cm}}{\{\{ac\}\}{\bf{Filtering}}} \tn % Row Count 4 (+ 1) % Row 2 \SetRowColor{LightBackground} Filter & \seqsplit{`intList.filter} \{ it \textgreater{} 2 \}` & `{[}3{]}` & Filter-in \tn % Row Count 7 (+ 3) % Row 3 \SetRowColor{white} \seqsplit{FilterKeys} & \seqsplit{`aMap.filterKeys} \{ it != "hello" \}` & `\{hi=1\}` & \tn % Row Count 10 (+ 3) % Row 4 \SetRowColor{LightBackground} \seqsplit{FilterValues} & \seqsplit{`aMap.filterValues} \{ it == 2 \}` & `\{hello=2\}` & \tn % Row Count 13 (+ 3) % Row 5 \SetRowColor{white} \seqsplit{FilterIndexed} & \seqsplit{`intList.filterIndexed} \{ idx, value -\textgreater{} idx == 2 || value == 2 \}` & `{[}2,3{]}` & \tn % Row Count 19 (+ 6) % Row 6 \SetRowColor{LightBackground} \seqsplit{FilterIsInstance} & `intList.filterIsInstance\textless{}String\textgreater{}()` & `{[}{]}` & All of them are ints \tn % Row Count 22 (+ 3) % Row 7 \SetRowColor{white} \mymulticolumn{4}{x{17.67cm}}{} \tn % Row Count 22 (+ 0) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{4}{x{17.67cm}}{\{\{ac\}\}{\bf{Taking and Dropping}}} \tn % Row Count 23 (+ 1) % Row 9 \SetRowColor{white} Take & \seqsplit{`intList.take(2)`} & `{[}1,2{]}` & Take n elements from Iterable. If passed in number larger than list, full list is returned. \tn % Row Count 30 (+ 7) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{3.7881 cm} x{5.2704 cm} x{1.9764 cm} x{5.4351 cm} } \SetRowColor{DarkBackground} \mymulticolumn{4}{x{17.67cm}}{\bf\textcolor{white}{Filtering and other predicates + simple HOFs (cont)}} \tn % Row 10 \SetRowColor{LightBackground} \seqsplit{TakeWhile} & \seqsplit{`intList.takeWhile} \{ it \textless{} 3 \}` & `{[}1,2{]}` & \tn % Row Count 3 (+ 3) % Row 11 \SetRowColor{white} TakeLast & \seqsplit{`intList.takeLast(2)`} & `{[}2,3{]}` & \tn % Row Count 5 (+ 2) % Row 12 \SetRowColor{LightBackground} \seqsplit{TakeLastWhile} & \seqsplit{`intList.takeLastWhile} \{ it \textless{} 3 \}` & `{[}{]}` & Last element already satisfies this condition -{}-\textgreater{} empty \tn % Row Count 10 (+ 5) % Row 13 \SetRowColor{white} Drop & \seqsplit{`intList.drop(2)`} & `{[}3{]}` & Drop n elements from the start of the Iterable. \tn % Row Count 14 (+ 4) % Row 14 \SetRowColor{LightBackground} \seqsplit{DropWhile} & \seqsplit{`intList.dropWhile} \{ it \textless{} 3 \}` & `{[}3{]}` & \tn % Row Count 17 (+ 3) % Row 15 \SetRowColor{white} DropLast & \seqsplit{`intList.dropLast(2)`} & `{[}1{]}` & \tn % Row Count 19 (+ 2) % Row 16 \SetRowColor{LightBackground} \seqsplit{DropLastWhile} & \seqsplit{`intList.dropLastWhile} \{ it \textgreater{} 2 \}` & `{[}1, 2{]}` & \tn % Row Count 22 (+ 3) % Row 17 \SetRowColor{white} \mymulticolumn{4}{x{17.67cm}}{} \tn % Row Count 22 (+ 0) % Row 18 \SetRowColor{LightBackground} \mymulticolumn{4}{x{17.67cm}}{\{\{ac\}\}{\bf{Retrieving individual elements}}} \tn % Row Count 23 (+ 1) % Row 19 \SetRowColor{white} \seqsplit{Component} & \seqsplit{`intList.component1()`} & `1` & There are 5 of these -{}-\textgreater{} \seqsplit{`component1()`}, \seqsplit{`component2()`}, \seqsplit{`component3()`}, \seqsplit{`component4()`}, \seqsplit{`component5()`} \tn % Row Count 31 (+ 8) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{3.7881 cm} x{5.2704 cm} x{1.9764 cm} x{5.4351 cm} } \SetRowColor{DarkBackground} \mymulticolumn{4}{x{17.67cm}}{\bf\textcolor{white}{Filtering and other predicates + simple HOFs (cont)}} \tn % Row 20 \SetRowColor{LightBackground} \seqsplit{ElementAt} & \seqsplit{`intList.elementAt(2)`} & `3` & Retrieve element at his index. Throws \seqsplit{IndexOutOfBounds} if element index doesn't exist \tn % Row Count 7 (+ 7) % Row 21 \SetRowColor{white} \seqsplit{ElementAtOrElse} & \seqsplit{`intList.elementAtOrElse(13)} \{ 4 \}` & `4` & Retrieve element at his index or return lambda value if element index doesn't exist. \tn % Row Count 14 (+ 7) % Row 22 \SetRowColor{LightBackground} \seqsplit{ElementAtOrNull} & \seqsplit{`intList.elementAtOrNull(666)`} & \seqsplit{`null`} & Retrieve element at his index or return null if element index doesn't exist. \tn % Row Count 20 (+ 6) % Row 23 \SetRowColor{white} Get (clumsy syntax) & \seqsplit{`intList.get(2)`} & `3` & Get element by index \tn % Row Count 23 (+ 3) % Row 24 \SetRowColor{LightBackground} Get & `intList{[}2{]}` & `3` & Shorthand and preferred way for the one above \tn % Row Count 27 (+ 4) % Row 25 \SetRowColor{white} \seqsplit{GetOrElse} & \seqsplit{intList.getOrElse(14)} \{ 42 \} & `42` & Get element or return lambda value if it doesn't exist. \tn % Row Count 32 (+ 5) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{3.7881 cm} x{5.2704 cm} x{1.9764 cm} x{5.4351 cm} } \SetRowColor{DarkBackground} \mymulticolumn{4}{x{17.67cm}}{\bf\textcolor{white}{Filtering and other predicates + simple HOFs (cont)}} \tn % Row 26 \SetRowColor{LightBackground} Get from Map (clumsy syntax) & \seqsplit{`aMap.get("hi")`} & `1` & \tn % Row Count 4 (+ 4) % Row 27 \SetRowColor{white} Get from Map & `aMap{[}"hi"{]}` & `1` & \tn % Row Count 6 (+ 2) % Row 28 \SetRowColor{LightBackground} GetValue & \seqsplit{`aMap.getValue("hi")1} & `1` & Get value or throw \seqsplit{NoSuchElementException} \tn % Row Count 10 (+ 4) % Row 29 \SetRowColor{white} \seqsplit{GetOrDefault} & \seqsplit{`aMap.getOrDefault("HI"}, 4)` & `4` & Get value or return the value returned from lambda \tn % Row Count 14 (+ 4) % Row 30 \SetRowColor{LightBackground} GetOrPut & \seqsplit{`mutableMap.getOrPut("HI")} \{ 5 \}` & `5` & MutableMap only. Returns the the value if it exist, otherwise puts it and returns put value. \tn % Row Count 22 (+ 8) % Row 31 \SetRowColor{white} \mymulticolumn{4}{x{17.67cm}}{} \tn % Row Count 22 (+ 0) % Row 32 \SetRowColor{LightBackground} \mymulticolumn{4}{x{17.67cm}}{\{\{ac\}\}{\bf{Finding}}} \tn % Row Count 23 (+ 1) % Row 33 \SetRowColor{white} \seqsplit{BinarySearch} & \seqsplit{`intList.binarySearch(2)`} & `1` & Does a binary search through the collection and returns the index of the element if found. Otherwise returns negative index. \tn % Row Count 33 (+ 10) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{3.7881 cm} x{5.2704 cm} x{1.9764 cm} x{5.4351 cm} } \SetRowColor{DarkBackground} \mymulticolumn{4}{x{17.67cm}}{\bf\textcolor{white}{Filtering and other predicates + simple HOFs (cont)}} \tn % Row 34 \SetRowColor{LightBackground} Find & \seqsplit{`intList.find} \{ it \textgreater{} 1 \}` & `2` & First element satisfying the condition or null if not found \tn % Row Count 5 (+ 5) % Row 35 \SetRowColor{white} FindLast & \seqsplit{`intList.findLast} \{ it \textgreater{} 1 \}` & `3` & Last element satisfying the condition or null if not found \tn % Row Count 10 (+ 5) % Row 36 \SetRowColor{LightBackground} First & \seqsplit{`intList.first()`} & `1` & First element of Iterable or throws \seqsplit{NoSuchElementException} \tn % Row Count 15 (+ 5) % Row 37 \SetRowColor{white} First with \seqsplit{predicate} & \seqsplit{`intList.first} \{ it \textgreater{} 1 \}` & `2` & Same as find but throws \seqsplit{NoSuchElementException} if not found \tn % Row Count 20 (+ 5) % Row 38 \SetRowColor{LightBackground} \seqsplit{FirstOrNull} & \seqsplit{`intList.firstOrNull()`} & `1` & Throw safe version of `first()`. \tn % Row Count 23 (+ 3) % Row 39 \SetRowColor{white} \seqsplit{FirstOrNull} with \seqsplit{predicate} & \seqsplit{`intList.firstOrNull} \{ it \textgreater{} 1 \}` & `2` & Throw safe version of `first(() -\textgreater{} Boolean)`. \tn % Row Count 27 (+ 4) % Row 40 \SetRowColor{LightBackground} IndexOf & \seqsplit{`intList.indexOf(1)`} & `0` & \tn % Row Count 29 (+ 2) % Row 41 \SetRowColor{white} \seqsplit{IndexOfFirst} & \seqsplit{`intList.indexOfFirst} \{ it \textgreater{} 1 \}` & `1` & \tn % Row Count 32 (+ 3) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{3.7881 cm} x{5.2704 cm} x{1.9764 cm} x{5.4351 cm} } \SetRowColor{DarkBackground} \mymulticolumn{4}{x{17.67cm}}{\bf\textcolor{white}{Filtering and other predicates + simple HOFs (cont)}} \tn % Row 42 \SetRowColor{LightBackground} \seqsplit{IndexOfLast} & \seqsplit{`intList.indexOfLast} \{ it \textgreater{} 1 \}` & `2` & \tn % Row Count 3 (+ 3) % Row 43 \SetRowColor{white} Last & \seqsplit{`intList.last()`} & `3` & Throws \seqsplit{NoSuchElementException} if empty Iterable \tn % Row Count 7 (+ 4) % Row 44 \SetRowColor{LightBackground} Last with \seqsplit{predicate} & \seqsplit{`intList.last} \{ it \textgreater{} 1 \}` & `3` & Throws \seqsplit{NoSuchElementException} if none found satisfying the condition. \tn % Row Count 13 (+ 6) % Row 45 \SetRowColor{white} \seqsplit{LastIndexOf} & \seqsplit{`intList.lastIndexOf(2)`} & `1` & \tn % Row Count 15 (+ 2) % Row 46 \SetRowColor{LightBackground} \seqsplit{LastOrNull} & \seqsplit{`intList.lastOrNull()`} & `3` & Throw safe version of `last()` \tn % Row Count 18 (+ 3) % Row 47 \SetRowColor{white} \seqsplit{LastOrNull} with \seqsplit{predicate} & \seqsplit{`intList.lastOrNull} \{ it \textgreater{} 1 \}` & `3` & Throw safe version of `last(() -\textgreater{} Boolean)`. \tn % Row Count 22 (+ 4) % Row 48 \SetRowColor{LightBackground} \mymulticolumn{4}{x{17.67cm}}{} \tn % Row Count 22 (+ 0) % Row 49 \SetRowColor{white} \mymulticolumn{4}{x{17.67cm}}{\{\{ac\}\}{\bf{Unions, distincts, intersections etc.}}} \tn % Row Count 23 (+ 1) % Row 50 \SetRowColor{LightBackground} Distinct & \seqsplit{`intList.distinct()`} & `{[}1, 2, 3{]}` & \tn % Row Count 26 (+ 3) % Row 51 \SetRowColor{white} \seqsplit{DistinctBy} & \seqsplit{`intList.distinctBy} \{ if (it \textgreater{} 1) it else 2 \}` & `{[}1,3{]}` & \tn % Row Count 30 (+ 4) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{3.7881 cm} x{5.2704 cm} x{1.9764 cm} x{5.4351 cm} } \SetRowColor{DarkBackground} \mymulticolumn{4}{x{17.67cm}}{\bf\textcolor{white}{Filtering and other predicates + simple HOFs (cont)}} \tn % Row 52 \SetRowColor{LightBackground} \seqsplit{Intersect} & \seqsplit{`intList.intersect(listOf(1}, 2))` & `{[}1,2{]}` & \tn % Row Count 3 (+ 3) % Row 53 \SetRowColor{white} \seqsplit{MinusElement} & \seqsplit{`intList.minusElement(2)`} & `{[}1,3{]}` & \tn % Row Count 6 (+ 3) % Row 54 \SetRowColor{LightBackground} \seqsplit{MinusElement} with \seqsplit{collection} & \seqsplit{`intList.minusElement(listOf(1}, 2))` & `{[}3{]}` & \tn % Row Count 10 (+ 4) % Row 55 \SetRowColor{white} Single & \seqsplit{`listOf("One} \seqsplit{Element").single()`} & `One \seqsplit{Element`} & Returns only element or throws. \tn % Row Count 14 (+ 4) % Row 56 \SetRowColor{LightBackground} \seqsplit{SingleOrNull} & \seqsplit{`intList.singleOrNull()`} & null & Throw safe version of `single()`. \tn % Row Count 17 (+ 3) % Row 57 \SetRowColor{white} OrEmpty & \seqsplit{`intList.orEmpty()`} & {[}1, 2{[}, 3{]} & Returns itself or an empty list if itself is null. \tn % Row Count 21 (+ 4) % Row 58 \SetRowColor{LightBackground} Union & \seqsplit{`intList.union(listOf(4},5,6))` & `{[}1,2,3,4,5,6{]}` & \tn % Row Count 25 (+ 4) % Row 59 \SetRowColor{white} Union (infix \seqsplit{notation)} & `intList union listOf(4,5,6)` & `{[}1,2,3,4,5,6{]}` & \tn % Row Count 29 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}----} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{2.9646 cm} x{5.9292 cm} x{2.9646 cm} x{4.6116 cm} } \SetRowColor{DarkBackground} \mymulticolumn{4}{x{17.67cm}}{\bf\textcolor{white}{Checks and Actions}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Method}} & {\bf{Example}} & {\bf{Result}} & {\bf{Notes}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{4}{x{17.67cm}}{\{\{ac\}\}{\bf{Acting on list elements}}} \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{4}{x{17.67cm}}{\{\{ac\}\}`val listOfFunctions = listOf(\{ print("first ") \}, \{ print("second ") \})`} \tn % Row Count 5 (+ 2) % Row 3 \SetRowColor{white} \seqsplit{ForEach} & \seqsplit{`listOfFunctions}.forEach \{ it() \}` & `first \seqsplit{second`} & \tn % Row Count 8 (+ 3) % Row 4 \SetRowColor{LightBackground} \seqsplit{ForEachIndexed} & \seqsplit{`listOfFunctions}.forEachIndexed \{ idx, fn -\textgreater{} if (idx == 0) fn() else print("Won't do it") \} ` & `first Won't do it` & \tn % Row Count 15 (+ 7) % Row 5 \SetRowColor{white} OnEach & \seqsplit{`intList.onEach} \{ print(it) \}` & `123` & \tn % Row Count 18 (+ 3) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{4}{x{17.67cm}}{\{\{ac\}\}{\bf{Checks}}} \tn % Row Count 19 (+ 1) % Row 7 \SetRowColor{white} All & `intList.all \{ it \textless{} 4 \}` & `true` & All of them are less than 4 \tn % Row Count 22 (+ 3) % Row 8 \SetRowColor{LightBackground} Any & \seqsplit{`intList.any()`} & `true` & Collection has elements \tn % Row Count 25 (+ 3) % Row 9 \SetRowColor{white} Any with \seqsplit{predicate} & `intList.any \{ it \textgreater{} 4 \}` & \seqsplit{`false`} & None of them are more than 4 \tn % Row Count 28 (+ 3) % Row 10 \SetRowColor{LightBackground} \seqsplit{Contains} & \seqsplit{`intList.contains(3)`} & `true` & \tn % Row Count 30 (+ 2) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{2.9646 cm} x{5.9292 cm} x{2.9646 cm} x{4.6116 cm} } \SetRowColor{DarkBackground} \mymulticolumn{4}{x{17.67cm}}{\bf\textcolor{white}{Checks and Actions (cont)}} \tn % Row 11 \SetRowColor{LightBackground} \seqsplit{ContainsAll} & \seqsplit{`intList.containsAll(listOf(2}, 3, 4))` & \seqsplit{`false`} & \tn % Row Count 3 (+ 3) % Row 12 \SetRowColor{white} \seqsplit{Contains} (Map) & \seqsplit{`aMap.contains("Hello")`} & \seqsplit{`false`} & Same as \seqsplit{`containsKey()`} \tn % Row Count 6 (+ 3) % Row 13 \SetRowColor{LightBackground} \seqsplit{ContainsKey} & \seqsplit{`aMap.containsKey("hello")`} & `true` & Same as \seqsplit{`contains()`} \tn % Row Count 8 (+ 2) % Row 14 \SetRowColor{white} \seqsplit{ContainsValue} & \seqsplit{`aMap.containsValue(2)`} & `true` & \tn % Row Count 10 (+ 2) % Row 15 \SetRowColor{LightBackground} None & \seqsplit{`intList.none()`} & \seqsplit{`false`} & There are elements on the list \tn % Row Count 13 (+ 3) % Row 16 \SetRowColor{white} None with \seqsplit{predicate} & `intList.none \{ it \textgreater{} 5 \}` & `true` & None of them are larger than 5 \tn % Row Count 16 (+ 3) % Row 17 \SetRowColor{LightBackground} \seqsplit{IsEmpty} & \seqsplit{`intList.isEmpty()`} & \seqsplit{`false`} & \tn % Row Count 18 (+ 2) % Row 18 \SetRowColor{white} \seqsplit{IsNotEmpty} & \seqsplit{`intList.isNotEmpty()`} & `true` & \tn % Row Count 20 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}----} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{\textless{}3 Kotlin}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{Github repository with all code examples:\{\{nl\}\}\{\{popup="https://github.com/Xantier/Kollections"\}\}https://github.com/Xantier/Kollections\{\{/popup\}\}} \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{PDF of this cheat sheet:\{\{nl\}\}\{\{popup="http://jussi.hallila.com/Kollections/"\}\}http://jussi.hallila.com/Kollections/\{\{/popup\}\}} \tn % Row Count 6 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{Created with \textless{}3 by Jussi Hallila} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \end{document}