Skip to content

Instantly share code, notes, and snippets.

Avatar
🐴
everything happens so much oh no

Quil robotlolita

🐴
everything happens so much oh no
View GitHub Profile
View foo.js
// Returns a list of `[key, value]` pairs for an object
function values(object) {
return Object.keys(object).map(function(key) {
return [key, object[key]]
})
}
// Computes something by applying a binary function to
// a seed value, and a `[key, value]` pair.
function foldRight(object, seed, computation) {
@robotlolita
robotlolita / 0-specification.md
Last active Jan 1, 2016
Monadic Futures specification
View 0-specification.md

A future should provide the fork, of, chain, and orElse methods, subject to the following rules:

new Future(computation)

  1. The computation should fulfil the type (α → γ), (β → γ) → γ.
  2. The value should be stored in the internal field [[Computation]], as-is.
  3. If computation is not a function, the behaviour is undefined.

Future.prototype.fork(f, g)

View gist:8033418
[p1, p2, p3, p4].reduce(function(result, current){
return result.then(function(context) {
return context.done? context
: /* otherwise */ b.then(function(value) {
return shouldWeGoOn(value)? { done: false, values: context.concat([value]) }
: /* otherwise */ { done: true, values: context }
})
})
}, Promise.of({ done: false, values: [] })
View continuations.js
function find_paths(w, h, downs, rights, path, paths, continuation) {
if ((downs == h) && (rights == w)) {
draw_path(path)
setTimeout(function() {
continuation(paths + 1)
}, 1000)
}
else if (downs < h) {
find_paths(w, h, downs + 1, rights, path.concat('d'), paths, continuation)
}
@robotlolita
robotlolita / trilean.hs
Last active Dec 30, 2015
Three-valued logic.
View trilean.hs
module Trilean where
data Trilean = Yes
| No
| Unknown
deriving (Eq, Show)
(/\) :: Trilean -> Trilean -> Trilean
(/\) Yes a = a
@robotlolita
robotlolita / char-parser.ls
Created Dec 1, 2013
Parsing combinators in LS
View char-parser.ls
export char = (a) -> (state) ->
state.consume 1 .chain (b) ->
| a is b => Either.Right [state.skip 1; b]
| otherwise => Either.Left [state; "Expected #a, found #b."]
.or-else ->
Either.Left [state; "Expected #a, but reached the end of the input."]
@robotlolita
robotlolita / control.monad.basics.ls
Last active Dec 29, 2015
sequence :: Monad m => [m a] -> m [a] in LiveScript
View control.monad.basics.ls
# ## Function: sequence
#
# Evaluates each action in sequence, left to right, collecting the
# results.
#
# + type: (Monad m) => m -> [m a] -> m [a]
export sequence = (m, ms) --> do
return ms.reduce-right perform, m.of []
# where:
function perform(m1, m2) => do
View bar-2.js
function f(a) {
if (a > 1000) return 1
if (a > 100) return 2
if (a > 10) return 3
return 4
}
var i = 100000;
while (i--) {
f(Math.random() * 1000)
View wtf.ls
memoised-fork = (f, future) ->
pending = []
fold = (g, h) ->
fold := (g, h) -> pending.push rejected: g, resolved: h
f do
* (a) -> do
future.is-pending := false
future.is-rejected := true
future.value := a
@robotlolita
robotlolita / loops-are-evil.md
Last active Mar 2, 2022
Why `xs.each(f)` should not be considered a "loop".
View loops-are-evil.md

First and foremost, let's take a look at the following pieces of code. The first one is something you should be rather familiar with, and the second one is also a somewhat familiar idiom these days (at least in languages with higher-order functions):

// Example 1:
30 + 12

// Example 2:
xs.map(f)