Skip to content

Instantly share code, notes, and snippets.

View tjmw's full-sized avatar

Tom Wey tjmw

View GitHub Profile
@tjmw
tjmw / fake_api.rb
Created February 29, 2024 09:36
Tiny rack app to fake a JSON API
require 'json'
class MyApp
def call(env)
request = Rack::Request.new(env)
headers = {
'Content-Type' => 'application/json',
'Access-Control-Allow-Origin' => '*',
}
@tjmw
tjmw / proc_comp.rb
Created March 22, 2019 12:35
Ruby proc composition example
TAX_RATE = 0.4
BENEFIT_COST = 1000
SALARY_IN_PENCE = 15_000 * 100
subtract_benefit = ->(salary) { salary - BENEFIT_COST }
pay_tax = ->(salary) { salary * (1 - TAX_RATE) }
calculate_take_home_pay = subtract_benefit >> pay_tax
puts "Pre-tax benefit: #{calculate_take_home_pay.call(SALARY_IN_PENCE)}"
@tjmw
tjmw / App.js
Created August 31, 2018 09:24
Async message sending and status updating
import React, { Component } from "react";
import logo from "./logo.svg";
import "./App.css";
const apiSendMessage = message =>
new Promise((resolve, reject) => {
const randomDelay = Math.floor(Math.random() * 3000);
setTimeout(() => {
const isSuccessful = Math.floor(Math.random() * 10) % 2 == 0;
@tjmw
tjmw / promise-retryer.js
Created August 3, 2018 08:59
Promise Retry
// @flow
class PromiseRetryer {
fn: () => Promise<*>;
maxTryCount: number;
retryDelay: number;
tries: number;
constructor(fn: () => Promise<*>, maxTryCount: number, retryDelay: number) {
this.fn = fn;
@tjmw
tjmw / TALK.md
Last active July 13, 2018 16:30
Elm Building Blocks Lightning Talk Notes

Elm Building Blocks

An introduction to a few FP concepts with Elm.

Currying

Functions are curried by default in Elm.

From Wikipedia: "the technique of translating the evaluation of a function that takes multiple arguments (or a tuple of arguments) into evaluating a sequence of functions, each with a single argument".

@tjmw
tjmw / and_then.rb
Last active February 6, 2018 08:57
Experiment in Result#and_then in Ruby
class Result
def initialize(operation, failure_test, *args)
@operation = operation
@failure_test = failure_test
@args = args
end
def and_then(next_operation)
if failure_test.call(result)
self
@tjmw
tjmw / active_model.rb
Created April 27, 2017 08:37
Quack like an ActiveRecord object
class FakeRecord
# ActiveModel plumbing to make `form_for` work
extend ActiveModel::Naming
include ActiveModel::Conversion
include ActiveModel::Validations
attr_accessor :foo, :bar
validates :foo, presence: true
highlight -O rtf order.rb --font-size 36 --style navajo-night | pbcopy
@tjmw
tjmw / find_with_perl_regexp.sh
Created January 19, 2016 10:05
find + perl regexp with a capture
find spec/renderers/ -type f -name '*.rb' -exec perl -p -i -e "s/loadPage\(\'(.*?)\'\)/loadPage('\1?client_bundle=abcde')/g" {} +
@tjmw
tjmw / events_emitter_mixin.js
Created November 18, 2015 12:38 — forked from ismasan/events_emitter_mixin.js
Simplified events emitter (JS)
/**
* Simple event dispatcher
*
* Example
*
* var MyConstructor = function () {
* var self = this
* var count = 0
* setInterval(function () {
* self.emit('tick', {count: count})