Skip to content

Instantly share code, notes, and snippets.

@icalvo
icalvo / MergeEnumerables.cs
Last active April 30, 2022 20:41
Merge sorted `IEnumerable` (also works if they are unbounded)
public static IEnumerable<T> Merge<T, TKey>(this IEnumerable<IEnumerable<T>> lists, Func<T, TKey> selector) where TKey : IComparable<TKey>
{
var enumerators = lists.Select(x => x.GetEnumerator()).ToList();
try
{
enumerators.RemoveAll(x => !x.MoveNext());
while (enumerators.Any())
{
var minEnumerator = enumerators.MinBy(x => selector(x.Current))!;
@icalvo
icalvo / connect_vpn.cmd
Created November 15, 2021 07:59
Restart Cisco AnyConnect VPN Client
@ECHO OFF
@REM taskkill without /f so that the icon is removed.
taskkill /im vpnui.exe
net stop vpnagent
net start vpnagent
@REM vpn_credentials.txt should have two lines, first with the username and second with the password.
"C:\Program Files (x86)\Cisco\Cisco AnyConnect Secure Mobility Client\vpncli.exe" connect rackspace-vpn.frontiersin.net -s < vpn_credentials.txt
START "" "C:\Program Files (x86)\Cisco\Cisco AnyConnect Secure Mobility Client\vpnui.exe"
iwr https://chocolatey.org/install.ps1 -UseBasicParsing | iex
choco install ruby -y
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine")
$hsg = iwr -Uri https://rubygems.org/pages/download
$hsg.Links | where class -eq 'download__format' | where innerText -eq 'zip' | % {iwr -Uri $_.href -OutFile rubygems.zip}
Expand-Archive .\rubygems.zip
cd rubygems
ls | cd
ruby setup.rb
@icalvo
icalvo / InteractiveConsoleModel.fsx
Created January 25, 2017 13:40
F# Mealy FSM model for interactive console applications
// transition:('State -> 'Input -> 'State) -> isFinish:('State -> bool) -> initialState:'State -> input:seq<'Input> -> 'State list
let interactiveConsole transition isFinish initialState input =
input
|> Seq.scan transition initialState
|> Seq.takeWhile (not << isFinish)
|> Seq.toList
// unit -> seq<string>
let consoleInput =
Seq.initInfinite (fun _ -> Console.ReadLine())
@icalvo
icalvo / DependencyBuilder.cs
Last active May 27, 2016 16:32
Dependency builder that tracks circular dependencies and duplicates
using System;
using System.Collections.Generic;
using System.Linq;
namespace Icm
{
public class Dependency<T> where T : class
{
public Dependency(T node, T parent = null, IEnumerable<T> dependencies = null)
{
@icalvo
icalvo / tasks.cs
Created November 6, 2015 14:56
Waiting for all tasks but cancel them all the moment one of them fails.
[TestClass]
public class TaskTests
{
[TestMethod]
public async Task Trololo()
{
Console.WriteLine("Starting");
var tokenSource = new CancellationTokenSource();
var token = tokenSource.Token;
Task task1 = Task.Run(async () =>
@icalvo
icalvo / MigrateIds.cs
Created November 5, 2014 00:33
MongoDB: Recreate documents with new Id (e.g. change Id type)
/// <summary>
/// For each document of a collection, remove it and add it again with a new
/// Id generated by a provided function.
/// </summary>
/// <param name="connectionString">MongoClient connection string.</param>
/// <param name="databaseName">The database name</param>
/// <param name="collectionName">The collection name.</param>
/// <param name="idFunction">The Id generation function.</param>
/// <returns>List of concern results.</returns>
public static IEnumerable<WriteConcernResult> MigrateIds(
@icalvo
icalvo / GenericRetrier.cs
Last active August 29, 2015 14:08
Retry logic, generic enough to manage lots of possible cases. As an example of its genericity, a reimplementation of one method of gist https://gist.github.com/gazlu/7886208. See my comment for details.
public class ResultCheckException : Exception
{
public ResultCheckException(string message)
: base(message)
{
}
}
/// <summary>
/// Utilities for retrying a block of code a number of times.
@icalvo
icalvo / CallsCollector.cs
Last active August 29, 2015 13:56
Class for collecting calls to given targets in your C# source code, using Roslyn.
namespace Icm
{
using System.Collections.Generic;
using System.Linq;
using Roslyn.Compilers.CSharp;
/// <summary>
/// This syntax walker collects all the calls to the specified targets.
/// </summary>
public class CallsCollector : SyntaxWalker