Skip to content

Instantly share code, notes, and snippets.

View XoseLluis's full-sized avatar

XoseLluis XoseLluis

  • Xixón, Asturies
View GitHub Profile
@XoseLluis
XoseLluis / orderPreservingObject.js
Last active August 29, 2015 14:04
JavaScript Object/Dictionary that keeps track of insertion order, allowing iteration in such order
// deploytonenyures.blogspot.com
//At the time of this writing (2014/07/22) this code only works in Firefox
//notice that at the time of this writing, Direct Proxies and Array.prototype.findIndex are only supported by Firefox
function OrderPreservingObject(initObj){
var obj = {
orderedKeys: [],
forEach: function(actionFunc){
//where actionFunc receives as parameters: item, key, tiedDic
@XoseLluis
XoseLluis / ActionPerformer.cs
Created August 9, 2014 23:46
Sample of how to Suspend, Resume, Abort Threads in .Net
// deploytonenyures.blogspot.com
using System;
using System.IO;
using System.Threading;
enum Status
{
NotStarted,
Running,
@XoseLluis
XoseLluis / DoCopy.cs
Last active August 29, 2015 14:05
Copies a file in a non locking mode, so that other processes will be able to write to it while the copy is taking place. The copy can be canceled, and events are raised each time a certaing percentage of the copy is achieved.
// deploytonenyures.blogspot.com
// example of how to use of the FileCopyTaker class
// while running the copy, you can easily try to append lines to the file with just: echo myline >> file.txt
using System;
using System.IO;
using System.Collections.Generic;
using System.Threading;
using System.Web.Script.Serialization;
@XoseLluis
XoseLluis / Array.prototype.while.js
Last active August 29, 2015 14:07
functional style while loop
//http://deploytonenyures.blogspot.fr/2014/10/canceling-functional-style-iteration.html
//function stopCondition(item, index, array) returns true to indicate stop
//function action(item, index, array)
Array.prototype.while = function(stopCondition, action){
var curPos = 0;
while (curPos < this.length && !stopCondition(this[curPos], curPos, this)){
action(this[curPos], curPos, this);
curPos++;
}
@XoseLluis
XoseLluis / AroundProxy.pm
Last active August 29, 2015 14:07
Basic Proxy example in Perl using AUTOLOAD
#http://deploytonenyures.blogspot.fr/2014/10/methodmissing.html
package AroundProxy;
use strict; use warnings;
sub New {
my ($class, $targetObj, $interceptorFunc) = @_;
my $self = {
targetObj => $targetObj,
interceptorFunc => $interceptorFunc
@XoseLluis
XoseLluis / asyncAwaitEnabler.js
Last active August 29, 2015 14:23
Simulate C# async/await in ES6 thanks to generators
// http://deploytonenyures.blogspot.fr/2015/06/simulating-await-with-es6-generators.html
//as of node 0.12.4 we still need the -harmony flag to enable generators
var print = console.log;
//---------------------
// class used to represent an asynchronous operation
//it's a sort of "poor man's" Promise
@XoseLluis
XoseLluis / City.pm
Created November 11, 2015 23:54
TypedSerializer.pm
package City;
use strict;
use warnings;
sub New
{
my ($type, $name) = @_;
my $self = {
name => $name,
@XoseLluis
XoseLluis / es6ProxiesAndGetTrap.js
Last active December 13, 2021 10:10
ES6 proxies and method interception, 2 different ways of setting your get trap
// http://deploytonenyures.blogspot.fr/2015/11/es6-proxies-part-ii.html
//to run it in node.js 4 it should be just this flag: --harmony_proxies
//but does not seem to work, so run it in Firefox
var cat = {
name: "Kitty",
method1: function(msg){
console.log("cat: " + this.name + ", method1 invoked with msg: " + msg);
this.method2(msg);
},
@XoseLluis
XoseLluis / ConsoleSpinner.cs
Created April 20, 2016 21:56
A ConsoleSpinner in C# (you know the typical |/-\|/-\ kind of thing)
using System;
using System.Collections.Generic;
namespace HttpClientAsyncTest
{
public class ConsoleSpinner
{
int pos;
List<string> items;
//http://blog.keithcirkel.co.uk/metaprogramming-in-es6-part-2-reflect/
//http://stackoverflow.com/a/25585988
function runTest(account){
console.log("___________");
console.log(account.totalSpent);
console.log("-----------");
console.log(account.amountAvailable);
console.log("-----------");
console.log("toString: " + account.toString());