Skip to content

Instantly share code, notes, and snippets.

@sapher
sapher / ProportionValue.cs
Last active December 18, 2015 07:00
Selecting an item randomly from a collection according to related proportions
using System;
using System.Collections.Generic;
namespace Range
{
public class ProportionValue<T>
{
public double Proportion { get; set; }
public T Value { get; set; }
}
@sapher
sapher / ListExtension.cs
Created June 9, 2013 15:16
Extension for IEnumerable that help to get one item randomly from a collection
using System;
using System.Collections.Generic;
using System.Linq;
namespace List.Extensions
{
public static class ListExtension
{
public static Object GetOneRandom<T>(this IEnumerable<T> list)
{
@sapher
sapher / LimitedStack.cs
Created June 10, 2013 10:51
Stack with a limited amount of element . When an element is added, the last element is removed. This class implements IEnumerable so it's possible to use it in a foreach for exemple.
using System;
using System.Collections;
using System.Collections.Generic;
namespace Stack
{
[Serializable]
public class LimitedStack<T> : IEnumerable
{
#region Fields
@sapher
sapher / HashKeyExists.js
Last active December 23, 2015 17:09
Check if keys exists in hash with Underscore.js
//Hash of data
data = {
longitude : 1,
latitude : 1
};
//Required keys
var requiredKeys = ['longitude', 'latitude'];
//Return true
@sapher
sapher / convertHrTimeToMs.coffee
Created December 30, 2013 15:51
Simply convert the "high-resolution real time" (difference) provided by NodeJS to an understandable value.
exports.convertHrTimeToMs = (hrtime) =>
diff = process.hrtime(hrtime)
return parseInt((diff[0] * 1e9 + diff[1]) * 1e-6)
@sapher
sapher / underscore_contains.js
Created January 12, 2014 05:32
Test with Underscore/Lodash if an object contains same keys as another array
var array = ["email", "password", "username"];
var search = ["email", "password"];
console.log(_.every(search, _.partial(_.contains, array)));
//true
@sapher
sapher / hash-password.js
Created February 21, 2014 13:27
Hashing password with NodeJS
crypto = require('crypto');
//Iterations
iterations = 100;
//Length
length = 32;
/*
Generate the salt
@sapher
sapher / ListExtension.cs
Last active August 29, 2015 14:04
List extension that help split a list into smaller chuncks of n size
using System;
using System.Collections.Generic;
using System.Linq;
namespace Core.Extensions
{
public static class ListExtension
{
public static IEnumerable<IEnumerable<T>> Split<T>(this List<T> list, int size)
{
@sapher
sapher / printf.c
Created September 2, 2014 18:54
Redirect printf for stm32f4 in keil uvision
#include <stdio.h>
#define ITM_Port8(n) (*((volatile unsigned char *)(0xE0000000+4*n)))
#define ITM_Port16(n) (*((volatile unsigned short*)(0xE0000000+4*n)))
#define ITM_Port32(n) (*((volatile unsigned long *)(0xE0000000+4*n)))
#define DEMCR (*((volatile unsigned long *)(0xE000EDFC)))
#define TRCENA 0x01000000
struct __FILE { int handle; /* Add whatever is needed */ };
@sapher
sapher / main.c
Last active August 29, 2015 14:07
Reading of 96bits long Unique ID on STM32F407VG
/* Includes ------------------------------------------------------------------*/
#include <stdio.h>
#include "STM32F4xx.h"
/* Private variables ---------------------------------------------------------*/
struct __FILE { int handle; };
FILE __stdout;
FILE __stdin;
/* Private functions ---------------------------------------------------------*/