{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "### Dependencies" ] }, { "cell_type": "code", "execution_count": 137, "metadata": { "dotnet_interactive": { "language": "fsharp" }, "polyglot_notebook": { "kernelName": "fsharp" } }, "outputs": [ { "data": { "text/html": [ "
Installed Packages
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "#r \"nuget: Plotly.NET.Interactive\"\n", "#r \"nuget: Plotly.NET.ImageExport\"" ] }, { "cell_type": "code", "execution_count": 138, "metadata": { "dotnet_interactive": { "language": "fsharp" }, "polyglot_notebook": { "kernelName": "fsharp" } }, "outputs": [], "source": [ "open System\n", "open System.Runtime.CompilerServices\n", "\n", "open Plotly.NET\n", "open Plotly.NET.ImageExport\n", "open Plotly.NET.LayoutObjects" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Utilities" ] }, { "cell_type": "code", "execution_count": 139, "metadata": { "dotnet_interactive": { "language": "fsharp" }, "polyglot_notebook": { "kernelName": "fsharp" } }, "outputs": [], "source": [ "let random = Random()" ] }, { "cell_type": "code", "execution_count": 140, "metadata": { "dotnet_interactive": { "language": "fsharp" }, "polyglot_notebook": { "kernelName": "fsharp" } }, "outputs": [], "source": [ "let staticConfig = Config.init(\n", " StaticPlot = true\n", ")\n", "\n", "type Chart with\n", " static member Display (chart: GenericChart.GenericChart) =\n", " match GenericChart.tryGetLayoutSize chart with\n", " | Some width, Some height ->\n", " chart\n", " |> Chart.withConfig staticConfig\n", " |> Chart.toSVGString(Width = width, Height = height)\n", " |> (fun chart -> chart.DisplayAs \"text/html\")\n", " | _ ->\n", " chart\n", " |> Chart.withConfig staticConfig\n", " |> Chart.toSVGString()\n", " |> (fun chart -> chart.DisplayAs \"text/html\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Chapter 1" ] }, { "cell_type": "code", "execution_count": 141, "metadata": { "dotnet_interactive": { "language": "fsharp" }, "polyglot_notebook": { "kernelName": "fsharp" } }, "outputs": [], "source": [ "type RollOfDice =\n", " | One = 0\n", " | Two = 1\n", " | Three = 2\n", " | Four = 3\n", " | Five = 4\n", " | Six = 5" ] }, { "cell_type": "code", "execution_count": 142, "metadata": { "dotnet_interactive": { "language": "fsharp" }, "polyglot_notebook": { "kernelName": "fsharp" } }, "outputs": [], "source": [ "let rollLoadedDice () =\n", " let rec loadedRoll numberOfAttempts =\n", " let roll = enum(random.Next 6)\n", " match roll with\n", " | RollOfDice.Four when numberOfAttempts <= 3 -> loadedRoll (numberOfAttempts + 1)\n", " | x -> enum(random.Next 6)\n", " loadedRoll 0" ] }, { "cell_type": "code", "execution_count": 143, "metadata": { "dotnet_interactive": { "language": "fsharp" }, "polyglot_notebook": { "kernelName": "fsharp" } }, "outputs": [], "source": [ "let rollLoadedDice () =\n", " match random.NextDouble() with\n", " | x when 0.000 <= x && x < 0.080 -> RollOfDice.Four\n", " | x when 0.080 <= x && x < 0.264 -> RollOfDice.One\n", " | x when 0.264 <= x && x < 0.448 -> RollOfDice.Two\n", " | x when 0.448 <= x && x < 0.632 -> RollOfDice.Three\n", " | x when 0.632 <= x && x < 0.816 -> RollOfDice.Five\n", " | x (* when 0.816 <= x < 1.0 *) -> RollOfDice.Six" ] }, { "cell_type": "code", "execution_count": 144, "metadata": { "dotnet_interactive": { "language": "fsharp" }, "polyglot_notebook": { "kernelName": "fsharp" } }, "outputs": [], "source": [ "let rollFairDice () =\n", " enum(random.Next 6)" ] }, { "cell_type": "code", "execution_count": 145, "metadata": { "dotnet_interactive": { "language": "fsharp" }, "polyglot_notebook": { "kernelName": "fsharp" } }, "outputs": [ { "data": { "text/html": [ "Six" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "rollLoadedDice()" ] }, { "cell_type": "code", "execution_count": 146, "metadata": { "dotnet_interactive": { "language": "fsharp" }, "polyglot_notebook": { "kernelName": "fsharp" } }, "outputs": [ { "data": { "text/html": [ "Six" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "rollFairDice()" ] }, { "cell_type": "code", "execution_count": 147, "metadata": { "dotnet_interactive": { "language": "fsharp" }, "polyglot_notebook": { "kernelName": "fsharp" } }, "outputs": [], "source": [ "let generateRawData (roll: Unit -> RollOfDice) numberOfRolls =\n", " [for _ in [1..numberOfRolls] -> roll()]" ] }, { "cell_type": "code", "execution_count": 148, "metadata": { "dotnet_interactive": { "language": "fsharp" }, "polyglot_notebook": { "kernelName": "fsharp" } }, "outputs": [], "source": [ "let chartSize = 350\n", "let layoutStyle = Layout.init(\n", " Margin = Margin.init(20, 20, 20, 20),\n", " BarGap = 0.1)\n", "\n", "let prepareDataForHistogram (rollsOfDice: RollOfDice list) =\n", " rollsOfDice\n", " |> List.sortBy int\n", " |> List.map (sprintf \"%A\")" ] }, { "cell_type": "code", "execution_count": 149, "metadata": { "dotnet_interactive": { "language": "fsharp" }, "polyglot_notebook": { "kernelName": "fsharp" } }, "outputs": [ { "data": { "text/html": [ "OneTwoThreeFourFiveSix020406080100" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "generateRawData rollLoadedDice 500\n", "|> prepareDataForHistogram\n", "|> Chart.Histogram\n", "|> Chart.withSize (chartSize, chartSize)\n", "|> Chart.withXAxisStyle (ShowGrid = true)\n", "|> Chart.withLayout layoutStyle\n", "|> Chart.withMarkerStyle(Color = Color.fromKeyword DarkCyan)\n", "|> Chart.Display" ] }, { "cell_type": "code", "execution_count": 151, "metadata": { "dotnet_interactive": { "language": "fsharp" }, "polyglot_notebook": { "kernelName": "fsharp" } }, "outputs": [ { "data": { "text/html": [ "OneTwoThreeFourFiveSix02000400060008000" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "generateRawData rollFairDice 50_000\n", "|> prepareDataForHistogram\n", "|> Chart.Histogram\n", "|> Chart.withSize (chartSize, chartSize)\n", "|> Chart.withXAxisStyle (ShowGrid = true)\n", "|> Chart.withLayout layoutStyle\n", "|> Chart.withMarkerStyle(Color = Color.fromKeyword DarkCyan)\n", "|> Chart.Display" ] } ], "metadata": { "kernelspec": { "display_name": ".NET (C#)", "language": "C#", "name": ".net-csharp" }, "language_info": { "name": "polyglot-notebook" }, "polyglot_notebook": { "kernelInfo": { "defaultKernelName": "csharp", "items": [ { "aliases": [], "name": "csharp" } ] } } }, "nbformat": 4, "nbformat_minor": 2 }