Skip to content

Instantly share code, notes, and snippets.

@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 
@atifaziz
atifaziz / BuilderAsMonoid.csx
Last active February 21, 2020 07:33
A less ceremonial approach to “explicit endomorphism” presented by Mark Seemann in “Builder as a monoid”
// This script is related the following blog entry by Mark Seeman titled,
// "Builder as a monoid":
//
// https://blog.ploeh.dk/2020/02/17/builder-as-a-monoid/
//
// It demonstrates a simpler approach to avoid the ceremony of "explicit
// endomorphism" by using merely delegates. It does so by replacing
// "IEndomorphism<T>" with "Func<T, T>" and then using "+" in lieu of
// "AppendEndomorphism<T>" to achieve the same effect.
//
@atifaziz
atifaziz / pack-priv.cmd
Created January 22, 2020 13:02
Windows batch for local/private NuGet packaging of projects
@echo off
setlocal
for /f "usebackq" %%f in (`PowerShell -C "([datetimeoffset](git ls -1 --pretty=%%cI)).ToUniversalTime().ToString('yyyyMMdd''T''HHmm')"`) do set COMMIT_TIMESTAMP=%%f
if not %ERRORLEVEL%==0 exit /b %ERRORLEVEL%
call "%~dp0pack.cmd" priv-%COMMIT_TIMESTAMP%
exit /b %ERRORLEVEL%
using System;
using System.IO;
using SharpLab.Runtime;
static class X
{
public static bool HasFlag(this FileAttributes self, FileAttributes flag) =>
(self & flag) == flag;
[JitGeneric(typeof(FileAttributes))]
@atifaziz
atifaziz / clang-llvm-wasm-wasi.md
Created June 14, 2019 14:42
Compiling C to WebAssembly using Clang/LLVM & WASI

Compiling C to WebAssembly using Clang/LLVM & WASI

These are my notes. See also “[Compiling C to WebAssembly using clang/LLVM and WASI][fda]” by [Frank Denis], which was my starting point.

Assume the following example program in a file called example.c:

#include