Skip to content

Instantly share code, notes, and snippets.

View riston's full-sized avatar

Risto Novik riston

View GitHub Profile
@riston
riston / rps.js
Last active August 29, 2015 14:06
reverse polish stack
var isOp = function (op) {
return !!({
'+': true,
'-': true,
'*': true,
'/': true
}[op])
};
var applyOp = function (op, a, b) {
@riston
riston / color.js
Created January 2, 2015 22:27
Colorful developing :D
// Paste in browser developer tools
Array(100).join('.').split('.').map(function (a, index) { return (~~(Math.random()*(1<<24))).toString(16) }).forEach(function(color) { console.log("%c ", "background-color: #" + color) });
@riston
riston / sublime-user.json
Created January 22, 2015 07:34
Sublime conf
{
"auto_indent": true,
"bold_folder_labels": false,
"caret_style": "wide",
"color_scheme": "Packages/User/Monokai (SL).tmTheme",
"ensure_newline_at_eof_on_save": true,
"file_exclude_patterns":
[
"*.pyc",
"*.pyo",
@riston
riston / db-service.js
Created February 19, 2015 17:10
Database service module for Node.js
var config = require("config");
var pg = require("pg");
var PromiseB = require("bluebird");
PromiseB.promisifyAll(pg);
// To prevent data loss handle bigint as string
// https://github.com/brianc/node-postgres/pull/353
pg.defaults.parseInt8 = false;
@riston
riston / gist:9d2fafe3c9869d9ae79d
Last active August 29, 2015 14:18
Transforming object into array
var requests = { docs: [ 'hello' ], boards: [ 'R404' ] };
var fetchResultTransform = function (requests)
{
var resource = [];
Object.keys(requests).forEach(function (colName)
{
requests[colName].forEach(function (docName)
{
@riston
riston / promise.js
Created May 21, 2015 14:32
Promise example
var p1 = new Promise(function (resolve) {
return resolve(10);
});
var add = function (num) {
return new Promise(function (resolve) {
@riston
riston / all.js
Created June 19, 2015 12:58
Promise and handling all
var getProviderInfo = function (provider)
{
return new Promise(function (resolve, reject) {
var time = ~~(Math.random() * 2000) + 2000;
// Make request and get the data, simulate request api, no fail in our case
window.setTimeout(function () {
@riston
riston / example.go
Created July 6, 2015 21:12
Go concurrency example
s := func(name string) string {
time.Sleep(4 * time.Second)
fmt.Println("Do something", name)
return "Done"
}
results := []string{}
c := make(chan string)
go func() { c <- s("first") }()
@riston
riston / convert.go
Created July 25, 2015 09:02
Convert map[string]interface{}
var profile OAuthProfile
MapToStruct(result, &profile)
fmt.Println("Extracted", profile)
func MapToStruct(m map[string]interface{}, val interface{}) error {
tmp, err := json.Marshal(m)
@riston
riston / average.js
Created September 9, 2015 20:30
Exponential moving average for streams
// Exponential moving average
function average (alpha) {
var last;
alpha = alpha || 3/4;
return function (input) {
if (!last) {
last = input;