Skip to content

Instantly share code, notes, and snippets.

View rstropek's full-sized avatar

Rainer Stropek rstropek

View GitHub Profile
@rstropek
rstropek / math.js
Created October 8, 2018 04:54
Code Snippet for Course "GitHub for Computer Science Classes"
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 / Program.cs
Last active August 19, 2020 13:47
Use case for C# 9's attributes on local functions
using System.Security.Claims;
using System.Text.Encodings.Web;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Threading.Tasks;
var tcs = new TaskCompletionSource();
var sigintReceived = false;
Console.WriteLine("Waiting for SIGINT/SIGTERM");
Console.CancelKeyPress += (_, ea) =>
{
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build-env
WORKDIR /app
# Copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore
# Copy everything else and build
COPY . ./
RUN dotnet publish -o out
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Threading;
using System.Threading.Tasks;
var host = new HostBuilder()
.ConfigureServices((hostContext, services) => services.AddHostedService<ShutdownService>())
.UseConsoleLifetime()
.Build();
@rstropek
rstropek / 01-old-style.cs
Last active November 30, 2020 11:37
C# Records
var heroes = new Hero[] {
new("Homelander", "DC", true),
new("Jessica Jones", "Marvel", false),
};
class Hero {
public Hero(string name, string universe, bool canFly) =>
(Name, Universe, CanFly) = (name, universe, canFly);
public string Name { get; }
@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;
@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 / 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 / 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,