Skip to content

Instantly share code, notes, and snippets.

View diegocasmo's full-sized avatar
👨‍💻

Diego Castillo diegocasmo

👨‍💻
View GitHub Profile
@diegocasmo
diegocasmo / mine-remoteok-io-listings.js
Last active March 3, 2019 20:38
Text processing script to mine https://remoteok.io/ job listings' titles.
// Get job listings' titles
var listings = Array.from(document.getElementsByTagName('h2'))
.splice(3)
.map(function(x) { return x.innerText });
// Tokenize listings' titles
var keywords = [].concat.apply(
[],
listings.map(function(x) { return x.split(' ') }));
@diegocasmo
diegocasmo / dfs.spec.rb
Created March 20, 2017 16:02
Tests for the implementation of the depth first search algorithm.
require 'minitest/autorun'
require './depth_first_search_undirected'
describe DFS do
before do
graph = {
:a => [:b, :e],
:b => [:a],
:c => [:d, :g, :h],
@diegocasmo
diegocasmo / dfs.rb
Created March 20, 2017 16:01
An implementation of the depth first search algorithm for undirected graphs.
class DFS
def initialize(graph={})
@graph = graph
@visited = {}
end
# This implementation of depth-first search visits
# only the portion of the graph reachable from the
# starting vertex
@diegocasmo
diegocasmo / store.js
Created March 13, 2017 14:09
Example definition of a Redux store using a custom middleware
import {createStore, combineReducers, applyMiddleware} from 'redux'
import {redirectMiddleware} from '../middleware/redirect_middleware';
let todoApp = combineReducers(reducers);
let store = createStore(
todoApp,
applyMiddleware(redirectMiddleware) // Apply redirect middleware
);
@diegocasmo
diegocasmo / redirect_middleware.js
Created March 13, 2017 14:08
A Redux middleware to redirect a user after a TODO has been successfully deleted.
import {browserHistory} from 'react-router'
export const redirectMiddleware = createMiddleware([
{
// Redirect user to index page on successful TODO delete
action: TODO__DELETE__SUCCESS,
afterHandler: (storeAPI, action) => {
browserHistory.replace({pathname: '/'});
}
}
@diegocasmo
diegocasmo / create_middleware.js
Created March 13, 2017 14:07
Utility method that allows to create a Redux middleware and execute custom code before or after an action is dispatched.
import * as Immutable from 'immutable';
// Helper method for creating a middleware that handles the given set of actions
export function createMiddleware(handlers) {
return storeAPI => next => action => {
const actionHandler = Immutable.List(handlers).find(h => h.action.type === action.type);
// Execute custom middleware handler before the action is dispatched
if (actionHandler && actionHandler.beforeHandler) {
actionHandler.beforeHandler(storeAPI, action);
}
// Dispatch the action
@diegocasmo
diegocasmo / todo_delete_actions.txt
Created March 13, 2017 14:06
Todo DELETE actions.
TODO__DELETE__INIT // Dispatched when user clicks the delete button
TODO__DELETE__SUCCESS // Dispatched when a TODO has been successfully deleted
TODO__DELETE__FAILURE // Dispatched if there was a problem while attempting to delete a TODO
@diegocasmo
diegocasmo / fft.spec.rb
Last active March 6, 2017 19:57
Tests for the implementation of the 'The Fast Fourier Transform Algorithm'
require 'minitest/autorun'
require './fft'
describe FFT do
before do
@fft = FFT.new
end
describe "#fft" do
@diegocasmo
diegocasmo / fft.rb
Created March 6, 2017 19:41
Implementation of the 'The Fast Fourier Transform Algorithm'
class FFT
# Input: n coefficients
# Output: Point-wise representation of the n coefficients
# Vec size must be a power of 2
def fft(vec)
return vec if vec.size <= 1
# Split A(x) into its odd and even powers
a_even = vec.each_with_index.select { |_, i| i.even? }.map { |i, _| i }
a_odd = vec.each_with_index.select { |_, i| i.odd? }.map { |i, _| i }
@diegocasmo
diegocasmo / fft_pseudocode.txt
Last active June 9, 2022 20:50
Pseudocode of the 'The Fast Fourier Transform Algorithm'
function FFT(A, ω)
Input: Coefficient representation of a polynomial A(x) of degree ≤ n − 1, where n is a power of 2
Output: Value representation A(ω^0), . . . , A(ω^n−1)
if ω = 1: return A(1)
express A(x) in the form Ae(x^2) + xAo(x^2)
call FFT(Ae, ω^2) to evaluate Ae at even powers of ω
call FFT(Ao, ω^2) to evaluate Ao at odd powers of ω
for j = 0 to n − 1:
compute A(ω^j) = Ae(ω^2j) + ω^jAo(ω^2j)