Skip to content

Instantly share code, notes, and snippets.

View asolove's full-sized avatar
🤔
Thinking about the essence of UI programming

Adam Solove asolove

🤔
Thinking about the essence of UI programming
View GitHub Profile
@asolove
asolove / polysub.js
Created April 20, 2016 22:51
Subtyping polymorphism!
// @flow
type Foo<A> = { n: number, extra: A };
type Foo2 = Foo<{s: string}>;
type Bar<A> = { foo: Foo<A> };
type Bar2 = { foo: Foo2 };
function getBarN(bar: Bar){ return bar.foo.n }
function getBarS(bar: Bar2){ return bar.foo.extra.s }
// @flow
type Foo = { n: number };
type Foo2 = Foo & { s: string };
type Bar = { foo: Foo };
type Bar2 = { foo: Foo2 };
function fooN(foo: Foo){ return foo.n; }
function barFooN(bar: Bar){ return bar.foo.n }
@asolove
asolove / subtype.js
Last active April 21, 2016 14:10
Nested subtyping
// @flow
// I want to write some shared logic with the 'core' data for an object:
type Widget = { id: number, customer_id: number }
function getCustomerId(w: Widget): number {
return w.customer_id
}
// But in another context Widgets have some extra information
type ManufacturableWidget = { id: number, customer_id: number, machine_id: number }
@asolove
asolove / darn.js
Created April 20, 2016 15:17
Flow sub/union typing
// Union types work as subtypes
type Animal = { legs: number }
type Person = Animal & { name: string }
function numberOfLegs(a: Animal): number {
return a.legs;
}
const p: Person = { legs: 2, name: "Adam" }
numberofLegs(p) === 2
@asolove
asolove / example.js
Created April 12, 2016 15:18
Flow not typing pure fns used as React components?
// @flow
import {Component} from 'react';
export class Parent extends Component {
render () {
return <div>
<JustFunction foo={2}/>
<FullComponent foo={2}/>
</div>
}
@asolove
asolove / foo.js
Created April 11, 2016 16:13
Flow type for react component with ref callback
// I want to do something like this:
export default class Foo extends React.Component {
render() {
return <div ref={div => this._div = div}>I want a reference to this!</div>
}
}
// Flow says: "Property _div not found on Foo"
// Is there a way to give Foo the type Component & {_div?: Component} ?
// If I have a variant with two arms
type Foo = { type: "bar", bar: string } | { type: "baz", baz: string };
// It would be nice if a function could handle those two arms.
function fromThingy(foo: Foo): string {
switch(foo.type) {
case "bar":
return foo.bar;
case "baz":
return foo.baz;
@asolove
asolove / generateCss.js
Created June 28, 2015 19:00
Dumb CSS generation
// LESS version
var less = require("less");
var lessTask = {
parseTime: function(source, options, callback) {
less.parse(source, options, function(err, root, imports, options) {
if (err) { return callback(err); }
var result;
try {
@asolove
asolove / fixWebFonts.js
Created March 14, 2014 17:59
Fix Chrome WebFonts painting bug
(function(){
function brieflyAddCss(cssCode) {
var styleElement = document.createElement("style");
styleElement.type = "text/css";
if (styleElement.styleSheet) {
styleElement.styleSheet.cssText = cssCode;
} else {
styleElement.appendChild(document.createTextNode(cssCode));
}
document.head.appendChild(styleElement);
@asolove
asolove / cursor.cljs
Created January 8, 2014 18:53
Om cursor protocol idea
(defprotocol ICursor
(value [self] "Get the current value of the cursor")
(transact [self f] "Update the current value of the cursor"))
(extend Atom
ICursor
(value [self]
(deref self))
(transact [self f]
(swap! self f)))