Skip to content

Instantly share code, notes, and snippets.

View rofr's full-sized avatar

Robert Friberg rofr

View GitHub Profile
@rofr
rofr / CryptoHelper.cs
Created January 14, 2012 15:30
You're not storing passwords in plain text, right? Here's one way to do it.
public class User
{
public string UserId{ protected get; set; }
protected string PasswordHash { get; set; }
public void SetPassword(string password)
{
PasswordHash = CryptoHelper.CreateHashWithRandomSalt(password);
}
@rofr
rofr / gist:1930423
Created February 28, 2012 07:41
NonNullable example
// NonNullable, the logical inverse of Nullable. Prevents a reference type from being null.
// Implicit conversions to and from the wrapped type provide transparency.
// Usage: Wrap method arguments and/or return values with NonNullable
//
public struct NonNullable<T> where T : class
{
public readonly T Item;
public NonNullable(T item)
{
@rofr
rofr / Program.cs
Created May 4, 2012 09:01
Simple regex based string templating
class Program
{
static void Main(string[] args)
{
Template t = new Template("My {pet} {name} has fleas");
string[] keys = {"dog", "name"};
IEnumerable<string> missing, unrecognized;
bool isValid = t.Validate(keys, out missing, out unrecognized);
@rofr
rofr / gist:2660299
Created May 11, 2012 15:01
Xsharp prime number tester
<xsharp>
<set var='isPrime'><true/></set>
<set var='i'><int>3</int></set>
<set var='root'>
<call class='Math' method='Sqrt'>
<get var='numberToTest'/>
</call>
</set>
<while>
<and>
@rofr
rofr / gist:3987204
Created October 31, 2012 14:06
An example of verbatim identifiers in C#
protected T LoadFromConfigOrDefault<T>(Func<T> @default = null)
{
@default = @default ?? (() => (T)Activator.CreateInstance(typeof(T)));
string configKey = ConfigKeyFromType(typeof(T));
var configTypeName = ConfigurationManager.AppSettings[configKey];
if (!String.IsNullOrEmpty(configTypeName))
{
return InstanceFromTypeName<T>(configTypeName);
}
else
@rofr
rofr / gist:3990796
Created November 1, 2012 00:14
Protobuf attributes for JournalEntry
[ProtoContract(ImplicitFields = ImplicitFields.AllFields)]
[Serializable]
public abstract class JournalEntry
{
public readonly long Id;
public readonly DateTime Created;
public JournalEntry(long id, DateTime? created = null)
{
@rofr
rofr / gist:4034337
Created November 7, 2012 20:49
Fibonacci killer
using System;
public class FibonacciKillerKata : IFibonacciKillerKata
{
public int Fibonacci(int n)
{
if (n <= 1) return n;
else return Fibonacci(n-1) + Fibonacci(n-2);
}
}
@rofr
rofr / gist:4042429
Created November 8, 2012 22:59
protobuf-net failing test case
/*
protobuf-net failing test case. This has got to be a bug!
*/
[TestMethod]
public void IsDefined_on_empty_model_should_return_false()
{
var typeModel = RuntimeTypeModel.Create();
@rofr
rofr / di-1.cs
Created November 13, 2012 08:39
Dependency injection
public class Worker
{
ILogger _logger;
public Worker()
{
//dependency inversion take #1
var logFactory = new LogFactory();
_logger = logFactory.CreateLog();
}
@rofr
rofr / gist:4106828
Created November 18, 2012 18:53
NearestNeighborClassifier.classify()
/**
* Classify using the kNN algorithm
* @param item The item to classify
* @return The predicted class of the item
*/
public double classify(T item) {
int numItems = items.size();
// calculated distances in same order as the items list
double[] distances = new double[numItems];