Skip to content

Instantly share code, notes, and snippets.

@clausreinke
clausreinke / monadic.ts
Created July 12, 2013 14:26
monadic javascript/typescript: promises and generators
declare function setImmediate(cb);
declare function setTimeout(cb,n);
declare var process;
function nextTick(cb) {
if (typeof setImmediate == "function") {
setImmediate(cb);
} else if (typeof process == "object" && typeof process.nextTick == "function") {
process.nextTick(cb);
} else if (typeof setTimeout == "function") {
const Day = ({ get, left, right }) => {
const map = f => Day ({
get: f (extract()),
left, right
})
const extend = f =>
Day ({
get: (left, right) => f (Day ({ get, left, right })),
@i-am-tom
i-am-tom / bifunctor-profunctor.js
Created June 26, 2017 18:32
The main examples from the "Bifunctor + Profunctor" post.
const { Left, Right } = require('fantasy-eithers')
const daggy = require('daggy')
Function.prototype.map = function (f) {
return x => f(this(x))
}
//- Where everything changes...
const login = user =>
user.name == 'Tom'
@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)
@i-am-tom
i-am-tom / Lazy.js
Last active January 13, 2024 01:35
A Fantasy Land-compliant type for lazy computation.
const fl = require('fantasy-land')
//- Lazy holds a vaue in a thunk, effectively delaying
//- evaluation until required. This is useful when we're
//- in a situation with either very large data set or
//- cyclical data.
//@ make stack-safe.
//> type Lazy a = Unit -> a
function Lazy(run) {
@i-am-tom
i-am-tom / RoseTree.js
Created May 12, 2017 18:41
A Fantasy Land-Compliant Rose Tree.
const fl = require('fantasy-land')
//- Textbook rose tree.
//+ type RoseTree a = { value :: a, children :: [RoseTree a] }
function RoseTree(value, children) {
if (this.constructor !== RoseTree)
return new RoseTree(value, children)
Object.assign(this, { value, children })
}
@DanHerbert
DanHerbert / fix-homebrew-npm.md
Last active February 12, 2024 17:18
Instructions on how to fix npm if you've installed Node through Homebrew on Mac OS X or Linuxbrew

OBSOLETE

This entire guide is based on an old version of Homebrew/Node and no longer applies. It was only ever intended to fix a specific error message which has since been fixed. I've kept it here for historical purposes, but it should no longer be used. Homebrew maintainers have fixed things and the options mentioned don't exist and won't work.

I still believe it is better to manually install npm separately since having a generic package manager maintain another package manager is a bad idea, but the instructions below don't explain how to do that.

Fixing npm On Mac OS X for Homebrew Users

Installing node through Homebrew can cause problems with npm for globally installed packages. To fix it quickly, use the solution below. An explanation is also included at the end of this document.

@atoponce
atoponce / gist:07d8d4c833873be2f68c34f9afc5a78a
Last active March 19, 2024 17:24 — forked from tqbf/gist:be58d2d39690c3b366ad
Cryptographic Best Practices

Cryptographic Best Practices

Putting cryptographic primitives together is a lot like putting a jigsaw puzzle together, where all the pieces are cut exactly the same way, but there is only one correct solution. Thankfully, there are some projects out there that are working hard to make sure developers are getting it right.

The following advice comes from years of research from leading security researchers, developers, and cryptographers. This Gist was [forked from Thomas Ptacek's Gist][1] to be more readable. Additions have been added from

@gaearon
gaearon / connect.js
Last active April 11, 2024 06:46
connect.js explained
// connect() is a function that injects Redux-related props into your component.
// You can inject data and callbacks that change that data by dispatching actions.
function connect(mapStateToProps, mapDispatchToProps) {
// It lets us inject component as the last step so people can use it as a decorator.
// Generally you don't need to worry about it.
return function (WrappedComponent) {
// It returns a component
return class extends React.Component {
render() {
return (
@tvcutsem
tvcutsem / horton.js
Created May 25, 2023 23:06
Horton in JavaScript: delegation with blame attribution in an object-capability language
/*
* Horton in JavaScript: delegation with blame attribution in an object-capability language
*
* See http://erights.org/elib/capability/horton/index.html for idea and paper.
*
* Implementation based on: http://erights.org/elib/capability/horton
* (with N-ary message support, lexical nesting and rights amplification)
*
* To run:
*