Skip to content

Instantly share code, notes, and snippets.

// That's more exploiting JavaScript closures and global scope than re-writing functions. Can't you do the same in C#?
[Test]
public void test_we_can_rewrite_variable_in_closure()
{
System.Func<string, string> fn = (a) =>
{
fn = (b) => "Goodbye " + b;
return "Hello " + a;
};
// I always thought this really interesting in JavaScript, and doesn't seem to work the same in C# or Python.
get_counter = function() {
var counter = 0;
return function() {
print(counter); counter++;
}
};
..
js> counter1 = get_counter();
@tarnacious
tarnacious / gist:775756
Created January 12, 2011 05:51
IIS7 website configuration script
# An example PowerShell script demonstrating configuring a website in IIS7
#
#
# Import the required IIS administration module
#
Import-Module WebAdministration
#
@tarnacious
tarnacious / gist:800086
Created January 28, 2011 10:28
A webmachine guestbook! Although a little hacky :)
#! src/guestbook_resource.erl
-module(guestbook_resource).
-export([init/1,
to_html/2,
process_post/2,
allowed_methods/2
]).
@tarnacious
tarnacious / gist:868056
Created March 13, 2011 12:37
Basic message based co-operative scheduling with trampolining.
using System;
using System.Collections.Generic;
public class Scheduler
{
public delegate Process Process(string message);
public static Dictionary<Guid, Process> Processes = new Dictionary<Guid,Process>();
public static Guid Spawn(Process process)
{
// C# implementation of a circular message chain of n nodes, which pass a message around m times.
// Uses a basic scheduler instead of threads.
using System;
using System.Linq;
using System.Collections.Generic;
public class MessageChain
{
public static void Main()
@tarnacious
tarnacious / messagechain.erl
Created March 14, 2011 07:13
Erlang implementation of a circular message chain of n nodes, which pass a message around m times. I wrote this over a year ago, so go easy please!
-module(messagechain).
-compile(export_all).
start() ->
Pid = messagechain:setup(3, 3),
Pid ! { message }.
setup(Times, Loops) ->
Pid = spawn(fun() -> message_node(Loops) end),
EndPid = messagechain:create_nodes(Times, Loops, Pid),
@tarnacious
tarnacious / messagechain_threads.cs
Created March 14, 2011 08:28
C# implementation of a circular message chain of n nodes, which pass a message around m times. This time with threads.
using System;
using System.Threading;
public class MessageChainThreads
{
public static AutoResetEvent CreateNode(AutoResetEvent triggerEvent, int messages)
{
var waitEvent = new AutoResetEvent(false);
CreateThread(waitEvent, triggerEvent, messages);
return waitEvent;
@tarnacious
tarnacious / gist:874052
Created March 17, 2011 09:26
Compiling SelectMany Linq without System.Linq. Also a List Monad.
using System;
using System.Collections.Generic;
public static class ExtensionMethods
{
public static IEnumerable<T> ToList<T>(this T value) {
yield return value;
}
public static IEnumerable<U> SelectMany<T, U>(this IEnumerable<T> m, Func<T, IEnumerable<U>> k)
@tarnacious
tarnacious / accumulators.erl
Created April 22, 2011 08:26
Hello World Process Chain
-module(accumulators).
-export([average/1]).
average(X) -> average(X, 0, 0).
average([H|T], Length, Sum) ->
average(T, Length + 1, Sum + H);
average([], Length, Sum) ->
Sum / Length.