Skip to content

Instantly share code, notes, and snippets.

Travis Parks jehugaleahsa

Block or report user

Report or block jehugaleahsa

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@jehugaleahsa
jehugaleahsa / ArgumentAssigner.js
Created Feb 10, 2012
Handle Optional Parameters with Default Values in JavaScript
View ArgumentAssigner.js
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') {
View Pagination.js
// 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))
};
}
View Convert.ChangeType.cs
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 Jan 31, 2017
Finding a balance in complexity
View jaded.md

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 Aug 5, 2014
Tail Recursion Optimization
View foldLeft.scala
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 Aug 29, 2015
A detailed description of Ninject's MockingKernel and the Scenario Builder Pattern.
View Unit Test Scenario Builder Pattern.md

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

View foreach expansion.cs
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 Sep 11, 2014
Navigator Using Earl.js
View navigator.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 / presql.sql
Last active Aug 30, 2019
Query Language Examples
View presql.sql
-- Entities come in the form of aliases (e.g., X as x)
-- and comma-separated fields are wrapped with curly braces (e.g., { x, y }).
-- Field names must be unique within entities.
-- Field names can be overwritten using a colon (e.g., { x: y, a: b }).
-- select - get all applications
from Application as a
select a
-- The distinct keyword following select removes duplicates from the results.
View QueryOrderer.cs
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
You can’t perform that action at this time.