Skip to content

Instantly share code, notes, and snippets.

@atifaziz
atifaziz / ByteSizeFormatProvider.cs
Last active Aug 29, 2015
Custom formatter in F# & C# for formatting byte (file/disk/stream) sizes/lengths (e.g. 1.1MB, 2.3GB, 4 bytes, etc.)
View ByteSizeFormatProvider.cs
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;
View WebClient.fs
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()
View svnarcst.fsx
// 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 Feb 27, 2014
Add or update an Excel WebQuery
View WebQuery.bas
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 Aug 29, 2015
Demo of enabling optional parameters support in Jayrock
View Program.cs
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 Aug 29, 2015
XlTable clipboard format used by Excel in DDE
View XlTableFormat.ClipboardId.cs
#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 Mar 26, 2014
Alternate IYielder Rx with exception handling
View IYielder.cs
// 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 Aug 29, 2015
GroupAdjacent for sequences in F#
View groupAdjacent.fs
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 Apr 3, 2014
Standard streams (stdin, stdout, and stderr) redirection detection
View ConsoleRedirection.cs
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
View Swapper.cs
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");