This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
facebook { | |
id = "some_id" | |
secret = "some_secret" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Data.Configurator | |
import qualified Data.Configurator as C | |
main :: IO () | |
main = do | |
-- other code | |
cfg <- C.load [C.Required "../.settings.cfg"] | |
app_id <- C.lookup cfg "facebook.id" :: IO (Maybe String) | |
app_secret <- C.lookup cfg "facebook.secret" :: IO (Maybe String) | |
let base_url = "https://graph.facebook.com/oauth/access_token?" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Specifying lag = n | |
diff :: (Num a) => [a] -> Int -> [a] | |
diff [] n = [] | |
diff (x:xs) n | length (xs) > n = (xs!!n - x) : diff xs n | |
| otherwise = [] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- the diff() function from R | |
diff :: (Num a) => [a] -> [a] | |
diff [] = [] | |
diff (_:[]) = [] | |
diff (x:y:xs) = (y - x) : diff (y:xs) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Simple plpython function performing a full text search | |
CREATE OR REPLACE FUNCTION fts(sterm text) RETURNS SETOF text | |
AS $$ | |
sterms = plpy.quote_literal(sterm) | |
words = " & ".join(sterms.split(" ")) | |
res = plpy.execute("SELECT title FROM articles WHERE to_tsvector('english',title || ' ' || abstract) @@ to_tsquery('english', %s)" % words) | |
return [ (r["title"]) for r in res] | |
$$ LANGUAGE plpythonu; | |
-- dummy database of NYTimes articles (via their API) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require(fArma) | |
# assumes data is already a ts object | |
i = diff(data) | |
z = data[1] | |
m = mean(i) | |
n = length(data) | |
k = 3 | |
fit = armaFit(~ arma(1,1), data=i) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Collatz conjecture | |
collatz :: Integer -> [Integer] | |
collatz 1 = [1] | |
collatz n | even n = n : collatz (n `div` 2) | |
| otherwise = n : collatz ((3 * n) + 1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- prime factorization | |
-- definitely needs improvement :X | |
getPrimeFactor :: Integer -> [Integer] -> (Integer, Integer) | |
getPrimeFactor n (p:ps) | n `mod` p == 0 = (p, (n `div` p)) | |
| otherwise = getPrimeFactor n ps | |
getPrimeFactors :: Integer -> [Integer] -> [Integer] | |
getPrimeFactors n s | not (result == 1) = factor : getPrimeFactors result s | |
| otherwise = [factor] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
destruct :: Integer -> [Integer] | |
destruct 0 = [] | |
destruct x = destruct (x `div` 10) ++ [(x `mod` 10)] | |
construct :: [Integer] -> Integer | |
construct [] = 0 | |
construct (x:xs) = (x * (10 ^ (length xs))) + construct xs | |
-- *Main> construct (destruct 123456) | |
-- 123456 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Calculating a Fibonacci number (see: http://en.wikipedia.org/wiki/Fibonacci_number) | |
fib :: Integer -> Integer | |
fib 0 = 0 | |
fib 1 = 1 | |
fib n = fib (n - 1) + fib (n - 2) | |
-- Solving a quiz, I needed to generate a Fibonacci sequence up to a certain limit, e.g. fib n < limit | |
-- Usual recursion here is expensive and inefficient, takes too long. | |
fibLst :: Integer -> [Integer] | |
fibLst 0 = [] |