Skip to content

Instantly share code, notes, and snippets.

View peter-leonov's full-sized avatar

Peter Leonov peter-leonov

View GitHub Profile
@peter-leonov
peter-leonov / promise_monad.md
Created January 14, 2018 19:52 — forked from VictorTaelin/promise_monad.md
async/await is just the do-notation of the Promise monad

async/await is just the do-notation of the Promise monad

CertSimple just wrote a blog post arguing ES2017's async/await was the best thing to happen with JavaScript. I wholeheartedly agree.

In short, one of the (few?) good things about JavaScript used to be how well it handled asynchronous requests. This was mostly thanks to its Scheme-inherited implementation of functions and closures. That, though, was also one of its worst faults, because it led to the "callback hell", an seemingly unavoidable pattern that made highly asynchronous JS code almost unreadable. Many solutions attempted to solve that, but most failed. Promises almost did it, but failed too. Finally, async/await is here and, combined with Promises, it solves the problem for good. On this post, I'll explain why that is the case and trace a link between promises, async/await, the do-notation and monads.

First, let's illustrate the 3 styles by implementing

@peter-leonov
peter-leonov / lazy-list.js
Created May 21, 2017 21:07 — forked from gvergnaud/lazy-list.js
Lazy List, implemented with es6 generators
/* ----------------------------------------- *
Lazy List Implementation
* ----------------------------------------- */
// Haskell-like infinite List, implemented with es6 generators
// Lazyness lets you do crazy stuff like
List.range(0, Infinity)
.drop(1000)
.map(n => -n)
// moved to https://github.com/peter-leonov/css-cleaner
# get hex color of pixel
# implementation of answer here http://stackoverflow.com/questions/8894194/retrieving-the-hex-code-of-the-color-of-a-given-pixel
# solution to 'undefined method for MiniMagick::CommandBuilder in mini_magick'
require 'mini_magick'
module MiniMagick
class Image
def pixel_at(x, y)
run_command("convert", "#{path}[1x1+#{x.to_i}+#{y.to_i}]", 'txt:').split("\n").each do |line|
return $1 if /^0,0:.*(#[0-9a-fA-F]+)/.match(line)
end