Skip to content

Instantly share code, notes, and snippets.

@atifaziz
atifaziz / ByteSizeFormatProvider.cs
Last active August 29, 2015 13:56
Custom formatter in F# & C# for formatting byte (file/disk/stream) sizes/lengths (e.g. 1.1MB, 2.3GB, 4 bytes, etc.)
using System;
// Adapted from: http://stackoverflow.com/questions/128618/c-file-size-format-provider
// Credit: http://flimflan.com/blog/FileSizeFormatProvider.aspx
public sealed class ByteSizeFormatProvider : IFormatProvider, ICustomFormatter
{
public object GetFormat(Type formatType)
{
return formatType == typeof(ICustomFormatter) ? this : null;
@atifaziz
atifaziz / WebClient.fs
Created February 20, 2014 14:23
WebClient fixes
open System
open System.Net
open System.Net.Mime
open System.Text
let opt x = if x = null then None else Some(x)
type WebClient() =
inherit System.Net.WebClient()
// Copyright (c) 2014 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 / WebQuery.bas
Created February 27, 2014 13:08
Add or update an Excel WebQuery
Function AddOrUpdateWebQuery(ByVal URL As String, Optional DontRefresh As Boolean = False) As QueryTable
Dim Connection As String: Connection = "URL;" & URL
Dim PostText As String: Dim Parts() As String
If Len(URL) > 1024 Then
' URL exceeds 1K so use HTTP POST to get around the limit
Parts = Split(URL, "?", 2)
URL = Parts(LBound(Parts))
If UBound(Parts) > LBound(Parts) Then PostText = Parts(UBound(Parts))
@atifaziz
atifaziz / Program.cs
Last active August 29, 2015 13:57
Demo of enabling optional parameters support in Jayrock
using System;
using System.Collections;
using System.Linq;
using System.Reflection;
using Jayrock;
using Jayrock.JsonRpc;
using Jayrock.Services;
static class Program
{
@atifaziz
atifaziz / XlTableFormat.ClipboardId.cs
Last active August 29, 2015 13:57
XlTable clipboard format used by Excel in DDE
#if WINFORMS
using System.Windows.Forms;
static partial class XlTableFormat
{
static int? _clipboardId;
public static int ClipboardId { get { return (_clipboardId ?? (_clipboardId = DataFormats.GetFormat("XlTable").Id)).Value; }}
}
#else
using System;
@atifaziz
atifaziz / IYielder.cs
Created March 26, 2014 12:54
Alternate IYielder Rx with exception handling
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
using System;
using System.Linq;
using System.Runtime.ExceptionServices;
using System.Security;
#if HAS_AWAIT
namespace System.Linq
{
@atifaziz
atifaziz / groupAdjacent.fs
Last active August 29, 2015 13:57
GroupAdjacent for sequences in F#
open System.Collections.Generic
let groupAdjacent projection (source : _ seq) = seq {
let rec loop (e : IEnumerator<_>) g (members : List<_>) = seq {
members.Add(e.Current)
if e.MoveNext() then
let key = projection e.Current
if key = g then
yield! loop e key members
else
@atifaziz
atifaziz / ConsoleRedirection.cs
Created April 3, 2014 08:44
Standard streams (stdin, stdout, and stderr) redirection detection
using System;
using System.Runtime.InteropServices;
// http://stackoverflow.com/a/3453272/6682
static class ConsoleRedirection
{
// http://msdn.microsoft.com/en-us/library/system.console.isinputredirected.aspx
public static bool IsInputRedirected() { return IsRedirected(StdHandle.Stdin); }
// http://msdn.microsoft.com/en-us/library/system.console.isoutputredirected.aspx
@atifaziz
atifaziz / Swapper.cs
Created April 10, 2014 13:38
State swapping
using System;
using System.Reactive.Disposables;
static partial class Swapper
{
public static Func<T, T> Create<T>(Func<T> getter, Action<T> setter)
{
if (getter == null) throw new ArgumentNullException("getter");
if (setter == null) throw new ArgumentNullException("setter");