Skip to content

Instantly share code, notes, and snippets.

@jmhdez
jmhdez / PlayerFactory.cs
Created May 7, 2014 18:53
Refactoring switch to Dictionary
using System;
using System.Collections.Generic;
using Model.Strategies;
using Model.Strategies.Minimax;
namespace Model
{
public class PlayerFactory
{
private ITwoPlayersGame TwoPlayersGame { get; set; }
@jmhdez
jmhdez / sample.cs
Created September 16, 2014 10:32
Maybe<T> Sample
public IEnumerable<ProductSalesEntry> GetProductSales(Maybe<User> user, DateTime fromDate, DateTime toDate)
{
// El método puede recibir o no un usuario o un Maybe<User>.Empty.
// Si recibe un usuario, se pasa su Id a la consulta SQl, si no,
// se pasa 0 y la consulta SQL no filtrará por usuario;
// vamos, el típico where (user.Id = @userId or @userId = 0)
// Para hacer explícito que el usuario es un parámetro opcional del método, se
// define como un Maybe<User>. Se converte en un Maybe<int> para obtener el Id
// usando "select" (el bind de cualquier mónada, pero más C# friendly) y finalmente
@jmhdez
jmhdez / gist:46de62a0520bd0c8081b
Created November 14, 2014 15:03
partial vs func
(def double-partial (partial * 2))
(defn double-fn [x] (* 2 x))
(time
(dotimes [n 10000000]
(double-partial 5)))
;; => "Elapsed time: 3139.323652 msecs"
(time
@jmhdez
jmhdez / MachineLearning.cs
Last active August 29, 2015 14:17
Ejemplo de uso de numl para generar árboles de decisión mediante aprendizaje automático sobre un corpus de Princesas Disney
// Require instalar el paquete numl desde NuGet
using System;
using numl;
using numl.Model;
using numl.Supervised.DecisionTree;
namespace MachineLearning
{
public enum HairColor
@jmhdez
jmhdez / analytics.cs
Created March 19, 2012 09:38 — forked from gregoryyoung/analytics.cs
Usage Tracking with Google Analytics
public class TrackingService
{
// Sample usage:
// service.TrackEvent("customers/add");
// service.TrackEvent("order/discount-applied");
public void TrackEvent(string path)
{
ThreadPool.QueueUserWorkItem(x => TrackPageView("/events/" + path));
}
@jmhdez
jmhdez / GoToPlugin.java
Created June 22, 2012 07:03
Sample Cordova Plugin for Android
package koalite.cordova;
import org.apache.cordova.api.Plugin;
import org.apache.cordova.api.PluginResult;
import org.apache.cordova.api.PluginResult.Status;
import org.json.JSONArray;
import android.content.Intent;
import android.net.Uri;
public class GoToPlugin extends Plugin {
"scripts": {
"browserify": "browserify lib/index.js -o dist/app.js -t [babelify --presets [es2015 react]]",
"dev": "nodemon --watch lib --exec npm run browserify"
},
@jmhdez
jmhdez / DuckType.cs
Created December 16, 2012 16:32
Duck typing con C# preparado para realizar implementaciones de interfaces con objetos anónimos
// Required libs
// - NUnit
// - Castle.DynamicProxy
using System;
using System.Linq;
using System.Reflection;
using Castle.DynamicProxy;
using NUnit.Framework;
@jmhdez
jmhdez / Sample.cs
Last active December 19, 2015 19:29
Unas cuantas opciones para refactorizar un método a la hora de testearlo
// Situación inicial
public class OrderStatsCalculator
{
// Inyectado por constructor. En todos los casos lo hago igual.
private IOrderRepository repository;
// Esto se puede testear con un mock/stub/fake/etc. que inyectes por
// el constructor
public int Calculate()
{
@jmhdez
jmhdez / Elements.cs
Last active December 20, 2015 11:19
Compartiendo elementos en posiciones dependientes del contenedor
public interface IElement
{
IEnumerable<ChildElement> Children { get; }
}
public class ChildElement
{
public readonly IElement Value;
public readonly Point Point;