Skip to content

Instantly share code, notes, and snippets.

View Mithrandir0x's full-sized avatar

Oriol López Sánchez Mithrandir0x

View GitHub Profile
@Mithrandir0x
Mithrandir0x / gist:3639232
Created September 5, 2012 16:15
Difference between Service, Factory and Provider in AngularJS
// Source: https://groups.google.com/forum/#!topic/angular/hVrkvaHGOfc
// jsFiddle: http://jsfiddle.net/pkozlowski_opensource/PxdSP/14/
// author: Pawel Kozlowski
var myApp = angular.module('myApp', []);
//service style, probably the simplest one
myApp.service('helloWorldFromService', function() {
this.sayHello = function() {
return "Hello, World!"
@Mithrandir0x
Mithrandir0x / gist:3683228
Last active October 10, 2015 11:27
Function for traversing a matrix over its diagonals
function diagonalTraverse(m, fn)
{
// This only works for rectangular or square
// form matrices. So no rows of variable sizes!
if ( m && m.length > 0 && m[0].length > 0 )
{
var i = 0, j = 0,
ci = null, cj = null,
M = m[i].length, // # of columns
@Mithrandir0x
Mithrandir0x / f_ptr_ex.c
Created November 16, 2012 12:14
Example of function pointers in C
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#define PI 3.141592653589793
double f_a(double x) { return x*x + sin(x) - PI; }
typedef double (*MATH_FUNCTION)(double);
@Mithrandir0x
Mithrandir0x / Console
Created November 23, 2012 17:37
Segmentation Fault with nested function in C
$ gcc -c -g nested_function_segfault.c -ansi -pedantic -O -Wall -Wno-unused-result
nested_function_segfault.c: In function ‘polinomi_d’:
nested_function_segfault.c:18:5: warning: ISO C forbids nested functions [-pedantic]
nested_function_segfault.c: In function ‘main’:
nested_function_segfault.c:33:1: warning: control reaches end of non-void function [-Wreturn-type]
$ gcc nested_function_segfault.o -o nested_function_segfault.exe -lm
$ ./nested_function_segfault.exe
Segmentation fault (core dumped)
@Mithrandir0x
Mithrandir0x / gist:4351696
Created December 21, 2012 09:21
Levenshtein and DTW stoopidities
A LGO R I S M I C A
A V A N Ç A D A
1 1 1 0 0 1 _ 0 1
_ _ _ _ 0 0 1 _ _
_ _ 1 1 1 _ _ _ _
1 1 1 1 1 1 1 0 1
1 1 1 1 0 0 0 1
@Mithrandir0x
Mithrandir0x / gist:4357802
Last active December 10, 2015 01:19
Asynchronous ForEach made by Zef Hemel
//http://zef.me/3420/async-foreach-in-javascript
function asyncParForEach(array, fn, callback) {
var completed = 0;
if(array.length === 0) {
callback(); // done immediately
}
var len = array.length;
for(var i = 0; i < len; i++) {
fn(array[i], function() {
@Mithrandir0x
Mithrandir0x / GenericObjectDeserializer.java
Created December 29, 2012 08:50
A class created by Brian Nettleton that allows to transform a JSON string to a Map object for GSON. To use it, just register a new Type Adapter for Object.class.
/**
* @author Brian Nettleton
*/
class GenericObjectDeserializer implements JsonDeserializer<Object>
{
@Override
public Object deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException
{
if( json.isJsonNull() ) {
@Mithrandir0x
Mithrandir0x / makeplain.js
Last active December 10, 2015 08:08
So, is it a bad idea to fetch data from "arguments"? (Rotten belches in 3, 2, 1...)
function makeplain(strAccents)
{
if(strAccents==undefined)
return "";
strAccents = strAccents.split('');
var strAccentsOut = new Array();
var strAccentsLen = strAccents.length;
var accents = 'ÀÁÂÃÄÅàáâãäåÒÓÔÕÕÖØòóôõöøÈÉÊËèéêëðÇçÐÌÍÎÏìíîïÙÚÛÜùúûüÑñŠšŸÿýŽž';
var accentsOut = ['A','A','A','A','A','A','a','a','a','a','a','a','O','O','O','O','O','O','O','o','o','o','o','o','o','E','E','E','E','e','e','e','e','e','C','c','D','I','I','I','I','i','i','i','i','U','U','U','U','u','u','u','u','N','n','S','s','Y','y','y','Z','z'];
@Mithrandir0x
Mithrandir0x / gist:4465421
Last active December 27, 2022 11:27
"Find HTML open tags" Regular Expression
// Found at http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454
//
// But beware, HTML cannot be regexed... The end is nigh.
var re = /<([a-z]+) *[^/]*?>/g;
A =
1.000000000000e-01 1.000000000000e-01 1.000000000000e+00
-3.900000000000e+00 1.100000000000e+00 3.200000000000e+00
1.400000000000e+00 1.500000000000e+00 -1.000000000000e+00
c = ( 1.000000000000e-01 1.100000000000e+00 -1.400000000000e+00 )
k = 0
SWAP F0 <=> F1
A =