Sunday, May 25, 2014

Wednesday, May 7, 2014

.NET - How can you split a 'caps' delimited string into an array?

System.Text.RegularExpressions.Regex.Replace(s, "([a-z](?=[A-Z])|[A-Z](?=[A-Z][a-z]))""$1 ");

Smo: How to get the table a foreign key refers to

foreach (ForeignKey key in currentTable.ForeignKeys)
{
    foreach (ForeignKeyColumn column in key.Columns)
    {
        Console.WriteLine("Column: {0} is a foreign key to Table: {1}", column.Name, key.ReferencedTable);
    }
}

Extension methods must be defined in a non-generic static class

change this
public class Stats
    {
        public static IEnumerable<IEnumerable<T>> Combinations<T>(this IEnumerable<T> elements, int k)
        {
            return k == 0 ? new[] { new T[0] } :
              elements.SelectMany((e, i) =>
                elements.Skip(i + 1).Combinations(k - 1).Select(c => (new[] { e }).Concat(c)));
        }
    }
to

public static class Stats
    {
        public static IEnumerable<IEnumerable<T>> Combinations<T>(this IEnumerable<T> elements, int k)
        {
            return k == 0 ? new[] { new T[0] } :
              elements.SelectMany((e, i) =>
                elements.Skip(i + 1).Combinations(k - 1).Select(c => (new[] { e }).Concat(c)));
        }
    }

LINQ Difference Between Select and SelectMany

SelectMany flattens queries that return lists of lists

Algorithm to return all combinations of k elements from n

public static class Stats
    {
        public static IEnumerable<IEnumerable<T>> Combinations<T>(this IEnumerable<T> elements, int k)
        {
            return k == 0 ? new[] { new T[0] } :
              elements.SelectMany((e, i) =>
                elements.Skip(i + 1).Combinations(k - 1).Select(c => (new[] { e }).Concat(c)));
        }
    }

How to calculate the difference between two dates using PHP?

<?php
$date1 = new DateTime("2007-03-24");
$date2 = new DateTime("2009-06-26");
$interval = $date1->diff($date2);
echo "difference " . $interval->y . " years, " . $interval->m." months, ".$interval->d." days ";