Skip to content

Instantly share code, notes, and snippets.

View sbycrosz's full-sized avatar
💭
I may be slow to respond.

Sam Aryasa sbycrosz

💭
I may be slow to respond.
View GitHub Profile
@sbycrosz
sbycrosz / app.js
Created June 16, 2014 04:42
Local image placeholder service
// Local image placeholder service
// Usage: <img src="http://localhost:9999/300/400" />
var express = require('express'),
app = express(),
gm = require('gm');
app.get('/:width/:height', function(request, response){
var width = request.params.width,
height = request.params.height,
@sbycrosz
sbycrosz / GameOfLife.scala
Last active August 29, 2015 14:25
Game of Life
package kata
object GameOfLife {
type World = List[List[Int]]
type Position = (Int, Int)
val DEAD = 0
val ALIVE = 1
def run(world: World): World = {
// ==UserScript==
// @name Leave Balance Calculator
// @namespace
// @version 0.0.1
// @description
// @author Sam
// @match https://na32.salesforce.com/a1W?rlid=00N50000002ejrH&id=a0y50000000onx0
// @grant none
// ==/UserScript==
case class Point(x: Double, y: Double)
def distance(pair: Option[(Point, Point)]): Double = {
pair match {
case None => Double.MaxValue
case Some((p1, p2)) => Math.sqrt( Math.pow(p1.x - p2.x, 2) + Math.pow(p1.y - p2.y, 2) )
}
}
def closestPair(points: List[Point]): Option[(Point, Point)] = {
// Implementation of Tree Traversal in Scala
package kata
trait Node {
def postorder: Stream[Node]
def inorder: Stream[Node]
def preorder: Stream[Node]
}
// A solution for disjoint subset problem (P27)
// which is described in 99 Scala problem http://aperiodic.net/phil/scala/s-99/
package kata
object Kata {
def group3(names: Set[String]): List[List[Set[String]]] =
group(List(2, 3, 4), names)
def group(groupSizes: List[Int], names: Set[String]): List[List[Set[String]]] =
#!/bin/bash
# Script for quickly change wallpaper when people leaves their mac unlocked
curl http://i64.tinypic.com/22amc9.jpg > 'wp.jpg'
sqlite3 ~/Library/Application\ Support/Dock/desktoppicture.db "update data set value = '${PWD}/wp.jpg'";
killall Dock;
@sbycrosz
sbycrosz / SimpleReducer_AsyncInRedux.js
Created March 8, 2018 07:16
SimpleReducer_AsyncInRedux.js
// todo/actions.js
export const getTodos = (dispatch) => () => {
dispatch({ type: 'GET_TODOS_REQUEST' });
return fetch('/api/v1/todos')
.then((todos) => dispatch({ type: 'GET_TODOS_SUCCESS', payload: todos })
.catch((error) => dispatch({ type: 'GET_TODOS_FAILURE', payload: error, error: true });
};
// todo/reducer.js
const initialState = { todos: [] };
@sbycrosz
sbycrosz / SimpleReducer_LoadingReducer.js
Last active May 4, 2020 14:24
SimpleReducer_LoadingReducer.js
// api/loadingReducer.js
const loadingReducer = (state = {}, action) => {
const { type } = action;
const matches = /(.*)_(REQUEST|SUCCESS|FAILURE)/.exec(type);
// not a *_REQUEST / *_SUCCESS / *_FAILURE actions, so we ignore them
if (!matches) return state;
const [, requestName, requestState] = matches;
@sbycrosz
sbycrosz / SimpleReducer_ConnectedComponent.js
Last active March 8, 2018 09:13
SimpleReducer_ConnectedComponent.js
// api/selectors.js
import _ from 'lodash';
export const createLoadingSelector = (actions) => (state) => {
// returns true only when all actions is not loading
return _(actions)
.some((action) => _.get(state, `api.loading.${action}`));
};
// components/todos/index.js
import { connect } from 'react-redux';