Skip to content

Instantly share code, notes, and snippets.

View joshpeterson's full-sized avatar

Joshua Peterson joshpeterson

View GitHub Profile
using System;
using System.Collections.Generic;
using System.Linq;
namespace ListReverse
{
/// <summary>
/// Demonstrate two methods which reverse the elements in a list.
/// </summary>
class Program
@joshpeterson
joshpeterson / ListReverse.cpp
Created July 25, 2010 15:48
Reverse a list of string in C++
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
// Reverse the elements in a vector.
template <typename T>
std::vector<T> Reverse(std::vector<T> input)
{
std::vector<T> output;
@joshpeterson
joshpeterson / CaseSensitiveWholeWordMatch.cpp
Created August 10, 2010 11:24
This code attempts to find a given string within another string, matching the given string exactly.
// #FixMyCode
// This code attempts to find a given string within another string, matching the given string exactly.
// Can you make it more correct, more elegant, faster, or just plain better? If so, please do!
// Fork this Gist and tweet me the result @joshuampeterson.
#include <string>
#include <iostream>
bool IsWordDelimiter(char character)
{
return character == ' ' || character == '.' || character == ',' ||
@joshpeterson
joshpeterson / WaitForBackgroundThread.cs
Created August 15, 2010 12:59
This code queues some work for a background thread, then waits for that work to complete.
// #FixMyCode
// This code queues some work for a background thread, then waits for that work to complete.
// Can you make it more correct, more elegant, faster, or just plain better? If so, please do!
// Fork this Gist and tweet me the result @joshuampeterson.
using System;
using System.Collections.Generic;
using System.Threading;
namespace WaitForBackgroundThread
{
@joshpeterson
joshpeterson / SimpleStatisticsMethods.cs
Created September 23, 2010 18:33
Use a class hierarchy to create some simple statistics operations as an example in the first chapter of The D Programming Language does.
using System;
using System.Collections.Generic;
using System.Reflection;
namespace StatisticsMethods
{
interface StatisticsMethod
{
void Accumulate(double value);
void PostProcess();
@joshpeterson
joshpeterson / MatrixTest.cpp
Created January 3, 2011 19:39
Compare the performance of a matrix multiplication in JavaScript and native code.
#include <cstdlib>
#include <ctime>
#include <iostream>
// From http://stackoverflow.com/questions/2704521/generate-random-double-numbers-in-c
double fRand(double fMin, double fMax)
{
double f = (double)rand() / RAND_MAX;
return fMin + f * (fMax - fMin);
}
@joshpeterson
joshpeterson / Normal.cs
Created May 10, 2011 11:38
Normal statically-typed C# code which creates an instance of a class with one property and uses it.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
@joshpeterson
joshpeterson / Clay.cs
Created May 10, 2011 11:39
Dynamically typed C# code using Clay which creates an instance of a class with one property and uses it.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ClaySharp;
namespace ConsoleApplication2
{
class Program
{
using NUnit.Framework;
namespace TennisScoringKata
{
[TestFixture]
public class DefineTennisGame
{
private TennisGame game;
[SetUp]
@joshpeterson
joshpeterson / UnexpectedLoop.cs
Created February 11, 2012 13:13
Storing iterator state in a collection can have unexpected consequences.
using System;
using System.Collections.Generic;
using System.Linq;
namespace UnexpectedLoop
{
class Program
{
static void Main(string[] args)
{