Skip to content

Instantly share code, notes, and snippets.

@jmhdez
jmhdez / console.js
Last active August 13, 2023 01:16
Javascript console replacement
(function(window, document) {
// Create the DOM structure to hold the console messages
var div = document.createElement("div");
div.style.cssText = "position: absolute; " +
"top: 5px; left: 5px; right: 5px; bottom: 5px; " +
"padding: 10px; " +
"overflow-y: auto; " +
"display: none; " +
@jmhdez
jmhdez / gist:6669478
Last active December 23, 2015 17:29
Enumerators y Dispose
Una consulta básica usando Linq-to-NHibernate (con EF supongo que sería parecido)
var dbQuery = session.Query<Product>();
El IEnumerable que devuelve, internamente tiene un Enumerator parecido a esto:
IEnumerator<Product> GetEnumerator() {
using (var reader = command.ExecuteReader())
{
while (reader.Read())
public static class Build
{
public static OrderBuilder Order(Cutomer customer)
{
return new OrderBuilder(customer);
}
public static CustomerBuilder Customer(string name)
{
return new CustomerBuilder(name);
@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 / marvel-api.clj
Last active February 23, 2018 14:09
Using the Marvel API from clojure
;; required deps [clj-http "0.9.2"]
(ns marvel-clj.core
(:require [clj-http.client :as http]
[clj-http.util :as util]
[clojure.string :as str])
(:gen-class))
(def public-key "YOUR_PUBLIC_KEY_HERE")
(def private-key "YOUR_PRIVATE_KEY_HERE")
@jmhdez
jmhdez / ko-chart.js
Last active February 11, 2019 15:32
Custom bindings to use Chart.js with Knockout.js
/*global ko, Chart */
(function(ko, Chart) {
ko.bindingHandlers.chartType = {
init: function(element, valueAccessor, allBindings, viewModel, bindingContext) {
if (!allBindings.has('chartData')) {
throw Error('chartType must be used in conjunction with chartData and (optionally) chartOptions');
}
},
@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
"scripts": {
"browserify": "browserify lib/index.js -o dist/app.js -t [babelify --presets [es2015 react]]",
"dev": "nodemon --watch lib --exec npm run browserify"
},