Skip to content

Instantly share code, notes, and snippets.

@christiantakle
christiantakle / closures?
Last active January 3, 2016 04:29
A function that illustrates closures and higher-order functions in JavaScript
//-- If you understand closures - you know why this will always return true
function f(value) {
return (function (copy) {
return copy === value;
}(value));
}
// Orignal ------------------------------------------------------------
var isFancy = true;
var fancy = document.querySelector("#commentsbtn");
fancy.addEventListener("click", function(e){
if (isFancy) {
document.body.classList.remove("fancy-comments");
isFancy = false;
}
else {
@christiantakle
christiantakle / leaf.hs
Created March 22, 2016 10:10 — forked from ali-abrar/index.html
Setting up Leaflet.js with Reflex.Dom
{-# LANGUAGE ForeignFunctionInterface, JavaScriptFFI #-}
import Reflex.Dom
import Data.Monoid
import GHCJS.Types
import GHCJS.Foreign
import GHCJS.DOM.Element
import GHCJS.DOM.Types
import Control.Monad.IO.Class
newtype LeafletMap = LeafletMap { unLeafletMap :: JSRef LeafletMap }
//-- Sum --------------------------------------------------------------
let
identity = 0,
mappend = (x,y) => x + y, // +
concatSum =
xs => xs.reduce(mappend, identity)
concatSum([1,2,3,4,5]) // => 15
//-- Product ----------------------------------------------------------
@christiantakle
christiantakle / navigation.haml
Last active March 28, 2016 12:03
Functional style, instead of adding conditional logic in the template use the fact that [] is a monoid. [a] + [] = [a] and [a] + [b] = [a,b]
:ruby
getAttrs = ->(link) {{href: link.fetch(:href), class: link.fetch(:class)}}
# Added to prevent typos in `tab_to_conf` and `mkLink`
events, following, calendar, overview, manager, followers, widget =
%w(events following calendar overview manager followers widget)
tab_to_conf =
{ calendar => [t('users.profile.calendar'), user_path(user)],
following => [t('users.profile.following'), following_user_path(user)],
@christiantakle
christiantakle / fix-free-cofree.hs
Created January 25, 2017 09:43 — forked from jtobin/fix-free-cofree.hs
Fix, Free, and Cofree
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE UndecidableInstances #-}
import Prelude hiding (succ)
newtype Fix f = Fix (f (Fix f))
deriving instance (Show (f (Fix f))) => Show (Fix f)
@christiantakle
christiantakle / blah.hs
Last active March 2, 2017 21:51
Haskell refactor, scale functions are made from minMax + period outside
-- old ----------------------------------------------------------------
mkGraphMeta (start,end) (aggregated,timeSeriesWithColor) =
(aggregated,) $ fromMaybe (Nothing,[]) $ case second unTimeSeries <$> (timeSeriesWithColor :: [(String,TimeSeries)]) of
[] -> Nothing
xss -> do
minMax@(vMin,vMax) <- getMinMax $ snd <$> (xss :: [(String,[(UTCTime,Double)])])
let
tw = end `diffUTCTime` start
fx = fromIntegral pWidth / tw
ph = fromIntegral pHeight
Postgres Cheat Sheet
Source: Postgresql Documentation
### shell commands
creatuser <user>
deletesuer <user>
createdb -O <user> -E utf8 -T <template> <db_name>
dropdb <db_name>
{-# LANGUAGE TupleSections #-}
import Control.Lens
data Expr
= Var String
| App Expr Expr
| Lam String Expr
deriving Show
@christiantakle
christiantakle / fizzbuzz.js
Created November 8, 2017 10:03
Different examples of fizzbuzz in javascript
const
whenDivisibleBy = (x, t, f) => n => (n % x == 0 ? t : f),
fizz = whenDivisibleBy(3, 'Fizz', ''),
buzz = whenDivisibleBy(5, 'Buzz', ''),
fizzBuzz = n => fizz(n) + buzz(n) || String(n);
//Array(...Array(100)).map((_,x) => fizzBuzz(x+1))