Skip to content

Instantly share code, notes, and snippets.

@jmhdez
jmhdez / AsDict.cs
Last active December 22, 2015 00:28
Extension Method para convertir un objeto anónimo en un IDictinary
public static class Extensions
{
public IDictionary<string, object> AsDict(this object obj)
{
return obj.GetType().GetProperties().ToDictionary(x => x.Name, x => x.GetValue(obj, null))
}
}
var obj = new { Name = "Lucas", Age = 14 }.AsDict();
@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);
;; depends on [loco "0.3.1"]
(ns loco-playground.core
(:require [loco.core :refer :all]
[loco.constraints :refer :all]))
(def queens-model
(conj
;; Variables [:x i] [:y i] para la posición de la reina i-ésima
(mapcat #(vector ($in [:x %] 0 7) ($in [:y %] 0 7)) (range 8))
;; Todas las filas deben ser distintas
@jmhdez
jmhdez / Post_tweet.cs
Created June 10, 2012 11:31
Post tweet
public void UpdateStatus(string message)
{
// Application tokens
const string CONSUMER_KEY = "YOUR_CONSUMER_KEY";
const string CONSUMER_SECRET = "YOUR_CONSUMER_SECRET";
// Access tokens
const string ACCESS_TOKEN = "YOUR_ACCESS_TOKEN";
const string ACCESS_TOKEN_SECRET = "YOUR_ACCESS_TOKEN_SECRET";
// Common parameters
@jmhdez
jmhdez / core.clj
Created July 18, 2016 20:04
RPG Kata
(ns rpg-kata-clj.core
(:require [clojure.set :refer [intersection]]))
(defrecord Entity [health level attack factions type])
(defn new-character []
(->Entity 1000 1 :melee #{} :character))
(defn new-prop [health]
(->Entity health 1 :melee #{} :property))
@jmhdez
jmhdez / GetTweets.cs
Created June 13, 2012 19:09
Get twitter's home timeline
// Related code: https://gist.github.com/2905028
public IEnumerable<string> GetTimeline(int count)
{
// Application tokens
const string CONSUMER_KEY = "YOUR_CONSUMER_KEY";
const string CONSUMER_SECRET = "YOUR_CONSUMER_SECRET";
// Access tokens
const string ACCESS_TOKEN = "YOUR_ACCESS_TOKEN";
const string ACCESS_TOKEN_SECRET = "YOUR_ACCESS_TOKEN_SECRET";
@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 / DuckType.cs
Created December 8, 2012 10:46
Duck Typing con C#
// Required libs
// - NUnit
// - Castle.DynamicProxy
using System;
using System.Linq;
using System.Reflection;
using Castle.DynamicProxy;
using NUnit.Framework;