373 |
380 |
374 \end{frame} |
381 \end{frame} |
375 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
382 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
376 |
383 |
377 |
384 |
378 |
385 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
379 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
386 \begin{frame}[c,fragile] |
380 \begin{frame}[t] |
387 %\frametitle{Option Type} |
|
388 |
|
389 Find something below 4 in a list. What do you think Scala answers?\bigskip\bigskip |
|
390 |
|
391 \begin{onlyenv}<1> |
|
392 \begin{lstlisting}[language=Scala, numbers=none, xleftmargin=-1mm] |
|
393 List(7,2,3,4,5,6).find(_ < 4) |
|
394 |
|
395 List(5,6,7,8,9).find(_ < 4) |
|
396 \end{lstlisting} |
|
397 \end{onlyenv} |
|
398 \begin{onlyenv}<2> |
|
399 \begin{lstlisting}[language=Scala, numbers=none, xleftmargin=-1mm] |
|
400 List(7,2,3,4,5,6).find(_ < 4) |
|
401 res: Option[Int] = Some(2) |
|
402 |
|
403 List(5,6,7,8,9).find(_ < 4) |
|
404 res: Option[Int] = None |
|
405 \end{lstlisting} |
|
406 \end{onlyenv} |
|
407 |
|
408 \end{frame} |
|
409 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
410 |
|
411 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
412 \begin{frame}[c] |
381 \frametitle{Option Type} |
413 \frametitle{Option Type} |
382 |
414 |
383 |
415 \begin{itemize} |
384 |
416 \item if the value is present, you use\bigskip |
385 \end{frame} |
417 \begin{center}\pcode{Some(value)}\end{center}\bigskip\bigskip |
386 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
418 |
387 |
419 \item if no value is present, you use\bigskip |
388 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
420 \begin{center}\pcode{None}\end{center}\bigskip\bigskip |
389 \begin{frame}[t] |
421 \end{itemize} |
|
422 |
|
423 \small e.g.~\code{Option[Int]}, then \code{Some(42)} and \code{None}\\ |
|
424 good for error handling |
|
425 \end{frame} |
|
426 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
427 |
|
428 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
429 \begin{frame}[c,fragile] |
|
430 \frametitle{Option Type} |
|
431 |
|
432 \small |
|
433 \begin{onlyenv}<1> |
|
434 \begin{lstlisting}[language=Scala, numbers=none, xleftmargin=-1mm] |
|
435 Integer.parseInt("1234") |
|
436 |
|
437 // vs. |
|
438 |
|
439 def get_me_an_int(s: String) : Option[Int] = |
|
440 Try(Some(Integer.parseInt(s))).getOrElse(None) |
|
441 \end{lstlisting} |
|
442 \end{onlyenv}\bigskip\bigskip\bigskip |
|
443 |
|
444 in the Scala code it is clear from the type I have to deal |
|
445 with the \pcode{None}-case; no JavaDoc needed |
|
446 |
|
447 \end{frame} |
|
448 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
449 |
|
450 |
|
451 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
452 \begin{frame}[c,fragile] |
390 \frametitle{Higher-Order Functions} |
453 \frametitle{Higher-Order Functions} |
391 |
454 |
392 |
455 In Scala, functions can take other functions as arguments and can return |
393 |
456 a function as a result.\bigskip\bigskip |
394 \end{frame} |
457 |
395 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
458 \begin{lstlisting}[language=Scala, numbers=none, xleftmargin=1mm] |
396 |
459 List(7,2,3,4,5,6).find(_ < 4) |
|
460 \end{lstlisting} |
|
461 |
|
462 \UParrow{1}{10}{11} |
|
463 \end{frame} |
|
464 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
465 |
|
466 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
467 \begin{frame}[c,fragile] |
|
468 \frametitle{Higher-Order Functions (2)} |
|
469 |
|
470 |
|
471 \begin{lstlisting}[language=Scala, numbers=none, xleftmargin=1mm] |
|
472 def even(x: Int) : Boolean = x % 2 == 0 |
|
473 |
|
474 List(1, 2, 3, 4, 5).filter(even) |
|
475 res : List[Int] = List(2, 4) |
|
476 |
|
477 List(1, 2, 3, 4, 5).count(even) |
|
478 res : Int = 2 |
|
479 |
|
480 List(1, 2, 3, 4, 5).find(even) |
|
481 res: Option[Int] = Some(2) |
|
482 \end{lstlisting} |
|
483 |
|
484 \end{frame} |
|
485 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
486 |
|
487 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
488 \begin{frame}[c,fragile] |
|
489 \frametitle{map (lower case)} |
|
490 |
|
491 applies a function to each element of a list (and more) |
|
492 |
|
493 \begin{center} |
|
494 \begin{tikzpicture}[scale=0.9] |
|
495 |
|
496 \node (A0) at (1.2,0) {\texttt{List(\,}}; |
|
497 \node (A1) at (2.0,0) {\texttt{1\makebox[0mm]{ ,}}}; |
|
498 \node (A2) at (2.9,0) {\texttt{2\makebox[0mm]{ ,}}}; |
|
499 \node (A3) at (3.8,0) {\texttt{3\makebox[0mm]{ ,}}}; |
|
500 \node (A4) at (4.7,0) {\texttt{4\makebox[0mm]{ ,}}}; |
|
501 \node (A5) at (5.6,0) {\texttt{5\makebox[0mm]{ ,}}}; |
|
502 \node (A6) at (6.5,0) {\texttt{6\makebox[0mm]{ ,}}}; |
|
503 \node (A7) at (7.4,0) {\texttt{7\makebox[0mm]{ ,}}}; |
|
504 \node (A8) at (8.3,0) {\texttt{8)}}; |
|
505 |
|
506 \node (B0) at (1.2,-3) {\texttt{List(\,}}; |
|
507 \node (B1) at (2.0,-3) {\texttt{1\makebox[0mm]{ ,}}}; |
|
508 \node (B2) at (3.0,-3) {\texttt{4\makebox[0mm]{ ,}}}; |
|
509 \node (B3) at (4.1,-3) {\texttt{9\makebox[0mm]{ ,}}}; |
|
510 \node (B4) at (5.2,-3) {\texttt{16\makebox[0mm]{ ,}}}; |
|
511 \node (B5) at (6.3,-3) {\texttt{25\makebox[0mm]{ ,}}}; |
|
512 \node (B6) at (7.4,-3) {\texttt{36\makebox[0mm]{ ,}}}; |
|
513 \node (B7) at (8.4,-3) {\texttt{49\makebox[0mm]{ ,}}}; |
|
514 \node (B8) at (9.4,-3) {\texttt{64\makebox[0mm]{ )}}}; |
|
515 |
|
516 \draw [->,line width=1mm] (A1.south) -- (B1.north); |
|
517 \draw [->,line width=1mm] (A2.south) -- (B2.north); |
|
518 \draw [->,line width=1mm] (A3.south) -- (B3.north); |
|
519 \draw [->,line width=1mm] (A4.south) -- (B4.north); |
|
520 \draw [->,line width=1mm] (A5.south) -- (B5.north); |
|
521 \draw [->,line width=1mm] (A6.south) -- (B6.north); |
|
522 \draw [->,line width=1mm] (A7.south) -- (B7.north); |
|
523 \draw [->,line width=1mm] (A8.south) -- (B8.north); |
|
524 |
|
525 \node [red] (Q0) at (-0.5,-0.3) {\large\texttt{n}}; |
|
526 \node (Q1) at (-0.5,-0.4) {}; |
|
527 \node (Q2) at (-0.5,-2.5) {}; |
|
528 \node [red] (Q3) at (-0.5,-2.65) {\large\texttt{n\,*\,n}}; |
|
529 \draw [->,red,line width=1mm] (Q1.south) -- (Q2.north); |
|
530 |
|
531 \node [red] at (-1.5,-1.5) {\Large{}\it\textbf{map}}; |
|
532 \end{tikzpicture} |
|
533 \end{center}\bigskip |
|
534 |
|
535 \begin{lstlisting}[language=Scala, numbers=none, xleftmargin=1mm] |
|
536 List(1,2,3,4,5,6,7,8).map(n => n * n) |
|
537 \end{lstlisting} |
|
538 \end{frame} |
|
539 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
540 |
|
541 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
542 \begin{frame}[c,fragile] |
|
543 \frametitle{For-Comprehensions are maps} |
|
544 |
|
545 \begin{lstlisting}[language=Scala, numbers=none, xleftmargin=1mm] |
|
546 for (n <- List(1,2,3,4,5,6,7,8)) |
|
547 yield n * n |
|
548 |
|
549 |
|
550 // is just syntactic sugar for |
|
551 |
|
552 |
|
553 List(1,2,3,4,5,6,7,8).map(n => n * n) |
|
554 \end{lstlisting} |
|
555 |
|
556 \end{frame} |
|
557 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
558 |
|
559 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
560 \begin{frame}[c,fragile] |
|
561 \frametitle{Map (upper case)} |
|
562 |
|
563 a type, representing a key-value association datastructure\bigskip\bigskip |
|
564 |
|
565 \begin{lstlisting}[language=Scala, numbers=none, xleftmargin=-2mm] |
|
566 val ascii = |
|
567 ('a' to 'z').map(c => (c, c.toInt)) |
|
568 |
|
569 val ascii_Map = ascii.toMap |
|
570 |
|
571 ascii_Map.get('a') // -> 97 |
|
572 \end{lstlisting} |
|
573 \end{frame} |
|
574 |
|
575 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
576 \begin{frame}[c,fragile] |
|
577 \frametitle{Recursion} |
|
578 |
|
579 |
|
580 |
|
581 \begin{lstlisting}[language=Scala, numbers=none, xleftmargin=-2mm] |
|
582 def fib(n: Int) : Int = { |
|
583 if (n == 0 || n == 1) 1 |
|
584 else fib(n - 1) + fib(n - 2) |
|
585 } |
|
586 \end{lstlisting} |
|
587 \end{frame} |
|
588 |
|
589 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
590 \begin{frame}[c,fragile] |
|
591 \frametitle{Recursion} |
|
592 |
|
593 \small |
|
594 \begin{lstlisting}[language=Scala, numbers=none, xleftmargin=-4mm] |
|
595 def my_flatten(xs: List[Option[Int]]): List[Int] = |
|
596 xs match { |
|
597 case Nil => Nil |
|
598 case None :: rest => my_flatten(rest) |
|
599 case Some(v) :: rest => v :: my_flatten(rest) |
|
600 } |
|
601 \end{lstlisting} |
|
602 \end{frame} |
|
603 |
|
604 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
397 |
605 |
398 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
606 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
399 \begin{frame}[c] |
607 \begin{frame}[c] |
400 \frametitle{\begin{tabular}{c}\\[0cm]\alert{Questions?}\end{tabular}} |
608 \frametitle{\begin{tabular}{c}\\[0cm]\alert{Questions?}\end{tabular}} |
401 |
609 |