Skip to content

Instantly share code, notes, and snippets.

View thejsj's full-sized avatar

Jorge Silva thejsj

View GitHub Profile
@thejsj
thejsj / tableCreate.js
Created May 26, 2015 23:35
A simple script showing RethinkDB's JavaScript API
var r = require('rethinkdb');
var assert = require('assert');
// Sample table names
var sampleTableName1 = 'sample_table_' + Math.floor(Math.random() * 10000);
var sampleTableName2 = 'sample_table_' + Math.floor(Math.random() * 10000);
// Create a table using callbacks
r.connect(function (err, conn) {
r.db('test').tableCreate(sampleTableName1)
import Data.List.Split
import Data.Char
import Data.Maybe
import Text.Read
converToFloat :: String -> Maybe Float
converToFloat x = readMaybe x :: Maybe Float
convertStringsToFloats :: [String] -> [Float]
convertStringsToFloats x = map fromJust $ filter (not . null) $ map converToFloat x
github-get-by-id () {
curl -s https://api.github.com/user/$1 | python -m json.tool
}
github-get-by-username () {
curl -s https://api.github.com/users/$1 | python -m json.tool
}
github-get-by-orgname () {
curl -s https://api.github.com/orgs/$1 | python -m json.tool
@thejsj
thejsj / token.js
Created August 25, 2016 20:44
Stripe Node Shortcuts
require('loadenv')()
const stripe = require('stripe')(process.env.STRIPE_API_KEY)
var token1, token2
stripe.tokens.create({ card: { "number": '4242424242424242', "exp_month": 12, "exp_year": 2017, "cvc": '123' }}).then(x => token1 = x)
stripe.tokens.create({ card: { "number": '4242424242424242', "exp_month": 1, "exp_year": 2020, "cvc": '456' }}).then(x => token2 = x)
@thejsj
thejsj / fizzbuzz.hs
Last active August 31, 2016 04:50
FizzBuzz in Haskell
fizzbuzz :: Int -> String
fizzbuzz x | mod x 15 == 0 = "fizzbuzz"
| mod x 5 == 0 = "fizz"
| mod x 3 == 0 = "buzz"
| otherwise = show x
printAll [] = return ()
printAll (x:xs) = putStrLn x >> printAll xs
main = do
@thejsj
thejsj / index.js
Created November 21, 2016 03:06
Return vs Else Example
'use strict'
// All these functions behave the same way
const getHello = function (a) {
if (a === true) {
return 'hello'
}
else {
return 'wow'
@thejsj
thejsj / if-else-case.js
Created November 21, 2016 06:00
Ejemplos Para Jaime
var user = prompt("Where you at?").toUpperCase();
var isSingle = prompt("Are you single?").match(/yes/);
var isHappy = prompt("Are you happy?").match(/yes/);
switch(user) {
case 'HOME':
console.log("it is nice to have a home");
break;
@thejsj
thejsj / index.js
Created December 12, 2016 07:02
Javascript Cloning for Arrays
var a = [1, 2, 3]
var b = a // b now points to the same array as a
a === b // true
var c = [...a] // Makes a shallow copy of the array
c === a // false
a.push(4)
b // [1, 2, 3, 4]
c // [1, 2, 3]
a = null
b // [1, 2, 3, 4] Still point to the same array
@thejsj
thejsj / permutate.js
Last active January 23, 2017 00:50
All permutations for an array
// http://stackoverflow.com/a/22063440/2684055
function allPermutations (arr) {
function permutate (res, value, key, arr) {
if (arr.length <= 1) return [value]
// Eliminate current value
let result = arr.slice(0, key).concat(arr.slice(key + 1))
.reduce(permutate, [])
.map(perm => [value].concat(perm))
return res.concat(result)
}
@thejsj
thejsj / sieve.js
Created January 23, 2017 00:59
Get all prime number up to N
function sieve (n) {
let all = [...Array(n).keys()].map(x => true)
for (let i = 2; i <= Math.sqrt(n); i += 1) {
if (all[i]) {
for (let j = i * i; j < n; j += i) {
all[j] = false
}
}
}
return all.map((x, i) => {