Skip to content

Instantly share code, notes, and snippets.

@prashcr
prashcr / centerpane.vim
Created March 30, 2017 09:26
vim center align
function CenterPane()
lefta vnew
wincmd w
exec 'vertical resize '. string(&columns * 0.75)
endfunction
@prashcr
prashcr / gen_proxy.sh
Last active May 24, 2016 08:08
Bash script to generate reverse REST API proxy from .proto files
GO_SRC="$GOPATH/src/github.com/prashcr/proxy";
mkdir -p $GO_SRC &&
protoc -I/usr/local/include -I. \
-I$GOPATH/src \
-I$GOPATH/src/github.com/gengo/grpc-gateway/third_party/googleapis \
--go_out=Mgoogle/api/annotations.proto=github.com/gengo/grpc-gateway/third_party/googleapis/google/api,plugins=grpc:$GO_SRC \
protos/*.proto &&
protoc -I/usr/local/include -I. \
-I$GOPATH/src \
-I$GOPATH/src/github.com/gengo/grpc-gateway/third_party/googleapis \
@prashcr
prashcr / luhn.ex
Created May 6, 2016 05:04
Luhn's algorithm to validate credit card numbers
defmodule Luhn do
def validate(num) do
digits = Integer.digits(num)
len = length digits
digits
|> Stream.with_index
|> Enum.reverse
|> Enum.reduce(0, fn {digit, index}, acc ->
if rem(len - index, 2) == 0 do
@prashcr
prashcr / luhn.js
Last active May 6, 2016 04:28
Luhn's algorithm to validate credit card numbers
function validate(n){
return n.toString().split('').map(Number).reduceRight(function (prev, digit, index, arr) {
if ((arr.length - index) % 2 === 0) {
return prev + (digit * 2).toString().split('').map(Number).reduce((a, b) => a + b)
}
else {
return prev + digit
}
}, 0) % 10 === 0
}
@prashcr
prashcr / mcss.css
Created April 22, 2016 04:01
Materialize v0.97.6 namespaced under `mcss`
.mcss .waves-effect,.mcss a{-webkit-tap-highlight-color:transparent}.mcss .picker__table,.mcss table{border-collapse:collapse;border-spacing:0}.mcss{}.mcss .materialize-red{background-color:#e51c23!important}.mcss .materialize-red-text{color:#e51c23!important}.mcss .materialize-red.lighten-5{background-color:#fdeaeb!important}.mcss .materialize-red-text.text-lighten-5{color:#fdeaeb!important}.mcss .materialize-red.lighten-4{background-color:#f8c1c3!important}.mcss .materialize-red-text.text-lighten-4{color:#f8c1c3!important}.mcss .materialize-red.lighten-3{background-color:#f3989b!important}.mcss .materialize-red-text.text-lighten-3{color:#f3989b!important}.mcss .materialize-red.lighten-2{background-color:#ee6e73!important}.mcss .materialize-red-text.text-lighten-2{color:#ee6e73!important}.mcss .materialize-red.lighten-1{background-color:#ea454b!important}.mcss .materialize-red-text.text-lighten-1{color:#ea454b!important}.mcss .materialize-red.darken-1{background-color:#d0181e!important}.mcss .materialize-red
@prashcr
prashcr / presidents.js
Last active April 13, 2016 03:21
Given JSON containing data for US presidents, finds the year(s) in which the maximum number of US presidents were alive. Question inspired by https://www.quora.com/Are-there-really-programmers-with-computer-science-degrees-who-cannot-pass-the-FizzBuzz-test/answer/Gayle-Laakmann-McDowell?srid=z46k
var presidents = [
{"number":1,"president":"George Washington","birth_year":1732,"death_year":1799,"took_office":"1789-04-30","left_office":"1797-03-04","party":"No Party"},
{"number":2,"president":"John Adams","birth_year":1735,"death_year":1826,"took_office":"1797-03-04","left_office":"1801-03-04","party":"Federalist"},
{"number":3,"president":"Thomas Jefferson","birth_year":1743,"death_year":1826,"took_office":"1801-03-04","left_office":"1809-03-04","party":"Democratic-Republican"},
{"number":4,"president":"James Madison","birth_year":1751,"death_year":1836,"took_office":"1809-03-04","left_office":"1817-03-04","party":"Democratic-Republican"},
{"number":5,"president":"James Monroe","birth_year":1758,"death_year":1831,"took_office":"1817-03-04","left_office":"1825-03-04","party":"Democratic-Republican"},
{"number":6,"president":"John Quincy Adams","birth_year":1767,"death_year":1848,"took_office":"1825-03-04","left_office":"1829-03-04","party":"Democratic-Republican"},
{"number":7,"president":
@prashcr
prashcr / presidents.js
Created April 13, 2016 03:01
Given JSON containing data for US presidents, finds the year(s) in which the maximum number of US presidents were alive.
var presidents = [
{"number":1,"president":"George Washington","birth_year":1732,"death_year":1799,"took_office":"1789-04-30","left_office":"1797-03-04","party":"No Party"},
{"number":2,"president":"John Adams","birth_year":1735,"death_year":1826,"took_office":"1797-03-04","left_office":"1801-03-04","party":"Federalist"},
{"number":3,"president":"Thomas Jefferson","birth_year":1743,"death_year":1826,"took_office":"1801-03-04","left_office":"1809-03-04","party":"Democratic-Republican"},
{"number":4,"president":"James Madison","birth_year":1751,"death_year":1836,"took_office":"1809-03-04","left_office":"1817-03-04","party":"Democratic-Republican"},
{"number":5,"president":"James Monroe","birth_year":1758,"death_year":1831,"took_office":"1817-03-04","left_office":"1825-03-04","party":"Democratic-Republican"},
{"number":6,"president":"John Quincy Adams","birth_year":1767,"death_year":1848,"took_office":"1825-03-04","left_office":"1829-03-04","party":"Democratic-Republican"},
{"number":7,"president":
module.exports = {
// You can also include other presets
presets: ['es2015'],
plugins: [
require('babel-plugin-syntax-class-properties'),
require('babel-plugin-syntax-decorators'),
require('babel-plugin-syntax-object-rest-spread'),
// You can pass parameters using an array syntax
[
@prashcr
prashcr / paginate.js
Created March 2, 2016 05:33
Poor man's knex/bookshelf pagination
var Promise, defaultPageSize, paginate;
Promise = require('bluebird');
defaultPageSize = 20;
paginate = function(knex) {
return function(query, paginationOptions, options) {
var limit, model, offset, page, pageSize, totalPromise, totalQuery;
if (query.fetchAll != null) {
@prashcr
prashcr / calc.js
Created February 26, 2016 08:54
First Code Academy exercise
const readline = require('readline')
const prices = {
MAX: 1000,
MAC: 1500,
MAE: 2000,
MAL: 3000,
TAX: 2700,
TAC: 3900,
TAE: 5100