Skip to content

Instantly share code, notes, and snippets.

View robotlolita's full-sized avatar
🐴
everything happens so much oh no

Quil robotlolita

🐴
everything happens so much oh no
View GitHub Profile
@robotlolita
robotlolita / 0-source.liz
Last active December 29, 2015 06:49
BEST COMPILER IS BEST
(defn sum ((xs))
"Returns the sum of all elements in xs."
(foldl + 0 xs))
@robotlolita
robotlolita / promises.md
Last active December 28, 2015 12:19
Promises: replacing dependency on time, by dependency on your data.

So, promises are the big thing now in JavaScript and all that shit. Though they are a fairly old concept. In short using promises means you're giving up on your dependency on time and mutable state, and buying into dependency on your data, regardless of time, or structure, or any of those unnecessarily complicated things.

First, let's build a conceptual understanding of promises, and then I'll show how this maps to the particulars of the Promises/A+ specification. A promise is a placeholder for a value. Instead of saying what your value depends on through your program structure, i.e.: doX(a); doY(a); ... you say what the value depends on through other values. ie.: b = doX(a); c = doX(b); — here b depends on a; and c depends on b, which depends on a.

Or, a more "visual" representation of this would be:

var a = Promise();

b = a.then(doX);
@robotlolita
robotlolita / maybe.md
Last active December 28, 2015 10:09
Maybe<T>/Option<T> are better alternatives to having Null

The once-upon-a-time package aims to bring the approaches to generic programming and software correctness that's been in use for quite a while in major functional languages (monads, functors, etc.).

In this case, we want to have a better alternative to using Null, for when we may or may not have a value. Haskell and ML models this explicitly with a type they, respectively, call Maybe and Option — the same thing, really, just different names.

var Maybe = require('once-upon-a-time').monads.Maybe

Take the find function, for example. Find must take in a predicate (a function that takes an element of a collection, and returns whether the element passes the test or not), a collection of things, and return the first thing that passes the test. But we do need to define what the function will return if no element passes the test. Usually, people would use null, or undefined, but we can't use either in this case because the collection may contain null values, and we would never be sure whet

@robotlolita
robotlolita / project-euler-1.md
Last active December 27, 2015 23:29
Find the sum of all the multiples of 3 or 5 below 1000.

Multiples of

We know that 3 is a multiple of 3. 5 is a multiple of five. 6 is a multiple of 3. And so on, and so forth. For this exercise, we're going to consider all numbers below 1000 that are multiples of either 3, or 5.

So, first, let's define what it means to be a "multiple of" something. To be a multiple of, means that the denominator is a multiple of the numerator if, and only if, there exists an integer k such that denominator × k yields the numerator.

We are not interested in finding what k is, in this exercise but rather only if there exists one. If one k exists, this means that we can divide the numerator by the denominator without leaving any rest. We have an operation in JavaScript which will give us the rest of a division, it's called %.

So, let's encode this definition as a function:

@robotlolita
robotlolita / 0-supporting-functions.ls
Last active December 27, 2015 20:39
Composing validations with the Validation monads and an Error semi-group
{ Validation, Error:ErrorGroup } = (require 'once-upon-a-time').monads
# :: a -> a
identity = (a) -> a
# :: Error -> ErrorGroup
as-error-group = (a) -> ErrorGroup.of a
# :: a -> [a]
filters = [
function isEven(a) { return a % 2 === 0 },
function isPositive(a) { return a > 0 }
]
var items = [-4, -3, -2, -1, 0, 1, 2, 3, 4]
// <*> :: [(a -> b)] -> [a] -> [b]
function ap(fs){ return function(y) {
return fs.map(function(f){ return f(y) })}}
@robotlolita
robotlolita / gist:7216015
Created October 29, 2013 14:46
Basically, every time the subject "correctness" comes up. Every. Single. Time. Damn, no wonder we have bugs everywhere.
<dmanderson> Oh Domo :)
<dmanderson> Nice templater
<Sorella> Oh.
<dmanderson> I've been using an underscore to JST compiler as of late, but
this looks really nice.
<Sorella> I don't quite like Domo, but sure.
<Sorella> Oh Lord, don't use Underscore's templating. Like,
really. Don't. Just. Don't.
<dmanderson> No I'm using JST
<dmanderson> writing the templates in underscore
@robotlolita
robotlolita / oo.elm
Last active December 26, 2015 15:09
OO as structurally typed polymorphic records in Elm
type Named a = { a | name: String }
type Greeter a = { a | greet: (String -> String) }
bob = { name = "Bob", age = 12 }
alice = { name = "Alice", age = 19, gender = "F" }
makeGreeterFromString: String -> Greeter {}
makeGreeterFromString name = { greet = \how -> how ++ ", " ++ name }
makeGreeterFromNamed: Named a -> Greeter (Named a)
@robotlolita
robotlolita / list.md
Last active December 26, 2015 08:59
PLT reading list yo~

Meta-Programming

  • Towards a fully-reflective meta-programming language (G. Neverov, P. Roe)
  • D-Expressions: Lisp Power, Dylan Style (J. Bachrach, K. Playford)
  • The Java Syntactic Extender (J. Bachrach, K. Playford)
  • Template Meta-Programming for Haskell (T. Sheard, S.P. Jones)
  • First-class macros have types (A. Bawden)
  • Macros that work (W. Clinger, J. Rees)
  • Macros as multi-stage computations: type-safe, generative, binding macros in MacroML (S.E. Ganz, A. Sabry, W. Taha)
  • Hygienic macro expansion (E. Kohlbecker, D. Friedman, M. felleisen, B. Duba)
@robotlolita
robotlolita / group-by.js
Last active December 24, 2015 19:59
Deriving `groupBy` from `reduce`
// Let's start by assuming you're not familiar with the concept of
// fold. A fold is a generalisation of a recursive pattern, or IOW:
fold :: (b, a → b), b, [a] → b
function fold(f, initial, []) {
return initial
}
function fold(f, initial, [x, ...xs]) {