Skip to content

Instantly share code, notes, and snippets.

View kevinadi's full-sized avatar

Kevin Adistambha kevinadi

  • MongoDB
  • Sydney, Australia
View GitHub Profile
@kevinadi
kevinadi / reimplementations.hs
Last active August 29, 2015 14:19
Haskell: learning how basic functions work
fold' :: (b->a->b) -> b -> [a] -> b
fold' f b [] = b
fold' f b (h:t) = fold' f (f b h) t
map' :: (a->b) -> [a] -> [b]
map' f lst = fold' (\b a -> b ++ [f a]) [] lst
map'' :: (a->b) -> [a] -> [b]
map'' f [] = []
map'' f (h:t) = f h : map'' f t
@kevinadi
kevinadi / split-to-chunks.hs
Created April 24, 2015 02:01
Haskell: split a string to chunks
chunks :: Int -> [a] -> [[a]]
chunks _ [] = []
chunks n xs = (fst c) : chunks n (snd c)
where c = splitAt n xs
chunks' :: Int -> [a] -> [[a]]
chunks' n = unfoldr (\b -> if null b then Nothing else Just (splitAt n b))
chunks'' :: Int -> [a] -> [[a]]
chunks'' n = takeWhile (not.null) . unfoldr (Just . splitAt n)
@kevinadi
kevinadi / split-with-char.hs
Created April 24, 2015 02:02
Haskell: split a string using a char
wordsWhen :: (Char -> Bool) -> String -> [String]
wordsWhen p s = case dropWhile p s of
"" -> []
s' -> w : wordsWhen p s''
where (w, s'') = break p s'
@kevinadi
kevinadi / split-with-substring.hs
Last active August 29, 2015 14:19
Haskell: split a string using a substring
splitStr :: Eq a => [a] -> [a] -> [[a]]
splitStr sub str = split' sub str [] []
where
split' _ [] subacc acc = reverse (reverse subacc:acc)
split' sub str subacc acc
| sub `isPrefixOf` str = split' sub (drop (length sub) str) [] (reverse subacc:acc)
| otherwise = split' sub (tail str) (head str:subacc) acc
@kevinadi
kevinadi / nodejs-mongodb.js
Last active May 4, 2016 07:26
NodeJS: Connect to MongoDB
// MongoClient -- callbacks
var assert = require('assert')
var MongoClient = require('mongodb').MongoClient
MongoClient.connect('mongodb://localhost:27017/test', function(err,db) {
assert.equal(null,err)
console.log('connected')
db.collection('test').find().toArray(function(err,res) {
assert.equal(null,err)
console.log(res)
})
@kevinadi
kevinadi / random_pwd.fish
Created May 2, 2016 06:09
Fish shell: create 5 random passwords
function random_pwd
for i in (seq 5)
cat /dev/urandom | env LC_CTYPE=C tr -dc 'a-zA-Z0-9' | fold -w 18 | head -n 1 | tr -d '\n' | fold -w 3 | tr '\n' '-'
echo ''
end
end
@kevinadi
kevinadi / unfoldr.scala
Created May 10, 2016 07:09
unfoldr in Scala
def unfoldr[A, B](seed: B)(func: B => Option[(A, B)]): Stream[A] =
func(seed) match {
case Some((a, b)) => a #:: unfoldr(b)(func)
case None => Stream.empty
}
/*
* Infinite sequence:
* val s = unfoldr (0) (b => Some(b,b+1))
* Fibonacci sequence:
@kevinadi
kevinadi / mongodb-query-plan-types.txt
Created May 12, 2016 23:00
MongoDB: query plan types
$ grep -R "::kStageType" *
src/mongo/db/exec/and_hash.cpp:const char* AndHashStage::kStageType = "AND_HASH";
src/mongo/db/exec/and_sorted.cpp:const char* AndSortedStage::kStageType = "AND_SORTED";
src/mongo/db/exec/cached_plan.cpp:const char* CachedPlanStage::kStageType = "CACHED_PLAN";
src/mongo/db/exec/collection_scan.cpp:const char* CollectionScan::kStageType = "COLLSCAN";
src/mongo/db/exec/count.cpp:const char* CountStage::kStageType = "COUNT";
src/mongo/db/exec/count_scan.cpp:const char* CountScan::kStageType = "COUNT_SCAN";
src/mongo/db/exec/delete.cpp:const char* DeleteStage::kStageType = "DELETE";
src/mongo/db/exec/distinct_scan.cpp:const char* DistinctScan::kStageType = "DISTINCT_SCAN";
src/mongo/db/exec/ensure_sorted.cpp:const char* EnsureSortedStage::kStageType = "ENSURE_SORTED";
@kevinadi
kevinadi / geodist.hs
Created May 25, 2016 02:06
Distance between two points on a great circle
-- Calculates distances between two points (a,b) and (x,y) on a great circle with radius r
-- Coordinates are in the form of (longitude,latitude)
--
-- Example: calculate distance between points (0,0) and (1,1) on earth:
-- ghci> geodist 6378.1 0 0 1 1
-- 157.42462387232553
--
-- Result should be identical to MongoDB's $geoNear
--
-- Formula was taken from http://andrew.hedges.name/experiments/haversine/
@kevinadi
kevinadi / mongo-binary.js
Created May 26, 2016 00:22
Save binary file in MongoDB
var mongoBinary = {
save : function(url, collection, filename) {
var fs = require('fs')
var Binary = require('mongodb').Binary
var MongoClient = require('mongodb').MongoClient
var assert = require('assert')
var doc = {