Skip to content

Instantly share code, notes, and snippets.

@honewatson
honewatson / functional.js
Last active February 7, 2017 23:08
Javascript - API and function.name pattern for closures and compose in javascript functional programming.
// closure
const add = x => y =>
x + y
// Bad - unlikely no auto complete/evaluation for your IDE/editor and also an anonymous function. Hard for debugging.
const addOne = add(1);
console.log(addOne.name); //=> "" // empty string
const __addOne = add(1);
@honewatson
honewatson / compose.js
Last active February 1, 2017 04:02
flowtype flow compose
// @flow
const immutable = <T>(item: T): T =>
typeof item === 'object' ? JSON.parse(JSON.stringify(item)) : item;
const immutableAll = (...args) =>
args.map(immutable)
const pipe = (fn: function, ...fns: Array<function>): function => <S>(...args: Array<S>) => {
return fns.reduce(
@honewatson
honewatson / ev.alt.2.js
Last active January 24, 2017 02:45
Basic ev manager
class App {
constructor() {
this._listeners = {};
}
ev(obj, listener) {
return this._listeners[name](this, obj);
}
listener(name, func) {
if(this._listeners[name]) {
throw `${name} already exists`;
@honewatson
honewatson / app.moon
Last active October 8, 2016 11:15
Basic Moon App
lapis = require "lapis"
db = require "lapis.db"
html = require "lapis.html"
queries = require "queries"
config = require("lapis.config").get!
import get_redis from require "lapis.redis"
import notNil from require "utils"
main = require "components.main"
get_set_redis = (widget) =>
@honewatson
honewatson / queries.moon
Created October 7, 2016 22:43
SQL Queries in Moonscript
toOneResult = (result) ->
if result
return result[1]
else
return result
blog = [[
SELECT `api_blog_option_terms`.`blog_option_name`, `api_blog_option_index`.`blog_option_title` FROM `api_blog_option_index`
JOIN api_blog_option_terms
ON api_blog_option_index.blog_option_id = api_blog_option_terms.blog_option_id
WHERE api_blog_option_index.domain_id = ?
@honewatson
honewatson / app.moon
Created October 7, 2016 22:20
Moonscript Lapis cached widget examle
lapis = require "lapis"
db = require "lapis.db"
html = require "lapis.html"
queries = require "queries"
config = require("lapis.config").get!
widgets = require "views.widgets"
import get_redis from require "lapis.redis"
import render_html from require "lapis.html"
import notNil from require "utils"
@honewatson
honewatson / json.nim
Created October 3, 2016 00:29
JSON Response Nim AsyncHttpServer
import asynchttpserver, asyncdispatch
import json
import cgi
var server = newAsyncHttpServer()
proc handler(req: Request) {.async.} =
let msg = %* {"message": "hello world"}
await req.respond(Http200, $msg, newHttpHeaders([("Content-Type","application/json")]))
@honewatson
honewatson / SetDepthDot.js
Created September 20, 2016 22:28
Set Object Value from Dot Notation
function setDepth(obj, path, value) {
var tags = path.split("."), len = tags.length - 1;
for (var i = 0; i < len; i++) {
obj = obj[tags[i]];
}
obj[tags[len]] = value;
}
@honewatson
honewatson / ImmutableDeepMerge.js
Created September 20, 2016 22:21
Immutable Deep Merge
export function isObject(item) {
return (item && typeof item === 'object' && !Array.isArray(item) && item !== null);
}
export default function mergeDeep(target, source) {
let output = Object.assign({}, target);
if (isObject(target) && isObject(source)) {
Object.keys(source).forEach(key => {
if (isObject(source[key])) {
if (!(key in target))
@honewatson
honewatson / peg.py
Created September 16, 2016 00:30 — forked from orlp/peg.py
PEG parser in Python.
from __future__ import unicode_literals
import sys
# We only use unicode in our parser, except for __repr__, which must return str.
if sys.version_info.major == 2:
repr_str = lambda s: s.encode("utf-8")
str = unicode
else:
repr_str = lambda s: s