Skip to content

Instantly share code, notes, and snippets.

View dvdsgl's full-sized avatar
🦋
Gliding

David Siegel dvdsgl

🦋
Gliding
View GitHub Profile
@dvdsgl
dvdsgl / rename.rb
Last active April 15, 2020 12:19
Recursively replace a string in a directory by renaming files, directories, and rewriting file contents (e.g. rename.rb . GAIM Pidgin).
#!env ruby
#
# Recursively replace a string in a directory by renaming files, directories, and rewriting file contents:
#
# $ rename.rb . GAIM Pidgin
#
# No changes are made unless -f is specified
DRY = !(ARGV.include? "-f")
@dvdsgl
dvdsgl / CachedFunc.cs
Last active December 13, 2015 17:38
Quickly create Funcs that memoize expensive computations.
// Quickly create Funcs that memoize expensive computations.
//
// In this example, ExpensiveMethod is only called once!
//
// var cached = CachedFunc.Create ((int x, string y) => x + ExpensiveMethod (y));
// for (int i = 0; i < 1000; i++)
// cached (123, "hello");
public static class CachedFunc
{
@dvdsgl
dvdsgl / cp_r.fs
Created May 22, 2013 18:28
Using F# active patterns to avoid boolean blindness.
let (|File|Directory|Missing|) path =
if Directory.Exists path then Directory
elif File.Exists path then File
else Missing
let cp_r source dest =
match source, dest with
| Missing, _ -> failwithf "missing file"
| File, File -> cp source dest
| File, Directory -> ...
scrollPoints = [
{ selector: "#window", fire: showAccountTile }
]
processScrollPoints = ->
scrollPoints = _.reject scrollPoints, ({selector, fire}) ->
if isScrolledIntoView $(selector)
fire()
true
else
class Enum
constructor: (@names...) ->
@values = [0 .. @names.length - 1]
@[name] = i for name, i in @names
getName: (i) => @names[i]
getTitle: (i) => _.str.titleize @getName i
# The next few definitions (e.g. Trial, Tiles) are for the interpretation of valid tile state
@dvdsgl
dvdsgl / Bounds.With.fs
Last active December 19, 2015 13:29
Beautifully maximizing a UIView in F# and Xamarin.iOS
// Beautifully maximizing a view
let status = 20.0F
this.Frame <- UIScreen.MainScreen.Bounds.With (Y = just status, Height = subtract status)
// FYI...
let just s _ = s
let subtract y x = x - y
// I previously had the more cryptic:
this.Frame <- UIScreen.MainScreen.Bounds.With (Y = (+) status, Height = (+) -status)
@dvdsgl
dvdsgl / Java.hs
Created August 15, 2013 18:15
A $12 million Haskell program.
import System.Exit
import Control.Monad
import Control.Concurrent
import Control.Concurrent.MVar
main = do
currentPos <- newMVar (1, 2)
forkIO $ forever $ do
(x, y) <- readMVar currentPos
Task Animate (double duration, Action animation)
{
var complete = new TaskCompletionSource<object> ();
UIView.Animate (duration, () => animation (), () => {
complete.TrySetResult (null);
});
return complete.Task;
}
async void Dismiss ()
#!/usr/bin/csharp
LoadAssembly ("/Library/Frameworks/Xamarin.Mac.framework/Versions/1.4.22/lib/mono/XamMac.dll");
using MonoMac.ScriptingBridge;
var app = SBApplication.FromBundleIdentifier ("com.apple.finder");
Console.WriteLine (app == null);
using System;
namespace Foo
{
class MainClass
{
static void Go (Action<int> act)
{
act (1);
}