Skip to content

Instantly share code, notes, and snippets.

@atifaziz
atifaziz / git-branch-orphan.ps1
Last active September 7, 2021 09:40
Create an orphan branch without checking it out
git stash # 1. save index changes
git rm -r --cached . # 2. empty the index
git branch orphan ( # 5. create the orphan branch
git commit-tree -m 'Empty commit' ( # 4. commit the empty tree
git write-tree)) # 3. write the empty index
git reset HEAD # 6. revert emptying of the index
git stash pop --index # 7. restore stashed changes
@atifaziz
atifaziz / EmptySet.cs
Created August 2, 2021 06:55
An empty set implementation
#region Copyright (c) 2021 Atif Aziz. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
@atifaziz
atifaziz / NCrontabFieldsDemo.csproj
Created November 7, 2020 15:29
NCrontab demo showing direct use of CrontabField
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<RootNamespace></RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="ncrontab" Version="3.3.1" />
@atifaziz
atifaziz / Util.linq
Last active August 27, 2020 14:47
LINQPadless helper for LINQPad's Util 📄 http://share.linqpad.net/age6lg.linq
<Query Kind="Program">
<Namespace>System.Globalization</Namespace>
<RemoveNamespace>System.Collections</RemoveNamespace>
<RemoveNamespace>System.Data</RemoveNamespace>
<RemoveNamespace>System.Diagnostics</RemoveNamespace>
<RemoveNamespace>System.Linq.Expressions</RemoveNamespace>
<RemoveNamespace>System.Reflection</RemoveNamespace>
<RemoveNamespace>System.Text</RemoveNamespace>
<RemoveNamespace>System.Text.RegularExpressions</RemoveNamespace>
<RemoveNamespace>System.Threading</RemoveNamespace>
@atifaziz
atifaziz / WebLinqData.linq
Last active September 20, 2020 08:47
LINQPad helper for WebLINQ
<Query Kind="Program">
<NuGetReference Version="4.0.0">System.Reactive</NuGetReference>
<NuGetReference Version="4.5.0">System.Text.Encoding.CodePages</NuGetReference>
<NuGetReference Version="1.0.0-alpha-20200416" Prerelease="true">WebLinq</NuGetReference>
<Namespace>System.CodeDom.Compiler</Namespace>
<Namespace>System.Globalization</Namespace>
<Namespace>System.Net</Namespace>
<Namespace>System.Net.Http</Namespace>
<Namespace>System.Net.Http.Headers</Namespace>
<Namespace>System.Reactive</Namespace>
@atifaziz
atifaziz / CowSay.linq
Last active August 26, 2020 21:12
dotnet-coway as a LINQPad C# Query Program (http://share.linqpad.net/9bfa4d.linq)
<Query Kind="Program">
<NuGetReference Version="1.0.0">Kurukuru</NuGetReference>
<NuGetReference Version="2.2.4">McMaster.Extensions.CommandLineUtils</NuGetReference>
<NuGetReference Version="11.0.2">Newtonsoft.Json</NuGetReference>
<Namespace>Kurukuru</Namespace>
<Namespace>Newtonsoft.Json</Namespace>
<Namespace>McMaster.Extensions.CommandLineUtils</Namespace>
<Namespace>System.Threading.Tasks</Namespace>
<Namespace>System.Net.Http</Namespace>
</Query>
@atifaziz
atifaziz / test.csx
Last active August 17, 2020 12:44
FileOptions.DeleteOnCode test for my StackOverflow answer https://stackoverflow.com/a/63451070/6682
using System.IO;
var fileName = Path.GetRandomFileName();
Console.WriteLine($"File name: {fileName}");
Console.WriteLine($"Exists? {File.Exists(fileName)}");
using (var fs = File.Create(fileName, 4096, FileOptions.DeleteOnClose))
Console.WriteLine($"Exists? {File.Exists(fileName)}");
Console.WriteLine($"Exists? {File.Exists(fileName)}");
using System;
public class C {
public void M((bool, object) x) {
if (x is (true, {} obj)) {
Console.WriteLine(obj);
}
}
public void N(bool f, object obj) {
if (f) {
@atifaziz
atifaziz / sorting.js
Last active May 28, 2020 12:54
Two techniques demonstrating sorting time strings
times = [ '12:00', '10:00', '12:15', '09:30' ]
// simple way
// relies on string comparison operators (`<` and `>`)
sorted = [...times].sort((a, b) => a > b ? 1 : a < b ? -1 : 0)
// sorted = [ '09:30', '10:00', '12:00', '12:15' ]
// complicated way
// - split time on ":" into two parts: hours and minutes
@atifaziz
atifaziz / README.md
Last active February 28, 2020 06:33
Demo that RyuJIT eliminates unreachable "typeof(T)==typeof(…)" branches in generic instantiations of a method

What's this?

Found an interesting [comment] in SqlDataReader's source code:

// this block of type specific shortcuts uses RyuJIT jit behaviours to achieve fast implementations of the primitive types
// RyuJIT will be able to determine at compilation time that the typeof(T)==typeof(<primitive>) options are constant
// and be able to remove all implementations which cannot be reached. this will eliminate non-specialized code for