{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "vscode": {
     "languageId": "dotnet-interactive.csharp"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div class=\"dni-plaintext\">572</div>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div class=\"dni-plaintext\">772</div>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "List<int> numbers = new List<int>(){ 1, 7, 18, 48, 28, 20, 17, 458 };\n",
    "IEnumerable<int> evenNumbers = numbers.Skip(1).Where(n => n % 2 == 0);\n",
    "DisplayExtensions.Display(evenNumbers.Sum());\n",
    "numbers.Add(200);\n",
    "DisplayExtensions.Display(evenNumbers.Sum());"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "vscode": {
     "languageId": "dotnet-interactive.csharp"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table><thead><tr><th><i>index</i></th><th>value</th></tr></thead><tbody><tr><td>0</td><td><div class=\"dni-plaintext\">4</div></td></tr><tr><td>1</td><td><div class=\"dni-plaintext\">36</div></td></tr><tr><td>2</td><td><div class=\"dni-plaintext\">144</div></td></tr></tbody></table>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "Func<int, bool> isEven = n => n % 2 == 0;\n",
    "DisplayExtensions.Display(\n",
    "    new List<int>(){3, 6, 6, 5, 2, 12, 11, 13}.Skip(2).Where(isEven).Select(n => n * n).OrderBy(n => n).ToArray());"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "vscode": {
     "languageId": "dotnet-interactive.csharp"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table><thead><tr><th><i>index</i></th><th>value</th></tr></thead><tbody><tr><td>0</td><td><div class=\"dni-plaintext\">2</div></td></tr><tr><td>1</td><td><div class=\"dni-plaintext\">3</div></td></tr><tr><td>2</td><td><div class=\"dni-plaintext\">6</div></td></tr><tr><td>3</td><td><div class=\"dni-plaintext\">5</div></td></tr><tr><td>4</td><td><div class=\"dni-plaintext\">12</div></td></tr><tr><td>5</td><td><div class=\"dni-plaintext\">13</div></td></tr><tr><td>6</td><td><div class=\"dni-plaintext\">11</div></td></tr></tbody></table>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "DisplayExtensions.Display(\n",
    "    new List<List<int>>(){ new List<int>(){2, 3, 6, 5}, new List<int>(){ 12, 13, 11 } }.SelectMany(l => l).ToArray());"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "vscode": {
     "languageId": "dotnet-interactive.csharp"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div class=\"dni-plaintext\">55</div>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div class=\"dni-plaintext\">5</div>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "List<int> numbers = new List<int>(){3, 6, 2, 5, 6, 12, 11, 13};\n",
    "Func<int, bool> isEven = n => n % 2 == 0;\n",
    "DisplayExtensions.Display(\n",
    "    numbers.Where(isEven)\n",
    "           .Select(n => n * n)\n",
    "           .Average());\n",
    "\n",
    "DisplayExtensions.Display(\n",
    "    numbers.Where(i => i > 4)\n",
    "           .OrderBy(i => i)\n",
    "           .FirstOrDefault());"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "vscode": {
     "languageId": "dotnet-interactive.csharp"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table><thead><tr><th><i>index</i></th><th>value</th></tr></thead><tbody><tr><td>0</td><td><div class=\"dni-plaintext\">6</div></td></tr><tr><td>1</td><td><div class=\"dni-plaintext\">2</div></td></tr><tr><td>2</td><td><div class=\"dni-plaintext\">6</div></td></tr><tr><td>3</td><td><div class=\"dni-plaintext\">12</div></td></tr></tbody></table>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<table><thead><tr><th><i>index</i></th><th>value</th></tr></thead><tbody><tr><td>0</td><td><div class=\"dni-plaintext\">[ 3, 5, 11, 13 ]</div></td></tr><tr><td>1</td><td><div class=\"dni-plaintext\">[ 6, 2, 12 ]</div></td></tr></tbody></table>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<table><thead><tr><th><i>index</i></th><th>value</th></tr></thead><tbody><tr><td>0</td><td><div class=\"dni-plaintext\">9</div></td></tr><tr><td>1</td><td><div class=\"dni-plaintext\">36</div></td></tr><tr><td>2</td><td><div class=\"dni-plaintext\">4</div></td></tr><tr><td>3</td><td><div class=\"dni-plaintext\">25</div></td></tr><tr><td>4</td><td><div class=\"dni-plaintext\">144</div></td></tr><tr><td>5</td><td><div class=\"dni-plaintext\">121</div></td></tr><tr><td>6</td><td><div class=\"dni-plaintext\">169</div></td></tr></tbody></table>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "[3;6;2;5;6;12;11;13]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "List<int> numbers = new List<int>(){3, 6, 2, 5, 6, 12, 11, 13};\n",
    "Func<int, bool> isEven = n => n % 2 == 0;\n",
    "\n",
    "DisplayExtensions.Display(\n",
    "    numbers.Where(isEven)\n",
    "        .ToList());\n",
    "\n",
    "DisplayExtensions.Display(\n",
    "    numbers.Distinct()\n",
    "        .GroupBy(i => isEven(i) ? \"Even\" : \"Odd\"));\n",
    "\n",
    "DisplayExtensions.Display(\n",
    "    numbers.Distinct().Select(i => i * i));\n",
    "\n",
    "DisplayExtensions.Display(\n",
    "    $\"[{String.Join(\";\", numbers.ToArray())}]\");"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "vscode": {
     "languageId": "dotnet-interactive.csharp"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Filter: 3\r\n",
      "Filter: 2\r\n",
      "Map: 2 => 4\r\n",
      "Filter: 12\r\n",
      "Map: 12 => 144\r\n",
      "Reduce: aggregate=4 next=144 => 148\r\n",
      "Filter: 5\r\n",
      "Filter: 13\r\n",
      "Filter: 11\r\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div class=\"dni-plaintext\">148</div>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "DisplayExtensions.Display(\n",
    "    new List<int>(){3, 2, 12, 5, 13, 11}.Select(\n",
    "        (i) =>\n",
    "        {\n",
    "            Console.WriteLine($\"Filter: {i}\");\n",
    "            return i;\n",
    "        })\n",
    "        .Where(i => i % 2 == 0)\n",
    "        .Select(\n",
    "            (i) =>\n",
    "            {\n",
    "                int result = i * i;\n",
    "                Console.WriteLine($\"Map: {i} => {result}\");\n",
    "                return result;\n",
    "            })\n",
    "        .Aggregate(\n",
    "            (sum, nextValue) =>\n",
    "            {\n",
    "                int result = sum + nextValue;\n",
    "                Console.WriteLine($\"Reduce: aggregate={sum} next={nextValue} => {result}\");\n",
    "                return result;\n",
    "            }));"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "vscode": {
     "languageId": "dotnet-interactive.csharp"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table><thead><tr><th><i>index</i></th><th>value</th></tr></thead><tbody><tr><td>0</td><td>This</td></tr><tr><td>1</td><td>is</td></tr><tr><td>2</td><td>test</td></tr></tbody></table>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<table><thead><tr><th><i>index</i></th><th>value</th></tr></thead><tbody><tr><td>0</td><td>This</td></tr><tr><td>1</td><td>is</td></tr><tr><td>2</td><td>test</td></tr></tbody></table>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<table><thead><tr><th><i>index</i></th><th>value</th></tr></thead><tbody><tr><td>0</td><td>This</td></tr><tr><td>1</td><td>is</td></tr><tr><td>2</td><td>test</td></tr><tr><td>3</td><td>another1</td></tr></tbody></table>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "List<string> lines = new List<string>(){\"This\", \"is\", \"a\", \"test\"};\n",
    "IEnumerable<string> linesWithEvenCount = lines.Where(line => line.Length % 2 == 0);\n",
    "DisplayExtensions.Display(linesWithEvenCount);\n",
    "DisplayExtensions.Display(linesWithEvenCount.ToArray());\n",
    "lines.Add(\"another\");\n",
    "lines.Add(\"another1\");\n",
    "DisplayExtensions.Display(linesWithEvenCount.ToArray());"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".NET (C#)",
   "language": "C#",
   "name": ".net-csharp"
  },
  "language_info": {
   "name": "C#"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}