Skip to content

Instantly share code, notes, and snippets.

View MiloszKrajewski's full-sized avatar

Milosz Krajewski MiloszKrajewski

  • Cambridge, UK
View GitHub Profile
@MiloszKrajewski
MiloszKrajewski / TypeExtensions.cs
Last active October 18, 2024 13:42
TypeExtensions
internal static class TypeExtensions
{
/// <summary>
/// Type distance cache. It should Concurrent dictionary but it is not available
/// on all flavors of Portable Class Library.
/// </summary>
private static readonly ConcurrentDictionary<(Type, Type), int> TypeDistanceMap = new();
/// <summary>Checks if child type inherits (or implements) from parent.</summary>
/// <param name="child">The child.</param>
@MiloszKrajewski
MiloszKrajewski / Pool.cs
Created March 15, 2020 15:10
Simple object pool
public class Pool<T>
{
private readonly ConcurrentQueue<T> _queue;
private readonly Func<T> _create;
private readonly Action<T> _reset;
private readonly Action<T> _destroy;
private int _freeSlots;
public Pool(Func<T> create, Action<T> reset, Action<T> destroy, int size)
{
@MiloszKrajewski
MiloszKrajewski / IncludeOtherFolder.targets
Last active March 10, 2020 00:18
MsBuild tricks (does NOT work with signing)
<ItemGroup>
<Content Include="../OtherLib/Internal/**/*.*">
<Link>Shared/Internal/%(RecursiveDir)%(Filename)%(Extension)</Link>
</Content>
</ItemGroup>
@MiloszKrajewski
MiloszKrajewski / run-postgres-here.ps1
Last active November 26, 2019 12:34
Run Postgres server here
$root = $PSScriptRoot
$version = "12.0-1"
$pg_bin = "$root/bin/pgsql/bin"
$pg_exe = "$pg_bin/postgres.exe"
function Decrypt-SecureString([SecureString] $secure) {
return [System.Runtime.InteropServices.marshal]::PtrToStringAuto([System.Runtime.InteropServices.marshal]::SecureStringToBSTR($secure))
}
function UnzipUrl([string] $url, [string] $outpath) {
/// <summary>
/// Helper class replacing Dapper.
/// </summary>
internal static class Dapperito
{
public static DbCommand CreateCommand(this DbConnection connection, string sql)
{
var command = connection.CreateCommand();
command.CommandText = sql;
return command;
@MiloszKrajewski
MiloszKrajewski / between.fs
Last active April 16, 2019 10:46
Function to create string "between" two other strings
open System
let rec between a z =
if a = z then a
elif a > z then between z a
else
let rec between a z acc =
match a, z with
| [], z -> between ['a'] z acc
| a, [] -> between a ['z'] acc
@MiloszKrajewski
MiloszKrajewski / bootstrap.fsx
Last active November 16, 2020 12:35
Self bootstrapping F# script
open System.Diagnostics
let root = __SOURCE_DIRECTORY__
let debug = false
let exec directory executable arguments =
let info =
ProcessStartInfo(
executable, arguments,
UseShellExecute = false, CreateNoWindow = not debug, WorkingDirectory = directory)

define actor:

  • var props = Props.FromProducer(() => actor);
  • var props = Props.FromFunc(context => { ... });

configure actor:

  • props.WithDispatcher(dispatcher);
  • props.WithMailbox(() => mailbox);
  • props.WithSupervisor(strategy);
  • props.WithSenderMiddleware(middleware[]);
  • props.WithReceiveMiddleware(middleware[]);
@MiloszKrajewski
MiloszKrajewski / quick-netcore-console-app.cs
Last active November 16, 2020 14:47
Quick and dirty .NET core console application
/*
Microsoft.Extensions.DependencyInjection
Microsoft.Extensions.Logging
Microsoft.Extensions.Configuration
*/
using System;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
@MiloszKrajewski
MiloszKrajewski / CloneRequest.cs
Created July 10, 2018 12:29
Close HttpRequest
private static async Task<HttpRequestMessage> CloneRequest(HttpRequestMessage request)
{
var clone = new HttpRequestMessage(request.Method, request.RequestUri);
if (request.Content != null)
{
var stream = await request.Content.ReadAsStreamAsync();
if ((stream.Length > 0 || request.Content.Headers.Any())
&& clone.Method != HttpMethod.Get)
{