Skip to content

Instantly share code, notes, and snippets.

@cgranade
Last active March 14, 2020 20:23
Show Gist options
  • Save cgranade/09dabfe1cc2e650cd88d2cfc98185791 to your computer and use it in GitHub Desktop.
Save cgranade/09dabfe1cc2e650cd88d2cfc98185791 to your computer and use it in GitHub Desktop.
Values in Q#
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Working with Strings"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<script type=\"text/javascript\">// ensure `requirejs` is available\r\n",
"if ((typeof (requirejs) !== typeof (Function)) || (typeof (requirejs.config) !== typeof (Function))) \r\n",
"{\r\n",
" let requirejs_script = document.createElement('script');\r\n",
" requirejs_script.setAttribute('src', 'https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.6/require.min.js');\r\n",
" requirejs_script.setAttribute('type', 'text/javascript');\r\n",
" requirejs_script.onload = function () {\r\n",
" loadDotnetInteractiveApi();\r\n",
" };\r\n",
"\r\n",
" document.getElementsByTagName('head')[0].appendChild(requirejs_script);\r\n",
"}\r\n",
"else {\r\n",
" loadDotnetInteractiveApi();\r\n",
"}\r\n",
"\r\n",
"async function probeAddresses(probingAddresses) {\r\n",
" if (Array.isArray(probingAddresses)) {\r\n",
" for (let i = 0; i < probingAddresses.length; i++) {\r\n",
" \r\n",
" let rootUrl = probingAddresses[i];\r\n",
"\r\n",
" if (!rootUrl.endsWith('/')) {\r\n",
" rootUrl = `${rootUrl}/`;\r\n",
" }\r\n",
"\r\n",
" let response = await fetch(`${rootUrl}discovery`, {\r\n",
" method: 'POST',\r\n",
" headers: {\r\n",
" 'Content-Type': 'text/plain'\r\n",
" },\r\n",
" body: probingAddresses[i]\r\n",
" });\r\n",
"\r\n",
" if (response.status == 200) {\r\n",
" return rootUrl;\r\n",
" }\r\n",
" }\r\n",
" }\r\n",
"}\r\n",
"\r\n",
"function loadDotnetInteractiveApi() {\r\n",
" probeAddresses([\"http://10.12.22.56:41205/\"])\r\n",
" .then((root) => {\r\n",
" // use probing to find host url and api resources\r\n",
" // ensure interactive helpers are loaded\r\n",
" let interactiveHelperElement = document.getElementById('dotnet-interactive-script-loaded');\r\n",
" if (!interactiveHelperElement) {\r\n",
" let apiRequire = requirejs.config({\r\n",
" context: 'dotnet-interactive.109.41205',\r\n",
" paths: {\r\n",
" dotnetInteractive: `${root}resources/dotnet-interactive`\r\n",
" }\r\n",
" });\r\n",
" apiRequire(['dotnetInteractive'],\r\n",
" function (api) {\r\n",
" api.init(window);\r\n",
" let sentinelElement = document.createElement('script');\r\n",
" sentinelElement.setAttribute('id', 'dotnet-interactive-script-loaded');\r\n",
" sentinelElement.setAttribute('type', 'text/javascript');\r\n",
" document.getElementsByTagName('head')[0].appendChild(sentinelElement);\r\n",
" },\r\n",
" function (error) {\r\n",
" console.log(error);\r\n",
" }\r\n",
" );\r\n",
" }\r\n",
" })\r\n",
" .catch(error => {console.log(error);});\r\n",
"}</script>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"<table><thead><tr><th>Item1</th><th>Item2</th></tr></thead><tbody><tr><td>aBc</td><td>abc</td></tr></tbody></table>"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"var s = \"abc\";\n",
"var t = s;\n",
"s = s.Remove(1, 1).Insert(1, \"B\");\n",
"(s, t)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Working with Arrays"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<table><thead><tr><th>Item1</th><th>Item2</th></tr></thead><tbody><tr><td>[ 1, 4, 6 ]</td><td>[ 1, 4, 6 ]</td></tr></tbody></table>"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"var a = new[] {2, 4, 6};\n",
"var b = a;\n",
"a[0] = 1;\n",
"(a, b)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Working with the ImmutableList class"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"using System.Collections.Immutable;"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<table><thead><tr><th><i>index</i></th><th>value</th></tr></thead><tbody><tr><td>0</td><td>2</td></tr><tr><td>1</td><td>4</td></tr><tr><td>2</td><td>6</td></tr></tbody></table>"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"var a = ImmutableList<int>\n",
" .Empty\n",
" .Add(2)\n",
" .Add(4)\n",
" .Add(6);\n",
"a"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<table><thead><tr><th>Item1</th><th>Item2</th></tr></thead><tbody><tr><td>[ 2, 4, 6 ]</td><td>[ 1, 4, 6 ]</td></tr></tbody></table>"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"var b = a.SetItem(0, 1);\n",
"(a, b)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": ".NET (C#)",
"language": "C#",
"name": ".net-csharp"
},
"language_info": {
"file_extension": ".cs",
"mimetype": "text/x-csharp",
"name": "C#",
"pygments_lexer": "csharp",
"version": "8.0"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
FROM mcr.microsoft.com/quantum/iqsharp-base:0.10.2002.2610
ENV IQSHARP_HOSTING_ENV=dev-to-sckaiser-arrays
# Make sure the contents of our repo are in ${HOME}.
# These steps are required for use on mybinder.org.
USER root
COPY . ${HOME}
RUN chown -R ${USER} ${HOME}
# While root, upgrade the .NET Core SDK.
RUN apt-get -y update && \
apt-get -y install dotnet-sdk-3.1 && \
apt-get clean && rm -rf /var/lib/apt/lists/
# Finish by dropping back to the notebook user and installing support needed
# for C# notebooks.
USER ${USER}
RUN dotnet tool install --global \
--add-source "https://dotnet.myget.org/F/dotnet-try/api/v3/index.json" \
Microsoft.dotnet-interactive && \
dotnet interactive jupyter install
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment