Skip to content

Instantly share code, notes, and snippets.

Thomas Parslow almost

Block or report user

Report or block almost

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@almost
almost / no-semis.js
Created Nov 25, 2015
Lack of semi-colons let line 2 and 3 combine. Runtime error :(
View no-semis.js
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 Nov 22, 2015
Parse words from a string of smushed together words with a single regexp
View word-parse.py
# 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 Sep 12, 2019
Reactive 2015 Lightning Talk Proposal: Pyramids be gone!: ES7 Async Function
View async-await-lightning-talk.md

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 Aug 29, 2015
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...
View StoreWatchMixin.js
"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 Jun 9, 2016
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)
View flow-type-at-pos.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 Aug 17, 2019
Test React Native on the simulator and on a device without editing the code each time!
View 1-react-native-simulator-and-device.md

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 Apr 15, 2018
Examples from Custom iOS Views in React Native on almostobsolete.net
View MyCustomView.m
#import "MyCustomView.h"
@implementation MyCustomView
{
UIColor *squareColor;
}
- (void)setIsRed:(BOOL)isRed
{
squareColor= (isRed) ? [UIColor redColor] : [UIColor greenColor];
View gist:285b634c863d749cd20c
play=p=function (s) {
return s.replace(/\b./g, p.call.bind(s.toUpperCase))
}
View golf.js
function play (x) {
x += ''
r = []
for (i = -1; ++i < x.length;)
r.push('zero two three five six seven eight'.split(' ')[x[i]])
return r.join(' ')
}
You can’t perform that action at this time.