Skip to content

Instantly share code, notes, and snippets.

Avatar
💭
👻

Guillermo C. Martínez telekosmos

💭
👻
View GitHub Profile
@telekosmos
telekosmos / silly-js-adt.js
Created Nov 20, 2020
Shy approach to pattern matching in JS
View silly-js-adt.js
class Action { }
class VisitPage extends Action {
constructor(pageUrl) {
super();
this.pageUrl = pageUrl;
}
}
class ViewUser extends Action {
constructor(userName) {
super();
@telekosmos
telekosmos / currying.scala
Last active Aug 21, 2020
Two ways of writing currying functions in Scala
View currying.scala
def sumx(a: Int)(b: Int)(c: Int) = a + b + c
def sumy(a: Int) = (b: Int) => a + b
val sumxx = sumx(1)(1)_ // mind the _
val sumyy = sumy(2)
sumxx(3) // 5
sumyy(3) // 5
@telekosmos
telekosmos / check-conditions-concurrently.md
Last active Jul 16, 2020
Javascript recipe to check asynchronous conditions concurrently
View check-conditions-concurrently.md

Check conditions concurrently

A system is said to be concurrent if it can support two or more actions in progress at the same time. A system is said to be parallel if it can support two or more actions executing simultaneously.

The Art of concurrency

We come up with a (curried) function with which we are able to evaluate a variable number of possibly asynchronous functions over a value in a concurrent way.

const checkConditions = (...predicates) => (value) =>
  Promise.all(predicates.map((p) => p(value))).then((results) => results.reduce((acc, value) => acc && value, true));
@telekosmos
telekosmos / initialize-array.js
Last active Sep 23, 2020
Initialize an array in javascript
View initialize-array.js
const initArray = (n, v) => Array(n).fill(v);
const initList = (size, fromZero = true) => Array.from({ length: size }, (_, i) => fromZero? i: i+1)
const l = initArray(5, 0);
// l = [0, 0, 0, 0, 0]
const l2 = initList(5);
// l2 = [0, 1, 2, 3, 4]
const l3 = initList(5, false);
// l3 = [1, 2, 3, 4, 5]
@telekosmos
telekosmos / file-ocurrences.sh
Created Jan 14, 2020
[shell] Rank word occurences in a text file
View file-ocurrences.sh
iconv -c -t UTF-8 <file> | tr -dC '[:print:]\t\n' | awk '{for(i = 1; i <= NF; i++) {a[$i]++}} END {for(k in a) if(a[k] >= 1) {print k, a[k]}}' | sort -n -k2 --reverse | less
@telekosmos
telekosmos / deconstructing-flatten.js
Last active Sep 29, 2019
es6 - One level deep object flattening
View deconstructing-flatten.js
const fn = ({ a, o }) => ({a, ...o});
const obj = { a:1, o: { b: 2, c: 3 } };
const r = fn(obj);
// r = { a: 1, b: 2, c: 3 }
View es6pojos.js
// doesn't work
const counter = {
val: 0,
next: () => ++this.val, // eslint-disable-line no-plusplus
};
counter.val; // 0
counter.next();
counter.val; // 0
// DOES work
@telekosmos
telekosmos / snapshot_utility.py
Created Jul 29, 2019 — forked from tomconte/snapshot_utility.py
Sample Python script to manage Azure Blob Storage snapshots: list snapshots, take a snapshot, delete a snapshot, copy a snapshot to a new Blob.
View snapshot_utility.py
#!/usr/bin/python
from azure.storage import BlobService
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("container", help="the blob container")
parser.add_argument("blob", help="the blob name")
parser.add_argument("-s", "--snapshot", help="take a new snapshot", action="store_true")
parser.add_argument("-d", "--delete", help="delete a snapshot")
View redirects.md

Unix shell command output redirects

  • > file redirects stdout to file
  • 1> file redirects stdout to file
  • 2> file redirects stderr to file
  • &> file redirects stdout and stderr to file
@telekosmos
telekosmos / kafka-commands.md
Last active May 22, 2019
Kafka tools handy commands
View kafka-commands.md
You can’t perform that action at this time.