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 / main.md
Last active February 20, 2022 10:29
Snippets on Scala type classes and implicits

FP Scala snippets

Type classes, implicits, monads, higher-kinded types.

To start with:

  • Semigroup -> combine(x: A, y: A): A associative
  • Monoid extends Semigroup -> zero: A means (combine(x, zero) == combine(zero, x) == x) (zero can be call empty, id)

And/Then:

@telekosmos
telekosmos / semigroup-on-type-classes.md
Last active May 1, 2022 14:42
Type classes + implicit values + extension methods

First, we define the type classes, which are just traits in scala:

// Type classes for Semigroup and Monoid (we will be using monoid, but this is more formal)
trait Semigroup[A] {
  def op(x: A, y: A): A // should be associative
}

trait Monoid[A] extends Semigroup[A] {
  def zero: A
@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]