Skip to content

Instantly share code, notes, and snippets.

💭
10x Ninja Rockstar

Adam Loving adamloving

💭
10x Ninja Rockstar
View GitHub Profile
@adamloving
adamloving / node-instagram-search.js
Created Mar 4, 2016
Node instagram search example (including getting an access token)
View node-instagram-search.js
const Instagram = require('instagram-node-lib')
Instagram.set('client_id', 'xxx')
Instagram.set('client_secret', 'xxx')
// to get an access_token
let url = Instagram.oauth.authorization_url({
redirect_uri: 'http://mysite.com'
})
@adamloving
adamloving / base-neg-2.js
Created Feb 8, 2016
Toptal Codility Problem: Convert to-and-from base -2
View base-neg-2.js
'use strict';
function toDecimal(A) {
var sum = 0;
for (var i = 0; i < A.length; i++) {
sum += A[i] * Math.pow(-2, i);
}
return sum;
}
@adamloving
adamloving / gist:9c1e9dfe70b339dcba7b
Created Feb 4, 2016
Codility binary gap solution
View gist:9c1e9dfe70b339dcba7b
// wrong for trailing 0s, ...
// 6, 328, n=16=2**4 and n=1024=2**10
// n=51712=110010100000000_2 and n=20=10100_2
function solution(N) {
var max = 0;
var currentLength = 0;
while (N > 0) {
var remainder = N % 2;
inGap = remainder === 0;
@adamloving
adamloving / react-toggle.js
Created Oct 22, 2015
Bootstrap button group toggle react js example
View react-toggle.js
var ProjectForm = React.createClass({
getInitialState: function() {
return { headerText: 'nothing' }
},
handleToggleChange: function(value) {
this.setState({ headerText: value.toUpperCase() })
},
render: function() {
return (
<form>
@adamloving
adamloving / when-spread-example.coffee
Created Sep 24, 2015
When.js spread function turns array of parameters into arguments.
View when-spread-example.coffee
w = require 'when'
w.all [
w.resolve(1)
w.resolve(2)
]
.then console.log
w.all [
w.resolve('a')
@adamloving
adamloving / promise-chain-catch.coffee
Last active Sep 24, 2015
When using when.js, should you put the catch first or last in the promise chain?
View promise-chain-catch.coffee
w = require 'when'
# When using when, should you put the catch first or last in the promise chain?
# You should put it last if you want it to end the chain before your thens run.
doSomethingAndFail = (n) ->
console.log 'fail', n
w.reject(n)
doSomethingAndFail(1)
@adamloving
adamloving / compare-contacts.rb
Created Sep 4, 2015
Ruby example code for finding the intersection of two CSV files full of contacts (exported from Google contacts).
View compare-contacts.rb
#!/usr/bin/ruby
# Usage: compare-contacts.rb [file1] [file2]
require 'csv'
def find_email_column_indexes(row)
[
row.index('E-mail Address'),
row.index('E-mail 2 Address'),
row.index('E-mail 3 Address'),
row.index('E-mail 1 - Value'),
@adamloving
adamloving / javascript.swift
Created Jun 25, 2015
running Javascript from Swift using JavaScriptCore (try this in a playground)
View javascript.swift
import JavaScriptCore
let context = JSContext()
context.evaluateScript("var num = 5 + 5")
context.evaluateScript("var names = ['Grace', 'Ada', 'Margaret']")
context.evaluateScript("var triple = function(value) { return value * 3 }")
context.evaluateScript("names.sort()")
let tripleNum: JSValue = context.evaluateScript("triple(num)")
@adamloving
adamloving / redis-scan-promise-iterate.js
Created Apr 13, 2015
Redis scan javascript promise (when) example
View redis-scan-promise-iterate.js
var count = 0;
when.iterate(function(args) {
var cursor = parseInt(args[0]);
return redis.whenZScan('mykey', cursor || 0);
}, function predicate(args) {
var cursor = parseInt(args[0]);
return cursor === 0;
}, function handler(args) {
if (args.length > 1) {
@adamloving
adamloving / swift-command-line.swift
Created Mar 27, 2015
example swift command line script
View swift-command-line.swift
#!/usr/bin/env xcrun swift -i
println("Process.arguments gave args:")
for s in Process.arguments {
println(s)
}
You can’t perform that action at this time.