Skip to content

Instantly share code, notes, and snippets.

View almost's full-sized avatar

Thomas Parslow almost

View GitHub Profile
@almost
almost / proposal.md
Last active September 12, 2019 09:07
Reactive 2016 Lightning Talk Proposal: Get Flow

This is a proposal for a lightning talk at the Reactive 2016 conference.

NOTE: If you like this, star ⭐ the Gist - the amount of stars decides whether it makes the cut! You could also Retweet if you want :)

Get Flow

Type checking JavaScript with Flow

JavaScript is a dynamic language, and there's nothing wrong with that. It allows quick iteration and lowers barriers. However, sometimes some compile-time type checking is just what you need to keep your code in line and give yourself the confidence to build bigger and faster. Flow gives the best of both worlds. You can have normal JavaScript but you can also add types where they're helpful, and it adds zero cost at runtime. In this talk I'll show Flow as it applies to a Redux & React codebase.

@almost
almost / notebook.sh
Last active April 16, 2018 09:33 — forked from yangj1e/notebook.sh
Deploy Jupyter Notebook server
#!/usr/bin/env bash
cd ~
wget http://repo.continuum.io/archive/Anaconda3-4.1.0-Linux-x86_64.sh
bash Anaconda3-4.1.0-Linux-x86_64.sh -b
echo 'PATH="/home/ubuntu/anaconda2/bin:$PATH"' >> .bashrc
. .bashrc
jupyter notebook --generate-config
@almost
almost / no-semis.js
Created November 25, 2015 13:46
Lack of semi-colons let line 2 and 3 combine. Runtime error :(
export function pagebounds2screen({x, y, width, height}) {
let ratio = window.devicePixelRatio
[x,y] = page2screen(x, y)
width = width * ratio
height = height * ratio
return {x, y, width, height}
}
@almost
almost / word-parse.py
Last active November 22, 2015 23:07
Parse words from a string of smushed together words with a single regexp
# Solving the same problem as
# http://blogs.perl.org/users/ingy_dot_net/2015/11/perl-regular-expression-awesomeness.html
# but using a Trie instead of regexps, without backtracking and in
# Python
from itertools import groupby
phrase = "yougotmailmanners"
def makeTrie(words, prefix=""):
node = {}
@almost
almost / async-await-lightning-talk.md
Last active September 12, 2019 09:07
Reactive 2015 Lightning Talk Proposal: Pyramids be gone!: ES7 Async Function

This is a proposal for a lightning talk at the Reactive 2015 conference.

NOTE: If you like this, star ⭐ the Gist - the amount of stars decides whether it makes the cut! You could also Retweet if you want :)

Pyramids be gone!

ES7 Async Functions

JavaScript is getting async functions (or already has them if you count Babel.JS) and with them a way to finally slay the evil pyramid. This new language feature lets you write asynchronous code that almost looks synchronous, while maintaining the same semantics as promises. This lets you shed your .then and .catch boilerplate and escape those nested callbacks in favour of clean, explicit, maintainable code.

@almost
almost / StoreWatchMixin.js
Last active August 29, 2015 14:26
A simple mixin for getting state from Flux stores in a React view. An example of how simple it can be. You're probably better off using a full library though...
"use strict";
var shallowEqual = require('shallow-equals');
// Create a mixin for a component that depends on a store(s) for
// state. Component should define getStateFromStores
module.exports = function StoreWatchMixin(...stores) {
return {
componentDidMount () {
stores.forEach((store) => {
store.addListener('change', this.onStoreChange);
@almost
almost / flow-type-at-pos.el
Last active June 9, 2016 20:57
Use flow.js to show type signature for value under cursor whenever the cursor is idle for more than half a second (assumes espresso-mode.el)
(defun call-process-on-buffer-to-string (command)
(with-output-to-string
(call-process-region (point-min) (point-max) shell-file-name nil standard-output nil shell-command-switch command)))
(defun flow-type ()
(interactive)
(let* ((info (json-read-from-string
(call-process-on-buffer-to-string
(format "flow type-at-pos --json %d %d" (line-number-at-pos) (current-column)))))
@almost
almost / 1-react-native-simulator-and-device.md
Last active November 17, 2022 14:05
Test React Native on the simulator and on a device without editing the code each time!

In the default React Native app scaffolding you have to edit AppDelegate.m to change where it loads the code if you want to test on your device. I use the following snippet to detect if it's being compiled for Debug or Production and for the Simulator or a device. For Production it uses a copy of the code included in the bundle, for Debug on the simualtor it loads from a server on localhost and for Debug on a device it loads from a server on a given IP address.

NOTE: You need to edit YOUR-IP-HERE and change it to the IP to load the code from when in Debug mode on a device. You could use a service like ngrok to make this work from anywhere.

  NSURL *jsCodeLocation;

  // Loading JavaScript code
  #if DEBUG
    // For Debug build load from development server. Start the server from the repository root:
@almost
almost / MyCustomView.m
Last active April 15, 2018 05:47
Examples from Custom iOS Views in React Native on almostobsolete.net
#import "MyCustomView.h"
@implementation MyCustomView
{
UIColor *squareColor;
}
- (void)setIsRed:(BOOL)isRed
{
squareColor= (isRed) ? [UIColor redColor] : [UIColor greenColor];
play=p=function (s) {
return s.replace(/\b./g, p.call.bind(s.toUpperCase))
}