├── .gitignore ├── LICENSE ├── README.md ├── images ├── classhierarchy.png ├── legend.graphml ├── legend.png ├── scala-collection-immutable.graphml ├── scala-collection-immutable.png ├── scala-collection-mutable.graphml ├── scala-collection-mutable.png ├── scala-collection.graphml └── scala-collection.png ├── scala-cheat-sheet.pdf └── scala-cheat-sheet.tex /.gitignore: -------------------------------------------------------------------------------- 1 | *.acn 2 | *.acr 3 | *.alg 4 | *.aux 5 | *.bbl 6 | *.blg 7 | *.dvi 8 | *.fdb_latexmk 9 | *.glg 10 | *.glo 11 | *.gls 12 | *.idx 13 | *.ilg 14 | *.ind 15 | *.ist 16 | *.lof 17 | *.log 18 | *.lot 19 | *.maf 20 | *.mtc 21 | *.mtc0 22 | *.nav 23 | *.nlo 24 | *.out 25 | *.pdfsync 26 | *.ps 27 | *.snm 28 | *.synctex.gz 29 | *.toc 30 | *.vrb 31 | *.xdy 32 | *.tdo 33 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Scala cheat sheet 2 | ============================ 3 | 4 | ##Download PDF 5 | [scala-cheat-sheet.pdf](https://github.com/soulmachine/scala-cheat-sheet/raw/master/scala-cheat-sheet.pdf) 6 | 7 | ##How to compile on Windows 8 | 1. Install [Tex Live 2013](http://www.tug.org/texlive/), then add its `bin` path for example `D:\texlive\2013\bin\win32` to he PATH environment variable. 9 | 2. Install [TeXstudio](http://texstudio.sourceforge.net/). 10 | 3. Configure TeXstudio. 11 | Run TeXstudio, click `Options-->Configure Texstudio-->Commands`, set `PdfLaTex` to `pdflatex.exe -synctex=1 -interaction=nonstopmode %.tex`. 12 | Click `Options-->Configure Texstudio-->Build`, 13 | set `Build & View` to `Compile & View`, 14 | set `Default Compiler` to `PdfLaTex`, 15 | set `PDF Viewer` to `Internal PDF Viewer(windowed)`, so that when previewing it will pop up a standalone window, which will be convenient. 16 | 4. Compile. Open `scala-cheat-sheet.tex` with TeXstudio,click the green arrow on the menu bar, then it will start to compile. 17 | In the messages window below we can see the compilation command that TeXstudio used is ` pdflatex.exe -synctex=1 -interaction=nonstopmode "scala-cheat-sheet".tex` 18 | -------------------------------------------------------------------------------- /images/classhierarchy.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soulmachine/scala-cheat-sheet/778c8158b267976ba17334b1239fa9542f99563f/images/classhierarchy.png -------------------------------------------------------------------------------- /images/legend.graphml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | Trait 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | Class 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | implemented by 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | default implementation 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | 219 | 220 | 221 | 222 | 223 | 224 | 225 | 226 | 227 | 228 | implemented by 229 | 230 | 231 | 232 | 233 | 234 | 235 | 236 | 237 | 238 | 239 | 240 | 241 | 242 | 243 | 244 | 245 | 246 | 247 | 248 | 249 | 250 | 251 | 252 | 253 | 254 | 255 | 256 | 257 | 258 | 259 | 260 | 261 | 262 | 263 | 264 | 265 | 266 | 267 | 268 | 269 | 270 | 271 | 272 | 273 | 274 | -------------------------------------------------------------------------------- /images/legend.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soulmachine/scala-cheat-sheet/778c8158b267976ba17334b1239fa9542f99563f/images/legend.png -------------------------------------------------------------------------------- /images/scala-collection-immutable.graphml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | Traversable 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | Iterable 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | Set 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | Seq 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | Map 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | IndexedSeq 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | LinearSeq 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | SortedSet 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | SortedMap 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | BitSet 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | ListSet 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | TreeSet 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | 214 | 215 | HashSet 216 | 217 | 218 | 219 | 220 | 221 | 222 | 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | 231 | HashMap 232 | 233 | 234 | 235 | 236 | 237 | 238 | 239 | 240 | 241 | 242 | 243 | 244 | 245 | 246 | 247 | ListMap 248 | 249 | 250 | 251 | 252 | 253 | 254 | 255 | 256 | 257 | 258 | 259 | 260 | 261 | 262 | 263 | TreeMap 264 | 265 | 266 | 267 | 268 | 269 | 270 | 271 | 272 | 273 | 274 | 275 | 276 | 277 | 278 | 279 | String 280 | 281 | 282 | 283 | 284 | 285 | 286 | 287 | 288 | 289 | 290 | 291 | 292 | 293 | 294 | 295 | Range 296 | 297 | 298 | 299 | 300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308 | 309 | 310 | 311 | NumericRange 312 | 313 | 314 | 315 | 316 | 317 | 318 | 319 | 320 | 321 | 322 | 323 | 324 | 325 | 326 | 327 | Vector 328 | 329 | 330 | 331 | 332 | 333 | 334 | 335 | 336 | 337 | 338 | 339 | 340 | 341 | 342 | 343 | List 344 | 345 | 346 | 347 | 348 | 349 | 350 | 351 | 352 | 353 | 354 | 355 | 356 | 357 | 358 | 359 | Stream 360 | 361 | 362 | 363 | 364 | 365 | 366 | 367 | 368 | 369 | 370 | 371 | 372 | 373 | 374 | 375 | Stack 376 | 377 | 378 | 379 | 380 | 381 | 382 | 383 | 384 | 385 | 386 | 387 | 388 | 389 | 390 | 391 | Queue 392 | 393 | 394 | 395 | 396 | 397 | 398 | 399 | 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 419 | 420 | 421 | 422 | 423 | 424 | 425 | 426 | 427 | 428 | 429 | 430 | 431 | 432 | 433 | 434 | 435 | 436 | 437 | 438 | 439 | 440 | 441 | 442 | 443 | 444 | 445 | 446 | 447 | 448 | 449 | 450 | 451 | 452 | 453 | 454 | 455 | 456 | 457 | 458 | 459 | 460 | 461 | 462 | 463 | 464 | 465 | 466 | 467 | 468 | 469 | 470 | 471 | 472 | 473 | 474 | 475 | 476 | 477 | 478 | 479 | 480 | 481 | 482 | 483 | 484 | 485 | 486 | 487 | 488 | 489 | 490 | 491 | 492 | 493 | 494 | 495 | 496 | 497 | 498 | 499 | 500 | 501 | 502 | 503 | 504 | 505 | 506 | 507 | 508 | 509 | 510 | 511 | 512 | 513 | 514 | 515 | 516 | 517 | 518 | 519 | 520 | 521 | 522 | 523 | 524 | 525 | 526 | 527 | 528 | 529 | 530 | 531 | 532 | 533 | 534 | 535 | 536 | 537 | 538 | 539 | 540 | 541 | 542 | 543 | 544 | 545 | 546 | 547 | 548 | 549 | 550 | 551 | 552 | 553 | 554 | 555 | 556 | 557 | 558 | 559 | 560 | 561 | 562 | 563 | 564 | 565 | 566 | 567 | 568 | 569 | 570 | 571 | 572 | 573 | 574 | 575 | 576 | 577 | 578 | 579 | 580 | 581 | 582 | 583 | 584 | 585 | 586 | 587 | 588 | 589 | 590 | 591 | 592 | 593 | 594 | 595 | 596 | 597 | 598 | 599 | 600 | 601 | 602 | 603 | 604 | 605 | 606 | 607 | 608 | 609 | 610 | 611 | 612 | 613 | 614 | 615 | 616 | 617 | 618 | 619 | 620 | 621 | 622 | 623 | 624 | 625 | 626 | 627 | 628 | 629 | 630 | 631 | 632 | 633 | 634 | 635 | 636 | 637 | 638 | 639 | 640 | 641 | 642 | -------------------------------------------------------------------------------- /images/scala-collection-immutable.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soulmachine/scala-cheat-sheet/778c8158b267976ba17334b1239fa9542f99563f/images/scala-collection-immutable.png -------------------------------------------------------------------------------- /images/scala-collection-mutable.graphml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | Traversable 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | Iterable 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | Set 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | Seq 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | Map 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | IndexedSeq 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | LinearSeq 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | SortedSet 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | BitSet 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | LinkedHashSet 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | TreeSet 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | HashSet 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | 214 | 215 | HashMap 216 | 217 | 218 | 219 | 220 | 221 | 222 | 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | 231 | ListMap 232 | 233 | 234 | 235 | 236 | 237 | 238 | 239 | 240 | 241 | 242 | 243 | 244 | 245 | 246 | 247 | ArraySeq 248 | 249 | 250 | 251 | 252 | 253 | 254 | 255 | 256 | 257 | 258 | 259 | 260 | 261 | 262 | 263 | Array 264 | 265 | 266 | 267 | 268 | 269 | 270 | 271 | 272 | 273 | 274 | 275 | 276 | 277 | 278 | 279 | StringBuilder 280 | 281 | 282 | 283 | 284 | 285 | 286 | 287 | 288 | 289 | 290 | 291 | 292 | 293 | 294 | 295 | ArrayBuffer 296 | 297 | 298 | 299 | 300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308 | 309 | 310 | 311 | MutableList 312 | 313 | 314 | 315 | 316 | 317 | 318 | 319 | 320 | 321 | 322 | 323 | 324 | 325 | 326 | 327 | LinkedList 328 | 329 | 330 | 331 | 332 | 333 | 334 | 335 | 336 | 337 | 338 | 339 | 340 | 341 | 342 | 343 | DoubledLinkedList 344 | 345 | 346 | 347 | 348 | 349 | 350 | 351 | 352 | 353 | 354 | 355 | 356 | 357 | 358 | 359 | ImmutableSetAdaptor 360 | 361 | 362 | 363 | 364 | 365 | 366 | 367 | 368 | 369 | 370 | 371 | 372 | 373 | 374 | 375 | ObservableSet 376 | 377 | 378 | 379 | 380 | 381 | 382 | 383 | 384 | 385 | 386 | 387 | 388 | 389 | 390 | 391 | SynchronizedSet 392 | 393 | 394 | 395 | 396 | 397 | 398 | 399 | 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | LinkedHashMap 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 419 | 420 | 421 | 422 | 423 | WeakHashMap 424 | 425 | 426 | 427 | 428 | 429 | 430 | 431 | 432 | 433 | 434 | 435 | 436 | 437 | 438 | 439 | OpenHashMap 440 | 441 | 442 | 443 | 444 | 445 | 446 | 447 | 448 | 449 | 450 | 451 | 452 | 453 | 454 | 455 | MultiMap 456 | 457 | 458 | 459 | 460 | 461 | 462 | 463 | 464 | 465 | 466 | 467 | 468 | 469 | 470 | 471 | ObservableMap 472 | 473 | 474 | 475 | 476 | 477 | 478 | 479 | 480 | 481 | 482 | 483 | 484 | 485 | 486 | 487 | SynchronizedMap 488 | 489 | 490 | 491 | 492 | 493 | 494 | 495 | 496 | 497 | 498 | 499 | 500 | 501 | 502 | 503 | ImmutableMapAdaptor 504 | 505 | 506 | 507 | 508 | 509 | 510 | 511 | 512 | 513 | 514 | 515 | 516 | 517 | 518 | 519 | Buffer 520 | 521 | 522 | 523 | 524 | 525 | 526 | 527 | 528 | 529 | 530 | 531 | 532 | 533 | 534 | 535 | ListBuffer 536 | 537 | 538 | 539 | 540 | 541 | 542 | 543 | 544 | 545 | 546 | 547 | 548 | 549 | 550 | 551 | ObservableBuffer 552 | 553 | 554 | 555 | 556 | 557 | 558 | 559 | 560 | 561 | 562 | 563 | 564 | 565 | 566 | 567 | SynchronizedBuffer 568 | 569 | 570 | 571 | 572 | 573 | 574 | 575 | 576 | 577 | 578 | 579 | 580 | 581 | 582 | 583 | Stack 584 | 585 | 586 | 587 | 588 | 589 | 590 | 591 | 592 | 593 | 594 | 595 | 596 | 597 | 598 | 599 | ArrayStack 600 | 601 | 602 | 603 | 604 | 605 | 606 | 607 | 608 | 609 | 610 | 611 | 612 | 613 | 614 | 615 | PriorityQueue 616 | 617 | 618 | 619 | 620 | 621 | 622 | 623 | 624 | 625 | 626 | 627 | 628 | 629 | 630 | 631 | SynchronizedStack 632 | 633 | 634 | 635 | 636 | 637 | 638 | 639 | 640 | 641 | 642 | 643 | 644 | 645 | 646 | 647 | SynchronizedPriorityQueue 648 | 649 | 650 | 651 | 652 | 653 | 654 | 655 | 656 | 657 | 658 | 659 | 660 | 661 | 662 | 663 | Queue 664 | 665 | 666 | 667 | 668 | 669 | 670 | 671 | 672 | 673 | 674 | 675 | 676 | 677 | 678 | 679 | SynchronizedQueue 680 | 681 | 682 | 683 | 684 | 685 | 686 | 687 | 688 | 689 | 690 | 691 | 692 | 693 | 694 | 695 | 696 | 697 | 698 | 699 | 700 | 701 | 702 | 703 | 704 | 705 | 706 | 707 | 708 | 709 | 710 | 711 | 712 | 713 | 714 | 715 | 716 | 717 | 718 | 719 | 720 | 721 | 722 | 723 | 724 | 725 | 726 | 727 | 728 | 729 | 730 | 731 | 732 | 733 | 734 | 735 | 736 | 737 | 738 | 739 | 740 | 741 | 742 | 743 | 744 | 745 | 746 | 747 | 748 | 749 | 750 | 751 | 752 | 753 | 754 | 755 | 756 | 757 | 758 | 759 | 760 | 761 | 762 | 763 | 764 | 765 | 766 | 767 | 768 | 769 | 770 | 771 | 772 | 773 | 774 | 775 | 776 | 777 | 778 | 779 | 780 | 781 | 782 | 783 | 784 | 785 | 786 | 787 | 788 | 789 | 790 | 791 | 792 | 793 | 794 | 795 | 796 | 797 | 798 | 799 | 800 | 801 | 802 | 803 | 804 | 805 | 806 | 807 | 808 | 809 | 810 | 811 | 812 | 813 | 814 | 815 | 816 | 817 | 818 | 819 | 820 | 821 | 822 | 823 | 824 | 825 | 826 | 827 | 828 | 829 | 830 | 831 | 832 | 833 | 834 | 835 | 836 | 837 | 838 | 839 | 840 | 841 | 842 | 843 | 844 | 845 | 846 | 847 | 848 | 849 | 850 | 851 | 852 | 853 | 854 | 855 | 856 | 857 | 858 | 859 | 860 | 861 | 862 | 863 | 864 | 865 | 866 | 867 | 868 | 869 | 870 | 871 | 872 | 873 | 874 | 875 | 876 | 877 | 878 | 879 | 880 | 881 | 882 | 883 | 884 | 885 | 886 | 887 | 888 | 889 | 890 | 891 | 892 | 893 | 894 | 895 | 896 | 897 | 898 | 899 | 900 | 901 | 902 | 903 | 904 | 905 | 906 | 907 | 908 | 909 | 910 | 911 | 912 | 913 | 914 | 915 | 916 | 917 | 918 | 919 | 920 | 921 | 922 | 923 | 924 | 925 | 926 | 927 | 928 | 929 | 930 | 931 | 932 | 933 | 934 | 935 | 936 | 937 | 938 | 939 | 940 | 941 | 942 | 943 | 944 | 945 | 946 | 947 | 948 | 949 | 950 | 951 | 952 | 953 | 954 | 955 | 956 | 957 | 958 | 959 | 960 | 961 | 962 | 963 | 964 | 965 | 966 | 967 | 968 | 969 | 970 | 971 | 972 | 973 | 974 | 975 | 976 | 977 | 978 | 979 | 980 | 981 | 982 | 983 | 984 | 985 | 986 | 987 | 988 | 989 | 990 | 991 | 992 | 993 | 994 | 995 | 996 | 997 | 998 | 999 | 1000 | 1001 | 1002 | 1003 | 1004 | 1005 | 1006 | 1007 | 1008 | 1009 | 1010 | 1011 | 1012 | 1013 | 1014 | 1015 | 1016 | 1017 | 1018 | 1019 | 1020 | 1021 | 1022 | 1023 | 1024 | 1025 | 1026 | 1027 | 1028 | 1029 | 1030 | 1031 | 1032 | 1033 | 1034 | 1035 | 1036 | 1037 | 1038 | 1039 | 1040 | 1041 | 1042 | 1043 | 1044 | 1045 | 1046 | 1047 | 1048 | 1049 | 1050 | 1051 | 1052 | 1053 | 1054 | 1055 | 1056 | 1057 | 1058 | 1059 | 1060 | 1061 | 1062 | 1063 | 1064 | 1065 | 1066 | 1067 | 1068 | 1069 | 1070 | 1071 | 1072 | 1073 | 1074 | 1075 | 1076 | 1077 | 1078 | 1079 | 1080 | 1081 | 1082 | 1083 | 1084 | 1085 | 1086 | 1087 | 1088 | 1089 | 1090 | 1091 | 1092 | 1093 | 1094 | 1095 | 1096 | 1097 | 1098 | 1099 | 1100 | 1101 | 1102 | 1103 | 1104 | 1105 | 1106 | 1107 | 1108 | 1109 | 1110 | 1111 | 1112 | 1113 | 1114 | 1115 | 1116 | 1117 | 1118 | 1119 | 1120 | 1121 | 1122 | 1123 | 1124 | 1125 | 1126 | 1127 | 1128 | 1129 | 1130 | 1131 | 1132 | 1133 | 1134 | -------------------------------------------------------------------------------- /images/scala-collection-mutable.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soulmachine/scala-cheat-sheet/778c8158b267976ba17334b1239fa9542f99563f/images/scala-collection-mutable.png -------------------------------------------------------------------------------- /images/scala-collection.graphml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | Traversable 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | Iterable 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | Set 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | Seq 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | Map 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | IndexedSeq 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | LinearSeq 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | SortedSet 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | BitSet 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | SortedMap 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | 219 | 220 | 221 | 222 | 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | 231 | 232 | 233 | 234 | 235 | 236 | 237 | 238 | 239 | 240 | 241 | 242 | 243 | 244 | 245 | 246 | 247 | 248 | 249 | 250 | 251 | 252 | 253 | 254 | 255 | 256 | 257 | 258 | 259 | 260 | 261 | 262 | 263 | 264 | 265 | 266 | 267 | 268 | 269 | 270 | 271 | 272 | 273 | 274 | 275 | 276 | 277 | 278 | 279 | 280 | 281 | 282 | 283 | 284 | 285 | 286 | 287 | 288 | 289 | 290 | 291 | -------------------------------------------------------------------------------- /images/scala-collection.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soulmachine/scala-cheat-sheet/778c8158b267976ba17334b1239fa9542f99563f/images/scala-collection.png -------------------------------------------------------------------------------- /scala-cheat-sheet.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soulmachine/scala-cheat-sheet/778c8158b267976ba17334b1239fa9542f99563f/scala-cheat-sheet.pdf -------------------------------------------------------------------------------- /scala-cheat-sheet.tex: -------------------------------------------------------------------------------- 1 | \documentclass[a4paper,twoside,10pt,landscape]{article} 2 | \usepackage{multicol} 3 | \usepackage{calc} 4 | \usepackage{ifthen} 5 | \usepackage[landscape]{geometry} 6 | \usepackage{hyperref} 7 | \usepackage{graphicx} % standard LaTeX graphics tool when including figure files 8 | \graphicspath{{images/}} 9 | \usepackage[font={small}]{caption} 10 | \captionsetup{aboveskip=3pt} 11 | 12 | % To make this come out properly in landscape mode, do one of the following 13 | % 1. 14 | % pdflatex latexsheet.tex 15 | % 16 | % 2. 17 | % latex latexsheet.tex 18 | % dvips -P pdf -t landscape latexsheet.dvi 19 | % ps2pdf latexsheet.ps 20 | 21 | 22 | % If you're reading this, be prepared for confusion. Making this was 23 | % a learning experience for me, and it shows. Much of the placement 24 | % was hacked in; if you make it better, let me know... 25 | 26 | 27 | % 2008-04 28 | % Changed page margin code to use the geometry package. Also added code for 29 | % conditional page margins, depending on paper size. Thanks to Uwe Ziegenhagen 30 | % for the suggestions. 31 | 32 | % 2006-08 33 | % Made changes based on suggestions from Gene Cooperman. 34 | 35 | 36 | % To Do: 37 | % \listoffigures \listoftables 38 | % \setcounter{secnumdepth}{0} 39 | 40 | 41 | % This sets page margins to .5 inch if using letter paper, and to 1cm 42 | % if using A4 paper. (This probably isn't strictly necessary.) 43 | % If using another size paper, use default 1cm margins. 44 | \ifthenelse{\lengthtest { \paperwidth = 11in}} 45 | { \geometry{top=.5in,left=.5in,right=.5in,bottom=.5in} } 46 | {\ifthenelse{ \lengthtest{ \paperwidth = 297mm}} 47 | {\geometry{top=1cm,left=1cm,right=1cm,bottom=1cm} } 48 | {\geometry{top=1cm,left=1cm,right=1cm,bottom=1cm} } 49 | } 50 | 51 | % Turn off header and footer 52 | \pagestyle{empty} 53 | 54 | 55 | % Redefine section commands to use less space 56 | \makeatletter 57 | \renewcommand{\section}{\@startsection{section}{1}{0mm}% 58 | {-1ex plus -.5ex minus -.2ex}% 59 | {0.5ex plus .2ex}%x 60 | {\normalfont\large\bfseries}} 61 | \renewcommand{\subsection}{\@startsection{subsection}{2}{0mm}% 62 | {-1explus -.5ex minus -.2ex}% 63 | {0.5ex plus .2ex}% 64 | {\normalfont\normalsize\bfseries}} 65 | \renewcommand{\subsubsection}{\@startsection{subsubsection}{3}{0mm}% 66 | {-1ex plus -.5ex minus -.2ex}% 67 | {1ex plus .2ex}% 68 | {\normalfont\small\bfseries}} 69 | \makeatother 70 | 71 | % Define BibTeX command 72 | \def\BibTeX{{\rm B\kern-.05em{\sc i\kern-.025em b}\kern-.08em 73 | T\kern-.1667em\lower.7ex\hbox{E}\kern-.125emX}} 74 | 75 | % Don't print section numbers 76 | %\setcounter{secnumdepth}{0} 77 | 78 | 79 | \setlength{\parindent}{0pt} 80 | \setlength{\parskip}{0pt plus 0.5ex} 81 | 82 | 83 | % ----------------------------------------------------------------------- 84 | 85 | \begin{document} 86 | 87 | \raggedright 88 | \footnotesize 89 | \begin{multicols}{3} 90 | 91 | 92 | % multicol parameters 93 | % These lengths are set only within the two main columns 94 | %\setlength{\columnseprule}{0.25pt} 95 | \setlength{\premulticols}{1pt} 96 | \setlength{\postmulticols}{1pt} 97 | \setlength{\multicolsep}{1pt} 98 | \setlength{\columnsep}{2pt} 99 | 100 | \begin{center} 101 | \Large{\textbf{Scala Cheat Sheet}} \\ 102 | \end{center} 103 | 104 | 105 | \section{Scala Class Hierarchy} 106 | \begin{center} 107 | \includegraphics[scale=.69]{classhierarchy.png} 108 | \captionof{figure}{Scala class hierarchy, source: \url{http://www.scala-lang.org/old/node/128}} 109 | \label{fig:scala-class-hierarchy} 110 | \end{center} 111 | 112 | 113 | \section{Scala Collections} 114 | 115 | 116 | \subsection{Scala Collections Hierarchy} 117 | 118 | \begin{center} 119 | \includegraphics[scale=.50]{legend.png} 120 | \end{center} 121 | 122 | \begin{center} 123 | \includegraphics[scale=.50]{scala-collection.png} 124 | \captionof{figure}{scala.collection} 125 | \label{fig:scala-collection} 126 | \end{center} 127 | 128 | \begin{center} 129 | \includegraphics[scale=.70]{scala-collection-immutable.png} 130 | \captionof{figure}{scala.collection.immutable} 131 | \label{fig:scala-collection-immutable} 132 | \end{center} 133 | 134 | \begin{center} 135 | \includegraphics[scale=.68]{scala-collection-mutable.png} 136 | \captionof{figure}{scala.collection.mutable} 137 | \label{fig:scala-collection-mutable} 138 | \end{center} 139 | 140 | 141 | \subsection{Trait \texttt{Traversable}} 142 | \begin{center} 143 | \captionof{table}{Methods in \texttt{Traversable}} 144 | \begin{tabular}{@{}lp{6.5cm}@{}} 145 | \hline\noalign{\smallskip} 146 | \textbf{Category} & \textbf{Methods} \\ 147 | \noalign{\smallskip}\hline\noalign{\smallskip} 148 | \textbf{Abstract} & \texttt{xs foreach f}\\ 149 | \textbf{Addition} & \texttt{xs ++ ys}\\ 150 | \textbf{Maps} & \texttt{xs map f, xs flatMap f, xs collect f}\\ 151 | \textbf{Conversions} & \texttt{toArray, toList, toIterable, toSeq, toIndexedSeq, toStream, toSet, toMap}\\ 152 | \textbf{Size info} & \texttt{isEmpty, nonEmpty, size, hasDefiniteSize}\\ 153 | \textbf{Element} & \texttt{head, headOption, last, lastOption},\\ 154 | \textbf{Retrieval} & \texttt{xs find p}\\ 155 | \textbf{Sub-} & \texttt{xs.tail, xs.init, xs slice (from, to)},\\ 156 | \textbf{collection} & \texttt{xs take n, xs drop n, xs takeWhile p, xs dropWhile p, xs filter p, xs withFilter p, xs filterNot p}\\ 157 | \textbf{Subdivision} & \texttt{xs splitAt n, xs span p, xs partition p, xs groupBy f}\\ 158 | \textbf{Element} & \texttt{xs forall p, xs exists p, xs count p}\\ 159 | \textbf{Condition} & \\ 160 | \textbf{Fold} & \texttt{(z /: xs)(op), (xs :\ z)(op), xs.foldLeft(z)(op), xs.foldRight(z)(op), xs reduceLeft op, xs reduceRight op}\\ 161 | \textbf{Specific Fold} & \texttt{xs.sum, xs.product, xs.min, xs.max}\\ 162 | \textbf{String} & \texttt{xs addString (b, start, sep, end), xs mkString (start, sep, end), xs.stringPrefix}\\ 163 | \textbf{View} & \texttt{xs.view, xs view (from, to)}\\ 164 | \noalign{\smallskip}\hline 165 | \end{tabular} 166 | \raggedright{\tiny{Reference: \url{http://docs.scala-lang.org/overviews/collections/trait-traversable.html}}} 167 | \end{center} 168 | 169 | 170 | \subsection{Trait \texttt{Iterable}} 171 | All methods in this trait are defined in terms of an an abstract method, \texttt{iterator}, which yields the collection’s elements one by one. 172 | 173 | \begin{center} 174 | \captionof{table}{Methods in \texttt{Iterable}} 175 | \begin{tabular}{@{}lp{6cm}@{}} 176 | \hline\noalign{\smallskip} 177 | \textbf{Category} & \textbf{Methods} \\ 178 | \noalign{\smallskip}\hline\noalign{\smallskip} 179 | \textbf{Abstract} & \texttt{xs.iterator}\\ 180 | \textbf{Iterator} & \texttt{xs grouped n, xs sliding n}\\ 181 | \textbf{Subcollection} & \texttt{xs takeRight n, xs dropRight n}\\ 182 | \textbf{Zipper} & \texttt{xs zip ys, xs zipAll (ys, x, y), xs.zipWithIndex}\\ 183 | \textbf{Comparison} & \texttt{xs sameElements ys}\\ 184 | \noalign{\smallskip}\hline 185 | \end{tabular} 186 | \raggedright{\tiny{Reference: \url{http://docs.scala-lang.org/overviews/collections/trait-iterable.html}}} 187 | \end{center} 188 | 189 | In the inheritance hierarchy below \texttt{Iterable} you find three traits: \texttt{Seq}, \texttt{Set}, and \texttt{Map}. A common aspect of these three traits is that they all implement the \texttt{PartialFunction} trait with its \texttt{apply} and \texttt{isDefinedAt} methods. However, the way each trait implements \texttt{PartialFunction} differs. 190 | 191 | 192 | \subsection{Seq} 193 | 194 | \begin{center} 195 | \captionof{table}{Methods in \texttt{Seq}} 196 | \begin{tabular}{@{}lp{6.5cm}@{}} 197 | \hline\noalign{\smallskip} 198 | \textbf{Category} & \textbf{Methods} \\ 199 | \noalign{\smallskip}\hline\noalign{\smallskip} 200 | \textbf{Indexing and} & \texttt{xs(i), xs isDefinedAt i, xs.length,}\\ 201 | \textbf{Length} & \texttt{xs.lengthCompare ys, xs.indices}\\ 202 | \textbf{Index Search} & \texttt{xs indexOf x, xs lastIndexOf x, xs indexOfSlice ys, xs lastIndexOfSlice ys, xs indexWhere p, xs segmentLength (p, i), xs prefixLength p}\\ 203 | \textbf{Addition} & \texttt{x +: xs, xs :+ x, xs padTo (len, x)}\\ 204 | \textbf{Update} & \texttt{xs patch (i, ys, r), xs updated (i, x), xs(i) = x}(only available for \texttt{mutable.Seq}s)\\ 205 | \textbf{Sorting} & \texttt{xs.sorted, xs sortWith lt, xs sortBy f}\\ 206 | \textbf{Reversal} & \texttt{xs.reverse, xs.reverseIterator, xs reverseMap f}\\ 207 | \textbf{Comparison} & \texttt{xs startsWith ys, xs endsWith ys, xs contains x, xs containsSlice ys, (xs corresponds ys)(p)}\\ 208 | \textbf{Multiset} & \texttt{xs intersect ys, xs union ys, xs diff ys, xs.distinct}\\ 209 | \noalign{\smallskip}\hline 210 | \end{tabular} 211 | \raggedright{\tiny{Reference: \url{http://docs.scala-lang.org/overviews/collections/seqs.html}}} 212 | \end{center} 213 | 214 | \begin{center} 215 | \captionof{table}{Methods in \texttt{Buffer}} 216 | \begin{tabular}{@{}lp{6.5cm}@{}} 217 | \hline\noalign{\smallskip} 218 | \textbf{Category} & \textbf{Methods} \\ 219 | \noalign{\smallskip}\hline\noalign{\smallskip} 220 | \textbf{Addition} & \texttt{buf += x, buf += (x, y, z), buf ++= xs, x +=: buf, xs ++=: buf, buf insert (i, x), buf insertAll (i, xs)}\\ 221 | \textbf{Removal} & \texttt{buf -= x, buf remove i, buf remove (i, n), buf trimStart n, buf trimEnd n, buf.clear()}\\ 222 | \textbf{Cloning} & \texttt{buf.clone}\\ 223 | \noalign{\smallskip}\hline 224 | \end{tabular} 225 | \end{center} 226 | 227 | 228 | \subsection{Set} 229 | \begin{center} 230 | \captionof{table}{Methods in \texttt{Set}} 231 | \begin{tabular}{@{}lp{6.5cm}@{}} 232 | \hline\noalign{\smallskip} 233 | \textbf{Category} & \textbf{Methods} \\ 234 | \noalign{\smallskip}\hline\noalign{\smallskip} 235 | \textbf{Test} & \texttt{xs contains x, xs(x), xs subsetOf ys}\\ 236 | \textbf{Addition} & \texttt{xs + x, xs + (x, y, z), xs ++ ys}\\ 237 | \textbf{Removal} & \texttt{xs - x, xs - (x, y, z), xs -- ys, xs.empty}\\ 238 | \textbf{Set operation} & \texttt{xs \& ys, xs intersect ys, xs | ys, xs union ys, xs \&~ ys, xs diff ys}\\ 239 | \noalign{\smallskip}\hline 240 | \end{tabular} 241 | \raggedright{\tiny{Reference: \url{http://docs.scala-lang.org/overviews/collections/sets.html}}} 242 | \end{center} 243 | 244 | Mutable sets offer in addition methods to add, remove, or update elements, which are summarized in below. 245 | 246 | \begin{center} 247 | \captionof{table}{Methods in \texttt{mutable.Set}} 248 | \begin{tabular}{@{}lp{6.5cm}@{}} 249 | \hline\noalign{\smallskip} 250 | \textbf{Category} & \textbf{Methods} \\ 251 | \noalign{\smallskip}\hline\noalign{\smallskip} 252 | \textbf{Addition} & \texttt{xs += x, xs += (x, y, z), xs ++= ys, xs add x}\\ 253 | \textbf{Removal} & \texttt{xs -= x, xs -= (x, y, z), xs --= ys, xs remove x, xs retain p, xs.clear()}\\ 254 | \textbf{Update} & \texttt{xs(x) = b}\\ 255 | \textbf{Cloning} & \texttt{xs.clone}\\ 256 | \noalign{\smallskip}\hline 257 | \end{tabular} 258 | \end{center} 259 | 260 | 261 | \subsection{Map} 262 | \begin{center} 263 | \captionof{table}{Methods in \texttt{Map}} 264 | \begin{tabular}{@{}lp{6cm}@{}} 265 | \hline\noalign{\smallskip} 266 | \textbf{Category} & \textbf{Methods} \\ 267 | \noalign{\smallskip}\hline\noalign{\smallskip} 268 | \textbf{Lookup} & \texttt{ms get k, ms(k), ms getOrElse (k, d), ms contains k, ms isDefinedAt k}\\ 269 | \textbf{Addition} & \texttt{ms + (k -> v), ms + (k -> v, l -> w), ms ++ kvs}\\ 270 | \textbf{Removal} & \texttt{ms - k, ms - (k, 1, m), ms -- ks}\\ 271 | \textbf{Update} & \texttt{ms updated (k, v)}\\ 272 | \textbf{Subcollection} & \texttt{ms.keys, ms.keySet, ms.keyIterator, ms.values, ms.valuesIterator}\\ 273 | \textbf{Transformation} & \texttt{ms filterKeys p, ms mapValues f}\\ 274 | \noalign{\smallskip}\hline 275 | \end{tabular} 276 | \raggedright{\tiny{Reference: \url{http://docs.scala-lang.org/overviews/collections/maps.html}}} 277 | \end{center} 278 | 279 | \begin{center} 280 | \captionof{table}{Methods in \texttt{mutable.Map}} 281 | \begin{tabular}{@{}lp{6cm}@{}} 282 | \hline\noalign{\smallskip} 283 | \textbf{Category} & \textbf{Methods} \\ 284 | \noalign{\smallskip}\hline\noalign{\smallskip} 285 | \textbf{Addition} & \texttt{ms += (k -> v), ms += (k -> v, l -> w), ms ++= kvs, }\\ 286 | \textbf{Removal} & \texttt{ms -= k, ms -= (k, l, m), ms --= ks, ms remove k, ms retain p, ms.clear()}\\ 287 | \textbf{Update} & \texttt{ms(k) = v, ms put (k, v), ms getOrElseUpdate (k, d)}\\ 288 | \textbf{Transformation} & \texttt{ms transform f}\\ 289 | \textbf{Cloning} & \texttt{xs.clone}\\ 290 | \noalign{\smallskip}\hline 291 | \end{tabular} 292 | \end{center} 293 | 294 | 295 | \subsection{Performance Characteristics} 296 | \begingroup 297 | \setlength{\tabcolsep}{1pt} 298 | \begin{center} 299 | \captionof{table}{Performance characteristics of sequence types} 300 | %\begin{tabular}{@{}l@{\hskip 2pt}c@{\hskip 2pt}c@{\hskip 2pt}c@{\hskip 2pt}c@{\hskip 2pt}c@{\hskip 2pt}c@{\hskip 2pt}c@{}} 301 | \begin{tabular}{@{}lccccccc@{}} 302 | \hline\noalign{\smallskip} 303 | & \textbf{head} & \textbf{tail} & \textbf{apply} & \textbf{update} & \textbf{prepend} & \textbf{append} & \textbf{insert}\\ 304 | \noalign{\smallskip}\hline\noalign{\smallskip} 305 | \textbf{immutable} &&&&&&& \\ 306 | \texttt{List} & C & C & L & L & C & L & - \\ 307 | \texttt{Stream} & C & C & L & L & C & L & - \\ 308 | \texttt{Vector} & eC & eC & eC & eC & eC & eC & - \\ 309 | \texttt{Stack} & C & C & L & L & C & C & L \\ 310 | \texttt{Queue} & aC & aC & L & L & L & C & - \\ 311 | \texttt{Range} & C & C & C & - & - & - & - \\ 312 | \texttt{String} & C & L & C & L & L & L & - \\ 313 | \textbf{mutable} &&&&&&& \\ 314 | \texttt{ArrayBuffer} & C & L & C & C & L & aC & L \\ 315 | \texttt{ListBuffer} & C & L & L & L & C & C & L \\ 316 | \texttt{StringBuilder} & C & L & C & C & L & aC & L \\ 317 | \texttt{MutableList} & C & L & L & L & C & C & L \\ 318 | \texttt{Queue} & C & L & L & L & C & C & L \\ 319 | \texttt{ArraySeq} & C & L & C & C & - & - & - \\ 320 | \texttt{Stack} & C & L & L & L & C & L & L \\ 321 | \texttt{ArrayStack} & C & L & C & C & aC & L & L \\ 322 | \texttt{Array} & C & L & C & C & - & - & - \\ 323 | \noalign{\smallskip}\hline 324 | \end{tabular} 325 | \raggedright{\tiny{Reference: \url{http://docs.scala-lang.org/overviews/collections/performance-characteristics.html}}} 326 | \end{center} 327 | \endgroup 328 | 329 | \begin{center} 330 | \captionof{table}{Performance characteristics of set and map types} 331 | \begin{tabular}{@{}lcccc@{}} 332 | \hline\noalign{\smallskip} 333 | & \textbf{lookup} & \textbf{add} & \textbf{remove} & \textbf{min} \\ 334 | \noalign{\smallskip}\hline\noalign{\smallskip} 335 | \textbf{immutable} &&&& \\ 336 | \texttt{HashSet/HashMap} & eC & eC & eC & L \\ 337 | \texttt{TreeSet/TreeMap} & Log & Log & Log & Log \\ 338 | \texttt{BitSet} & C & L & L & $eC^1$ \\ 339 | \texttt{ListMap} & L & L & L & L \\ 340 | \textbf{mutable} &&&& \\ 341 | \texttt{HashSet/HashMap} & eC & eC & eC & L \\ 342 | \texttt{WeakHashMap} & eC & eC & eC & L \\ 343 | \texttt{BitSet} & C & aC & C & $eC^1$ \\ 344 | \texttt{TreeSet} & Log & Log & Log & Log \\ 345 | \noalign{\smallskip}\hline 346 | \multicolumn{5}{c}{\small{Footnote 1: Assuming bits are densely packed.}} 347 | \end{tabular} 348 | \end{center} 349 | 350 | The entries in these two tables are explained as follows: 351 | 352 | \begin{tabular}{@{}lp{7cm}@{}} 353 | C & The operation takes (fast) constant time. \\ 354 | eC & The operation takes effectively constant time, but this might depend on some assumptions such as maximum length of a vector or distribution of hash keys. \\ 355 | aC & The operation takes amortized constant time. Some invocations of the operation might take longer, but if many operations are performed on average only constant time per operation is taken. \\ 356 | Log & The operation takes time proportional to the logarithm of the collection size. \\ 357 | L & The operation is linear, that is it takes time proportional to the collection size. \\ 358 | - & The operation is not supported. 359 | \end{tabular} 360 | 361 | 362 | \section{Scala Parallel Collections} 363 | 364 | 365 | \subsection{Creating a Parallel Collection} 366 | Two ways to create a parallel collection: \texttt{new} and \texttt{par}. 367 | 368 | 369 | \subsection{Semantics} 370 | Conceptually, Scala’s parallel collections framework parallelizes an operation on a parallel collection by recursively “splitting” a given collection, applying an operation on each partition of the collection in parallel, and re-“combining” all of the results that were completed in parallel. 371 | 372 | These concurrent, and “out-of-order” semantics of parallel collections lead to the following two implications: 373 | \begin{enumerate} 374 | \item Side-effecting operations can lead to non-determinism 375 | \item Non-associative operations lead to non-determinism 376 | \end{enumerate} 377 | 378 | 379 | \subsection{Concrete Parallel Collection Classes} 380 | \texttt{mutable.ParArray}, \texttt{immutable.ParVector}, \texttt{immutable.ParRange}, 381 | 382 | \texttt{mutable.ParHashSet}, \texttt{mutable.ParHashMap}, \texttt{immutable.ParHashSet}, \texttt{immutable.ParHashMap}, 383 | 384 | \texttt{mutable.ParTrieMap} 385 | 386 | 387 | \subsection{Performance characteristics} 388 | \begingroup 389 | \setlength{\tabcolsep}{1pt} 390 | \begin{center} 391 | \captionof{table}{Performance characteristics of sequence types} 392 | \begin{tabular}{@{}lccccccc@{}} 393 | \hline\noalign{\smallskip} 394 | & \textbf{head} & \textbf{tail} & \textbf{apply} & \textbf{update} & \textbf{prepend} & \textbf{append} & \textbf{insert}\\ 395 | \noalign{\smallskip}\hline\noalign{\smallskip} 396 | \texttt{ParArray} & C & L & C & C & L & L & L \\ 397 | \texttt{ParVector} & eC & eC & eC & eC & eC & eC & - \\ 398 | \texttt{ParRange} & C & C & C & - & - & - & - \\ 399 | \noalign{\smallskip}\hline 400 | \end{tabular} 401 | \raggedright{\tiny{\url{http://docs.scala-lang.org/overviews/parallel-collections/concrete-parallel-collections.html}}} 402 | \end{center} 403 | \endgroup 404 | 405 | \begin{center} 406 | \captionof{table}{Performance characteristics of set and map types} 407 | \begin{tabular}{@{}lcccc@{}} 408 | \hline\noalign{\smallskip} 409 | & \textbf{lookup} & \textbf{add} & \textbf{remove} \\ 410 | \noalign{\smallskip}\hline\noalign{\smallskip} 411 | \textbf{immutable} &&& \\ 412 | \texttt{ParHashSet/ParHashMap} & eC & eC & eC \\ 413 | \textbf{mutable} &&& \\ 414 | \texttt{ParHashSet/ParHashMap} & C & C & C \\ 415 | \texttt{ParTrieMap} & eC & eC & eC \\ 416 | \noalign{\smallskip}\hline 417 | \end{tabular} 418 | \end{center} 419 | 420 | 421 | \subsection{Parallel Collection Conversions} 422 | Every sequential collection can be converted to its parallel variant using the \texttt{par} method. Certain sequential collections have a direct parallel counterpart. For these collections the conversion is efficient– it occurs in constant time, since both the sequential and the parallel collection have the same data-structural representation (one exception is mutable hash maps and hash sets which are slightly more expensive to convert the first time \texttt{par} is called, but subsequent invocations of par take constant time). It should be noted that for mutable collections, changes in the sequential collection are visible in its parallel counterpart if they share the underlying data-structure. 423 | 424 | \begin{center} 425 | \captionof{table}{Sequential collections and their direct parallel counterparts} 426 | \begin{tabular}{@{}ll@{}} 427 | \hline\noalign{\smallskip} 428 | \textbf{Sequential} & \textbf{Parallel} \\ 429 | \noalign{\smallskip}\hline\noalign{\smallskip} 430 | \textbf{mutable} & \\ 431 | \texttt{Array} & \texttt{ParArray} \\ 432 | \texttt{HashMap} & \texttt{ParHashMap} \\ 433 | \texttt{HashSet} & \texttt{ParHashSet} \\ 434 | \texttt{TrieMap} & \texttt{ParTrieMap} \\ 435 | \textbf{immutable} & \\ 436 | \texttt{Vector} & \texttt{ParVector} \\ 437 | \texttt{Range} & \texttt{ParRange} \\ 438 | \texttt{HashMap} & \texttt{ParHashMap} \\ 439 | \texttt{HashSet} & \texttt{ParHashSet} \\ 440 | \noalign{\smallskip}\hline 441 | \end{tabular} \\ 442 | \raggedright{\tiny{Source: \url{http://docs.scala-lang.org/overviews/parallel-collections/conversions.html}}} 443 | \end{center} 444 | 445 | Other collections, such as lists, queues or streams, are inherently sequential in the sense that the elements must be accessed one after the other. These collections are converted to their parallel variants by copying the elements into a similar parallel collection. For example, a functional list is converted into a standard immutable parallel sequence, which is a parallel vector. 446 | 447 | Every parallel collection can be converted to its sequential variant using the \texttt{seq} method. Converting a parallel collection to a sequential collection is always efficient– it takes constant time. Calling seq on a mutable parallel collection yields a sequential collection which is backed by the same store– updates to one collection will be visible in the other one. 448 | 449 | 450 | \subsection{Architecture of the Parallel Collections Library} 451 | Two core abstractions: \texttt{Splitter}s and \texttt{Combiner}s. 452 | 453 | \textbf{Splitter} 454 | \begin{verbatim} 455 | trait Splitter[T] extends Iterator[T] { 456 | def split: Seq[Splitter[T]] 457 | } 458 | \end{verbatim} 459 | 460 | \textbf{Combiner} 461 | \begin{verbatim} 462 | trait Combiner[Elem, To] extends Builder[Elem, To] { 463 | def combine(other: Combiner[Elem, To]): Combiner[Elem, To] 464 | } 465 | \end{verbatim} 466 | 467 | 468 | 469 | \rule{0.3\linewidth}{0.25pt} 470 | \scriptsize 471 | 472 | Copyright \copyright\ 2014 soulmachine 473 | 474 | Github: \url{https://github.com/soulmachine/scala-cheat-sheet} 475 | 476 | My blog: \url{http://www.soulmachine.me} 477 | 478 | Last update: \today 479 | 480 | 481 | \end{multicols} 482 | \end{document} 483 | --------------------------------------------------------------------------------