Skip to content

Instantly share code, notes, and snippets.

View jehugaleahsa's full-sized avatar

Travis Parks jehugaleahsa

View GitHub Profile
@jehugaleahsa
jehugaleahsa / ArgumentAssigner.js
Created February 10, 2012 20:46
Handle Optional Parameters with Default Values in JavaScript
function assignArguments(values, expression) {
// determine how many arguments are needed for each parameter
// grab all of the defaults, too
var needed = 1;
var argsNeeded = {};
var defaults = {};
var queue = [expression];
for (var queueIndex = 0; queueIndex < queue.length; ++queueIndex) {
var node = queue[queueIndex];
if (typeof node === 'string') {
@jehugaleahsa
jehugaleahsa / Pagination.js
Created February 15, 2012 17:29
Pagination
// page: the currently selected page (1 <= page <= pageCount)
// pageCount: the total number of pages (1 <= pageCount)
// pagesToDisplay: the number of pages displayed at a time (1 <= pagesToDisplay)
function paginate(page, pageCount, pagesToDisplay) {
var buffer = Math.floor(pagesToDisplay / 2);
return {
start: Math.max(1, Math.min(page - buffer, pageCount - pagesToDisplay)),
stop: Math.min(pageCount, Math.max(page + buffer, pagesToDisplay))
};
}
private static T getValue<T>(object source, IFormatProvider provider)
{
Type type = Nullable.GetUnderlyingType(typeof(T)) ?? typeof(T);
if (source == null)
{
if (typeof(T).IsValueType && typeof(T) == type)
{
throw new InvalidCastException();
}
return default(T);
@jehugaleahsa
jehugaleahsa / jaded.md
Last active January 31, 2017 01:21
Finding a balance in complexity

The Early Years...

I was one of those eager college grads that went and read the GOF's Design Patterns. Being pretty fresh to development in a professional setting, I used design patterns everywhere, even where they didn't belong. I've also read a ton of books on OOP and software architecture and they had a huge influence on my past projects.

Jaded

Now that I've been at it for about 10 years, I've seen a sharp decline in the number of design patterns I use. In terms of UML and OOP, I barely even think about them anymore. Some of it's just that I'm so familiar with them that I don't really notice when I'm using them. The rest is because I just don't find myself reaching for them as often as before. Recently, I've noticed a sharp correlation between the use of abstract classes and increased code complexity. So, I usually rely on composition and helper classes for my shared functionality. I use interfaces mostly to support dependency injection and unit testing.

Every now and then, I whip out the Deco

@jehugaleahsa
jehugaleahsa / foldLeft.scala
Created August 5, 2014 19:21
Tail Recursion Optimization
private def foldLeftBad[T, TResult](list: List[T], initial: TResult)(folder: (TResult, T) => TResult): TResult = list match {
case Nil => initial
case head :: tail => folder(foldLeftBad(tail, initial)(folder), head)
}
private def foldLeft[T, TResult](list: List[T], initial: TResult)(folder: (TResult, T) => TResult): TResult = {
@tailrec
def foldLeftSoFar(list: List[T], computer: () => TResult): TResult = list match {
case Nil => computer()
@jehugaleahsa
jehugaleahsa / Unit Test Scenario Builder Pattern.md
Last active August 29, 2015 14:05
A detailed description of Ninject's MockingKernel and the Scenario Builder Pattern.

Unit Test Scenario Builder Pattern

Reusing test fixtures without hiding what’s being tested

Unit testing is hard…

Only in the most basic situations is unit testing actually “easy”. The only code that’s easy to test involve functions that simply take input and calculate a result. These types of test fall into the classic testing paradigm of calling a function and running some assertions. The problem is that most code interacts with other components, which makes it extremely difficult to test functionality in isolation. Remember, testing code in isolation is what makes it a unit test.

The only option available in type-safe languages is to replace dependencies with fakes (stubs, mocks, etc.) at runtime. This requires the dependency to be an interface or an abstract class in C#. On the flip side, in dynamic languages such as JavaScript, Python or Ruby, you can do monkey patching to replace an object’s members (including its methods) at runtime ([http://en.wikipedia.org/wiki/Monkey_patch]). This allows

foreach (int i in items)
{
Console.Out.WriteLine(i);
}
IEnumerator enumerator = ((IEnumerable)items).GetEnumerator();
try
{
int i;
while (enumerator.MoveNext())
@jehugaleahsa
jehugaleahsa / navigator.js
Created September 11, 2014 15:23
Navigator Using Earl.js
application.factory('navigator', ['$http', '$window', 'baseUrl', function ($http, $window, baseUrl) {
function enrich(urlTemplate) {
if (!urlTemplate) {
return null;
}
var template = earl(urlTemplate);
var resource = {
@jehugaleahsa
jehugaleahsa / preql.md
Last active July 6, 2024 14:57
Query Language Examples

Literals and primitives

Literals, such as booleans, integers, floats, and character strings are actually vectors of length 1 (a.k.a., scalars).

booleans: true or false

integers (can have arbitrary _ as separators): _1_, 45_, 1_000_000

floats

@jehugaleahsa
jehugaleahsa / QueryOrderer.cs
Last active August 29, 2015 14:16
EF Orderer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
namespace DataModeling
{
public class QueryOrderer<TEntity>
where TEntity : class