{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Imperative vs Declarative functions" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "var numbers = [4, 10, 0, 27, 42, 17, 15, -6, 58]\n", "var faves = [];\n", "var magicNumber = 0;\n", "\n", "pickFavoriteNumbers();\n", "calculateMagicNumber();\n", "outputMsg();\n", "\n", "////////////////////////\n", "function calculateMagicNumber() {\n", " for (let fave of faves) {\n", " magicNumber = magicNumber + fave;\n", " }\n", "}\n", "\n", "function pickFavoriteNumbers(){\n", " for (let num of numbers) {\n", " if (num >= 10 && num <= 20) {\n", " faves.push( num );\n", " }\n", " }\n", "}\n", "\n", "function outputMsg() {\n", " var msg = `The magic number is: ${magicNumber}`;\n", " console.log(msg);\n", "}" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Functional Solve Below:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "var sumOnlyFavorites = FP.compose ( [\n", " FP.filterReducer( FP.gte( 10 ) ),\n", " FP.filterReducer( FP.lte( 20 ) )\n", "])( sum );\n", "\n", "var printMagicNumber = FP.pipe ( [\n", " FP.reduce( sumOnlyFavorites, 0 ),\n", " constructMsg,\n", " console.log\n", "])\n", "\n", "var numbers = [4, 10, 0, 27, 42, 17, 15, -6, 58];\n", "\n", "printMagicNumber( numbers );\n", "\n", "function sum(x,y) { return x + y }\n", "function constructMsg(v) { return `The magic number is: ${v}`};" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "interleaving spread operator with multiple arguments" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4\n", "4\n" ] } ], "source": [ "function foo (...args) {\n", " console.log( args[3] );\n", "}\n", "\n", "var arr = [1, 2, 3, 4, 5];\n", "\n", "foo( ...arr );\n", "\n", "var arr = [2];\n", "\n", "foo (1, ...arr, 3, ...[4,5]); " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "destructuring arrays in args" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "function foo (...args) {\n", " console.log( args.length );\n", "}\n", "\n", "function foo([x,y,...args]=[]){\n", " \n", "}\n", "foo([1,2,3]);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " collecting multiple values into an array to return" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "42\n" ] } ], "source": [ "function foo(){\n", " var retValue1 = 11;\n", " var retValue2 = 31;\n", " return [ retValue1, retValue2];\n", "}\n", "\n", "var [ x,y ] = foo();\n", "console.log(x + y);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "early returns and single return" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n", "2\n", "8\n", "13\n" ] } ], "source": [ "function foo (x) {\n", " if(x > 10) return x + 1;\n", " \n", " var y = x / 2;\n", " \n", " if(y > 3) {\n", " if(x % 2 == 0) return x;\n", " }\n", " \n", " if (y > 1) return y;\n", " \n", " return x;\n", "}\n", "\n", "console.log(foo(2));\n", "console.log(foo(4));\n", "console.log(foo(8));\n", "console.log(foo(12));" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "4\n", "6\n" ] } ], "source": [ "function foo(x) {\n", " var retValue;\n", " \n", " if(retValue == undefined && x > 10) {\n", " retValue = x + 1;\n", " }\n", " \n", " var y = x / 2;\n", " \n", " \n", " if( y > 3){\n", " if (retValue = undefined && x % 2 == 0) {\n", " retValue = x\n", " }\n", " }\n", " \n", " if(retValue == undefined && 1 < y < 3) {\n", " retValue = y\n", " }\n", " \n", " if(retValue == undefined) {\n", " retValue = x;\n", " }\n", " \n", " return retValue;\n", "}\n", "\n", "console.log(foo(2));\n", "console.log(foo(4));\n", "console.log(foo(8));\n", "console.log(foo(12));" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " implicit vs explicit output" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "11" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "var y;\n", "\n", "function f(x) {\n", " y = (2 * Math.pow(x,2)) + 3;\n", "}\n", "\n", "f(2);\n", "\n", "y;" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "11" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "function f(x) {\n", " return ( 2 * Math.pow(x,2)) + 3;\n", "}\n", "\n", "var y = f(2);\n", "\n", "y;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "--------" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "avoiding side effects" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ 1, 3, 9, 27, <1 empty item>, 84 ]\n", "[ 1, 3, 9, 27, 0, 84 ]\n" ] } ], "source": [ "function sum (list) {\n", " var total = 0;\n", " for (let i=0; i < list.length; i++) {\n", " if(!list[i]) list[i] = 0;\n", " \n", " total = total + list[i];\n", " }\n", " return total;\n", "}\n", "\n", "var nums = [1, 3, 9 ,27, , 84];\n", "console.log(nums);\n", "sum(nums);\n", "console.log(nums);" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "higher order functions" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [] } ], "metadata": { "kernelspec": { "display_name": "Javascript (Node.js)", "language": "javascript", "name": "javascript" }, "language_info": { "file_extension": ".js", "mimetype": "application/javascript", "name": "javascript", "version": "10.15.3" } }, "nbformat": 4, "nbformat_minor": 2 }