{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# map" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "List(2, 4, 6, 8)\n", "List(2, 4, 6, 8)\n" ] }, { "data": { "text/plain": [ "\u001b[36ma\u001b[39m: \u001b[32mList\u001b[39m[\u001b[32mInt\u001b[39m] = \u001b[33mList\u001b[39m(\u001b[32m1\u001b[39m, \u001b[32m2\u001b[39m, \u001b[32m3\u001b[39m, \u001b[32m4\u001b[39m)\n", "defined \u001b[32mfunction\u001b[39m \u001b[36mf\u001b[39m" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val a = List(1, 2, 3, 4)\n", "def f(x: Int): Int = x * 2\n", "println( a.map(f) )\n", "println( a.map(_ * 2) )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# reduce" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10\n", "24\n" ] }, { "data": { "text/plain": [ "defined \u001b[32mfunction\u001b[39m \u001b[36mg\u001b[39m\n", "defined \u001b[32mfunction\u001b[39m \u001b[36mh\u001b[39m" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def g(left: Int, right: Int): Int = left + right\n", "println( a.reduce(g) )\n", "def h(left: Int, right: Int): Int = left * right\n", "println( a.reduce(h) )" ] } ], "metadata": { "kernelspec": { "display_name": "Scala", "language": "scala", "name": "scala" }, "language_info": { "codemirror_mode": "text/x-scala", "file_extension": ".scala", "mimetype": "text/x-scala", "name": "scala211", "nbconvert_exporter": "script", "pygments_lexer": "scala", "version": "2.11.11" } }, "nbformat": 4, "nbformat_minor": 2 }