Skip to content

Instantly share code, notes, and snippets.

public static IEnumerable<string> GetPermutations(string s)
{
if (s.Count() > 1)
return from ch in s
from permutation in GetPermutations(s.Remove(s.IndexOf(ch), 1))
select string.Format("{0}{1}", ch, permutation);
else
return new string[] { s };
}
public static List<string> Permutations(string s)
{
if (s.Length == 1)
{
return new List<string> { s };
}
List<string> permutations = new List<string>();
foreach (char c in s)
@simpleprogrammer-shared
simpleprogrammer-shared / lambda-extensible-fizzbuzz2-with-linq-1.cs
Created July 14, 2016 20:13
Lambda Extensible FizzBuzz 2.0 With LINQ
public static void FizzBuzz()
{
Dictionary<Func<int, bool>, Func<int, string>> rules = new Dictionary<Func<int, bool>, Func<int, string>>();
rules.Add(x => x % 3 == 0, x => "fizz");
rules.Add(x => x % 5 == 0, x => "buzz");
rules.Add(x => x % 5 != 0 && x % 3 != 0, x => x.ToString());
rules.Add(x => true, x => "\n");
var output = from n in Enumerable.Range(1, 100)
from f in rules
@simpleprogrammer-shared
simpleprogrammer-shared / kiss-one-best-practice-to-rule-them-all-3.cs
Created July 17, 2016 18:59
KISS — One Best Practice to Rule Them All 3
ar salesMan = GetSalesMan();
var noOfOrders = GetSalesManNoOfOrders(salesMan);
var customers = GetSalesManCustomers(salesMan);
var provision = GetSalesManProvision(salesMan);
// etc.
@simpleprogrammer-shared
simpleprogrammer-shared / refactoring-switches-to-classes-1.cs
Last active April 25, 2018 02:38
Refactoring Switches to Classes 1
// In fighting code
switch(classType)
{
case WARRIOR:
swingSword();
break;
case MAGE:
castSpell();
break;
case THIEF:
@simpleprogrammer-shared
simpleprogrammer-shared / solving-problems-breaking-it-down-1.cs
Created July 11, 2016 14:29
Solving Problems, Breaking it Down 1
String newWord =""
for(int index = oldWord.Length – 1; index <= 0; index—)
newWord += oldWord[index];
return newWord;
@simpleprogrammer-shared
simpleprogrammer-shared / javascript-execution-stack-2.js
Created July 17, 2016 18:10
The JavaScript Execution Stack: The Key to Learning the Language 2
var a = “first variable”;
function myfunction () {
var b = “ second variable”;
console.log(b);
}
console.log(a) // => ‘first variable’
myfunction(); //=> ‘second variable’
@simpleprogrammer-shared
simpleprogrammer-shared / mock-eliminating-patterns-2.cs
Last active July 21, 2016 07:25
Back to Basics: Mock Eliminating Patterns 2
private apocalypseStateMachine = new ApocalypseStateMachine();
public ApocalypseScenario CreateNew()
{
if(apocalypseStateMachine.State == States.PENGUIN_SLAUGHTER)
return new ApocalypseScenario("Penguin Apocalypse, Code Blue");
}
@simpleprogrammer-shared
simpleprogrammer-shared / refactoring-static-methods-stepwise-vs-wrapping-4.cs
Last active July 21, 2016 07:20
Refactoring Static Methods Step-Wise vs Wrapping and Delegating 4
public interface ILostOrderService
{
IEnumerable<Orders> GetLostOrders(int customerId);
IEnumerable<Customer> GetCustomerWithLostOrders()
}
public class LostOrderServiceWrapper : ILostOrderService
{
public IEnumerable<Orders> GetLostOrders(int customerId)
{
@simpleprogrammer-shared
simpleprogrammer-shared / techniques-to-make-apis-easier-to-use-5.cs
Created July 20, 2016 04:15
3 Simple Techniques to Make APIs Easier to Use and Understand 5
public void LoginAsCustomer(LoginInfo loginInfo, CustomerType customerType = CustomerType.NEW_CUSTOMER, int numberOfTries = 3)
{
// ...
}
var loginInfo = new LoginInfo("Darth Vader", "ihatewookes");
LoginAsCustomer(loginInfo);
// What if we need to override a default? No problem.