Skip to content

Instantly share code, notes, and snippets.

View rstropek's full-sized avatar

Rainer Stropek rstropek

View GitHub Profile
using System;
ReadOnlySpan<Vector2d> vectors = stackalloc Vector2d[] { new(1d, 1d), new(2d, 2d), };
Console.WriteLine(AddAll(vectors));
static T AddAll<T>(ReadOnlySpan<T> addables) where T: IAddable<T>
{
var result = T.Zero;
foreach (var a in addables) result += a;
return result;
using System;
using System.Text;
var p1 = new Point(1d, 2d);
// p1.X = 3d; // This does not work because readonly records are immutable
Console.WriteLine(p1);
// Note: At the time of writing, deconstruct can be compiled on sharplab.io, but cannot be executed.
// If you want to run this code on sharplab.io, comment the following statement.
var (x, y) = p1; // Deconstruction works similar to record classes.
using System;
var v1 = new Vector2d(1d, 2d);
v1.X = 3d; // This works because get and set are generated by default
Console.WriteLine(v1.X);
Console.WriteLine(v1); // record structs implement ToString
var v2 = v1 with { X = 4d }; // We can use the with keyword
Console.WriteLine(v2.X);
using System;
var p = new Person("Foo", "Bar", 42);
// The following line does not work because records are immutable
// p.LastName = "Baz";
Console.WriteLine(p.FirstName);
var b = new Product("Bike", "Mountainbike", 499m);
Console.WriteLine(b.Name);
@rstropek
rstropek / MainWindow.xaml
Last active May 20, 2021 08:22
Machine Monitor Sample (XAML)
<Window x:Class="MachineMonitor.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MachineMonitor"
mc:Ignorable="d"
Title="Machine Monitor" Height="450" Width="800">
<Window.Resources>
<Style TargetType="local:TemperatureChart">
@rstropek
rstropek / 010-MainWindow.xaml
Last active April 14, 2021 13:36
XAML Intro
<Window x:Class="HelloXaml.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:HelloXaml"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<!--
+=== Will become an object of this class
@rstropek
rstropek / app.module.ts
Last active February 24, 2021 19:56
MSAL 2 Angular
import { HttpClientModule, HTTP_INTERCEPTORS } from "@angular/common/http";
import { NgModule } from "@angular/core";
import { FormsModule } from "@angular/forms";
import { BrowserModule } from "@angular/platform-browser";
import {
MsalInterceptor,
MsalModule,
MsalService,
MSAL_INSTANCE,
MSAL_INTERCEPTOR_CONFIG,
@rstropek
rstropek / math.js
Created January 31, 2021 13:44
math.js (failing test)
exports.add = function (x, y) {
return x + y;
}
exports.sub = function (x, y) {
return x - y;
}
exports.sumOfPositiveNumbers = function (upperLimitInclusive) {
let result = 0;
@rstropek
rstropek / 010-constant-pattern.cs
Last active June 17, 2021 12:47
C# Pattern Matching Inside Out
using static System.Console;
// We can simply replace == with is. Doesn't make much sense, though.
int someNumber = 42;
if (someNumber is 42) WriteLine("Some number is 42");
// Situation changes when we change the type to object.
object something = 42;
// The following statement would not work (you cannot use == with
@rstropek
rstropek / iterator.go
Created December 1, 2020 10:07
Generic iterator in Go
package main
import (
"fmt"
"strings"
)
// Generic iterator function type
type iteratorFunc[T any] func() *T;