Skip to content

Instantly share code, notes, and snippets.

View telekosmos's full-sized avatar
💭
👻

Guillermo C. Martínez telekosmos

💭
👻
View GitHub Profile
@telekosmos
telekosmos / comprehensions-map-flatmap.scala
Last active January 14, 2022 16:53
How for comprehensions relate to map/flatMap as syntactic sugar in Scala
val l1 = List(1,3,5,7)
val l2 = List(2,4,6,8)
val result = for {
x <- l1
y <- l2
} yield x * y
val flatMapResult = l1.flatMap(i => l2.map(_ * i))

(Mostly taken from here)

If I want to make a benchmark module, I'll usually resort to clock facilities provided by the standard libraries or, at worse, I'll declare a clock type and request to be initialized with a class implementing it before the module's functionality is ready to be used.

When module support in the language is available, however, I'll not only declare the benchmark interfaces I provide, but I'll also declare that I need a "clock module" -- a module exporting certain interfaces that I need.

A client of my module would not be required to do anything to use my interfaces -- it could just go ahead and use it. Or it could not even declare that my benchmark module would be used and, instead, declare that it has a requirement for that module.

@telekosmos
telekosmos / latency.markdown
Created November 12, 2021 11:56 — forked from josejuansanchez/latency.markdown
Latency numbers every programmer should know

Latency numbers every programmer should know

L1 cache reference ......................... 0.5 ns
Branch mispredict ............................ 5 ns
L2 cache reference ........................... 7 ns
Mutex lock/unlock ........................... 25 ns
Main memory reference ...................... 100 ns             
Compress 1K bytes with Zippy ............. 3,000 ns  =   3 µs
Send 2K bytes over 1 Gbps network ....... 20,000 ns  =  20 µs
SSD random read ........................ 150,000 ns  = 150 µs

Read 1 MB sequentially from memory ..... 250,000 ns = 250 µs

@telekosmos
telekosmos / silly-js-adt.js
Created November 20, 2020 16:33
Shy approach to pattern matching in 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 August 21, 2020 09:04
Two ways of writing currying functions in 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 July 16, 2020 11:42
Javascript recipe to check asynchronous conditions concurrently

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) =&gt; p(value))).then((results) =&gt; results.reduce((acc, value) =&gt; acc &amp;&amp; value, true));
@telekosmos
telekosmos / initialize-array.js
Last active September 23, 2020 10:18
Initialize an array in javascript
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 January 14, 2020 13:02
[shell] Rank word occurences in a text file
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 September 29, 2019 18:31
es6 - One level deep object flattening
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 }
@telekosmos
telekosmos / es6pojos.js
Last active August 23, 2019 07:57
ES6 POJOs
// 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