Skip to content

Instantly share code, notes, and snippets.

View MiloszKrajewski's full-sized avatar

Milosz Krajewski MiloszKrajewski

  • Cambridge, UK
View GitHub Profile
open System.Collections.Generic
// NOTE: it running in Fable/REPL you need "queue-polyfill.fs"
let bfs idof fanout node =
let queue = Queue([node])
let visited = HashSet()
// DSL
let enqueue = queue.Enqueue
@MiloszKrajewski
MiloszKrajewski / bloxorz.fs
Last active April 11, 2018 23:27
Bloxorz for Fable REPL (fable.io/repl)
namespace Bloxorz
module BFS =
open System.Collections.Generic
type Queue<'a>(values: 'a seq) =
let values = ResizeArray(values)
member x.Count = values.Count
member x.Enqueue value = values.Add(value)
member x.Dequeue () = let result = values.[0] in values.RemoveAt(0); result
@MiloszKrajewski
MiloszKrajewski / tryParse.fsx
Last active March 16, 2018 13:28
Universal tryParse
open System
open System.IO
let inline autoParse<'a when 'a : (static member TryParse : string * byref<'a> -> bool)> text =
let mutable result = Unchecked.defaultof<'a>
match (^a : (static member TryParse : string * byref<'a> -> bool) (text, &result)) with
| true -> Some result | _ -> None
// we don't need these two, they are just for readability
let orFail = Option.get
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard1.5</TargetFramework>
<RootNamespace>Proto.Persistence.MySql</RootNamespace>
</PropertyGroup>
<PropertyGroup>
<Version>0.1.14</Version>
<AssemblyVersion>0.1.14</AssemblyVersion>
<FileVersion>0.1.14</FileVersion>
Add-Type -AssemblyName System.IO.Compression.FileSystem
function Unzip([string] $zipfile, [string] $outpath) {
[System.IO.Compression.ZipFile]::ExtractToDirectory($zipfile, $outpath)
}
function Download([string] $url, [string] $path) {
$temp = [System.IO.Path]::GetTempFileName()
Invoke-WebRequest "$url" -OutFile "$temp"
Unzip "$temp" "$path"
@MiloszKrajewski
MiloszKrajewski / protoc.ps1
Last active June 3, 2021 12:27
protoc stub with PowerShell
Param(
[string] $root = "."
)
$protoc_folder = "$PSScriptRoot/.tools/protoc"
$protoc_executable = "$protoc_folder/bin/protoc.exe"
$protoc_includes = "$protoc_folder/include"
$protoc_version = "3.6.0"
$protoc_url = "https://github.com/google/protobuf/releases/download/v$protoc_version/protoc-$protoc_version-win32.zip"
private static IObservable<string> ObserveConsole() =>
Observable.Create<string>(
observer => {
var input = new StreamReader(System.Console.OpenStandardInput());
return Observable
.FromAsync(() => input.ReadLineAsync())
.Repeat()
.TakeWhile(line => line != null)
.Subscribe(observer!);
}).Publish().RefCount();
private static IObservable<T> Observe<T>(
this NetMQSocket socket,
ISocketPollableCollection poller,
Func<NetMQSocket, T> reader)
{
IEnumerable<T> ReadMany(NetMQSocket sckt)
{
while (sckt.HasIn)
yield return reader(sckt);
}
rem This must be executed as administrator
rem it requires double % if ran as batch file, but single % if copying-pasting lines
assoc .ps1=PowerShell.Script
ftype PowerShell.Script=powershell.exe -ExecutionPolicy RemoteSigned -File %%1 %%*
setx PATHEXT %PATHEXT%;.PS1
rem This is just a test and may require clicking a checkbox (but only first time)
echo Write-Host 'Hello from PS1' > %temp%\test.ps1
@MiloszKrajewski
MiloszKrajewski / ProcessHelper.fs
Last active December 6, 2017 08:37
Simple process helper
#r "System.Management.dll"
module Process =
open System
open System.Diagnostics
open System.Threading.Tasks
open System.Management
let fix (proc: Process option) =
match proc with