Skip to content

Instantly share code, notes, and snippets.

View jehugaleahsa's full-sized avatar

Travis Parks jehugaleahsa

View GitHub Profile
@jehugaleahsa
jehugaleahsa / di.js
Last active August 29, 2015 14:17
JavaScript Dependency Injection
var di = (function () {
if (!Array.isArray) {
Array.isArray = function(arg) {
return Object.prototype.toString.call(arg) === '[object Array]';
};
}
function getContainer() {
var container = function () {
@jehugaleahsa
jehugaleahsa / ObservableModel.cs
Last active February 2, 2016 15:38
Smart or stupid...
public abstract class ObservableModel<TModel> : INotifyPropertyChanged
where TModel : ObservableModel<TModel>
{
private readonly Dictionary<PropertyInfo, object> lookup;
protected ObservableModel()
{
this.lookup = new Dictionary<PropertyInfo, object>();
}
@jehugaleahsa
jehugaleahsa / factories.md
Last active March 8, 2016 01:28
Factories and the Rebirth of Object-Oriented Programming

As familiar as I am with OOP and design patterns, I rarely see myself coding in an object-oriented fashion. I make heavy use of the strategy pattern to swap out implementations based on configuration settings, but that's about it. Like most OOP enthusiasts, at one time I used patterns pretty heavily. Some composite pattern here, decorator there -- sprinkle in a little bridge pattern for taste. With a gradual introduction to TDD and functional programming, that's slowly faded away over the years.

In 2012, it dawned on me how freaking awesome functional programming is. With a few tricks here and there, you can almost completely eliminate the need for stateful classes. Instead, classes just get passed their dependencies via dependency injection (DI). Any other information is passed via method parameters. Closures and other functional hacks handled those edge cases where there seemed to be a need for state. This style of programming is especially effective when you start working with RESTful APIs or heavily thre

@jehugaleahsa
jehugaleahsa / try.java
Created March 17, 2016 17:05
Try/Catch/Finally Hell
List<String> requestCodes(String dbUrl, String id) {
List<String> result = new ArrayList<String>();
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
conn = DriverManager.getConnection(dbUrl);
stmt = conn.prepareStatement("SELECT * FROM customers WHERE id = ?");
stmt.setString(1, id);
rs = stmt.executeQuery();
@jehugaleahsa
jehugaleahsa / performance comparison.txt
Created May 29, 2016 23:54
FlatFiles CSV vs CSV Helper
Flat Files: 00:00:05.4573869
Flat Files: 00:00:05.4114582
Flat Files: 00:00:05.3200663
Flat Files: 00:00:05.3753001
Flat Files: 00:00:05.3733791
Flat Files: 00:00:05.4018424
Flat Files: 00:00:05.3608055
Flat Files: 00:00:05.4624856
Flat Files: 00:00:05.3894682
Flat Files: 00:00:05.5045739
@jehugaleahsa
jehugaleahsa / Program.py
Created August 9, 2016 15:08
Python - Count Occurrences
def getSegments(input):
start = 0
length = len(input)
while start != length:
current = input[start]
end = next((i for i in range(start, length) if input[i] != current), length)
count = end - start
yield (count, current)
start = end
@jehugaleahsa
jehugaleahsa / settings.json
Last active August 22, 2016 14:30
MS Code User Settings
// Place your settings in this file to overwrite the default settings
{
"editor.wrappingColumn": 0,
"editor.rulers": [80],
"editor.autoClosingBrackets": false,
"window.openFilesInNewWindow": false,
"window.restoreFullscreen": true,
"window.format.wrapLineLength": 0,
"html.format.indentInnerHtml": true,
"terminal.integrated.shell.linux": "bash",
@jehugaleahsa
jehugaleahsa / SqlBulkCopier.cs
Created August 24, 2016 14:20
SqlBulkCopy Wrapper
public class SqlBulkCopier<T>
{
private readonly string tableName;
private readonly Dictionary<PropertyInfo, PropertyMapping> mappings;
public SqlBulkCopier(string tableName)
{
if (String.IsNullOrWhiteSpace(tableName))
{
throw new ArgumentNullException("tableName");
@jehugaleahsa
jehugaleahsa / ConnectionLifetimeManager.cs
Created August 24, 2016 14:21
Connection Lifetime Manager
public class ConnectionLifetimeManager : IDisposable
{
private readonly IDbConnection connection;
private readonly bool needsClosed;
public ConnectionLifetimeManager(IDbConnection connection)
{
this.connection = connection;
if (connection.State == ConnectionState.Closed)
{
@jehugaleahsa
jehugaleahsa / join.ps1
Last active July 7, 2023 19:56
PowerShell Script to Split Large Files
function join($path)
{
$files = Get-ChildItem -Path "$path.*.part" | Sort-Object -Property @{Expression={
$shortName = [System.IO.Path]::GetFileNameWithoutExtension($_.Name)
$extension = [System.IO.Path]::GetExtension($shortName)
if ($extension -ne $null -and $extension -ne '')
{
$extension = $extension.Substring(1)
}
[System.Convert]::ToInt32($extension)