Skip to content

Instantly share code, notes, and snippets.

View rofr's full-sized avatar

Robert Friberg rofr

View GitHub Profile
// ----------------------------------------------------------------------------------------------
// display control buttons to right of each list item on hover
// ----------------------------------------------------------------------------------------------
var buttonMarkup = "<span class='btn-group'><a href='#' class='drag-handle btn'>" +
"<i class='icon-resize-vertical'></i></a>" +
"<a href='#' class='btn remove-item'><i class='icon-trash'></i></a></span>";
$("#sortable")
.on("mouseenter", "li", function(e){$(this).append($(buttonMarkup))})
.on("mouseleave", "li", function(e){$(this).find("span.btn-group").remove()}
@rofr
rofr / gist:5540634
Created May 8, 2013 14:02
This has got to be madly wrong, right?
using (var service = new PollServiceClient())
{
service.PollAsync(request, Poll_Completed);
}
@rofr
rofr / gist:5848438
Created June 24, 2013 07:59
Just discovered the dotnet BigInteger type. Nice use of implicit type conversion and operator overloading.
/// <summary>
/// Primality test of mersenne prime using the lucas-lehmer test
/// </summary>
/// <param name="p">A prime number</param>
/// <returns>true if 2^p-1 is prime, otherwise false</returns>
public static bool IsMersennePrime(int p)
{
BigInteger mp = BigInteger.Pow(2, p) - 1;
BigInteger n = 4;
for (int i = 1; i < p - 1; i++)
@rofr
rofr / ImmutableList[T].cs
Last active December 24, 2015 08:39
Some immutability stuff for informatorbloggen
public class ImmutableList<T> : IEnumerable<T>
{
List<T> _items;
public ImmutableList()
{
_items = new List<T>();
}
private ImmutableList(List<T> items)
@rofr
rofr / SquashPlayer.cs
Created October 1, 2013 00:24
Immutable domain entity
public class SquashPlayer
{
public readonly string Name;
public readonly int Ranking;
public SquashPlayer(string name, int ranking)
{
Name = name;
Ranking = ranking;
}
@rofr
rofr / program.cs
Last active December 24, 2015 08:39
Immutability code for informatorbloggen
public static void Main(string[] args)
{
string[] input = {"Ramy", "Gregory", "Willstrop",
"Matthew", "El Shorbagy", "Drakenberg", "Hult", "Friberg"};
var players = new ImmutableList<SquashPlayer>();
var rnd = new Random();
var writer = new Thread( () => {
int i = 1;
@rofr
rofr / dual.cs
Created October 2, 2013 09:22
Which design is prefereble? Derive from a subclass and override an abstract method or derive from same class and overload exactly one of 2 possible virtual methods?
public abstract class Command<M> where M : Model
{
public virtual void Execute(M modelIn, out M modelOut)
{
modelOut = null;
throw new NotImplementedException();
}
public virtual void Execute(M Model)
{
@rofr
rofr / ActiveMqTopicClient.cs
Created October 18, 2013 12:13
This is an ActiveMq pub/sub wrapper. Instances are running within multiple IIS worker processes on multiple machines. I'm not receiving messages on all clients. Can you spot any obvious errors using the NMS API? For ex connection, session, producer and consumer lifecycle, lost connection issues etc. The same topic and brokerUri is used on all se…
public class ActiveMqTopicClient : IDisposable
{
public delegate void MessageReceivedHandler(IMessage message);
public event MessageReceivedHandler MessageReceived;
readonly IConnection _connection;
readonly ISession _session;
readonly IMessageProducer _producer;
readonly IMessageConsumer _consumer;
bool _isDisposed = false;
@rofr
rofr / activemqTest.cs
Created October 18, 2013 14:42
Passing test confirming null selector doesn't block messages and that nolocal means local either to the connection or session objects.
[TestMethod]
public void separate_client_in_same_process_receives_message_using_null_selector_and_ignoring_local_messages()
{
var topic = Guid.NewGuid().ToString();
var host = "tcp://localhost:61616";
var topicClient1 = new ActiveMqTopicClient(topic, host, ignoreLocal: true);
var topicClient2 = new ActiveMqTopicClient(topic, host, ignoreLocal: true);
var eventsRecieved = 0;
var waitHandle = new ManualResetEvent(initialState: false);
@rofr
rofr / gist:8688105
Last active August 29, 2015 13:55
Example OrigoDB Server Razor Query
@using Todo.Core
@{
var listNames = Query(m => m.Lists.Select(list => list.Name).ToArray());
}
<ul>
@foreach (var name in listNames)
{
var tasks = Query(db => db.Lists.Single(list => list.Name == name)
.Tasks.Select(task => task.Title).ToArray());