Skip to content

Instantly share code, notes, and snippets.

@Sam-Serpoosh
Sam-Serpoosh / vimrc
Created February 24, 2014 23:37
Switching between production and test file in the same directory back and forth! (Works for Ruby & Python)
function! OpenTestOrProduction()
let current_file_without_extension = expand("%:r")
let filename_parts = split(current_file_without_extension, "_")
let target_file = ""
if IsInTestFile()
let main_name_parts = filename_parts[0:-2]
let target_file = CreateTargeFilename(main_name_parts)
else
let target_file = CreateTargeTestFilename(current_file_without_extension)
end
@Sam-Serpoosh
Sam-Serpoosh / message_to_csv_for_export.rb
Last active August 29, 2015 13:59
Export Message Data in CSV format, Including the Annotation data if available.
def self.to_csv(options: { col_sep: "," } , column_names: self.column_names, messages: limit(1000))
CSV.generate(options) do |csv|
csv << column_names.push(*["Annotation Reason", "Annotation Notes"])
messages.each do |message|
attributes = message.attributes.values_at(*column_names)
attributes.push(*get_annotation_data_for_message(message))
csv << attributes
end
end
end
@Sam-Serpoosh
Sam-Serpoosh / klesli_category_for_optional.hs
Created December 26, 2014 22:02
Implementation of "Kleisli Category" for "Optional" type! (based on the challenge at the end of this article -> http://bartoszmilewski.com/2014/12/23/kleisli-categories/)
data Optional a = Invalid | Valid a
deriving (Show)
optionalToString :: (Show a) => Optional a -> String
optionalToString Invalid = "Not Valid"
optionalToString (Valid value) = show value
safeRoot :: Double -> Optional Double
safeRoot x
| x >= 0 = Valid (sqrt x)
@Sam-Serpoosh
Sam-Serpoosh / ParseJson.hs
Created February 13, 2015 04:07
Sample code for JSON parsing using Aeson in Haskell! From the blog post -> http://blog.raynes.me/blog/2012/11/27/easy-json-parsing-in-haskell-with-aeson/
{-# LANGUAGE OverloadedStrings #-}
module PraseJsonSample where
import Data.Aeson ((.:), (.:?), decode, FromJSON(..), Value(..))
import Control.Applicative ((<$>), (<*>))
import Data.Time.Format (parseTime)
import Data.Time.Clock (UTCTime)
import System.Locale (defaultTimeLocale)
import Control.Monad (liftM)
import qualified Data.HashMap.Strict as HM
@Sam-Serpoosh
Sam-Serpoosh / Sized.hs
Created March 3, 2015 22:39
Type Deduction problem in Haskell (type class and instance declarations)
{-# LANGUAGE GeneralizedNewtypeDeriving, FlexibleInstances #-}
module Sized where
import Data.Monoid
newtype Size = Size { getSize :: Int }
deriving (Eq, Ord, Show, Num)
class Sized a where
size :: a -> Size
@Sam-Serpoosh
Sam-Serpoosh / BindOption.scala
Last active November 10, 2015 05:20
A pattern of converting a one-track-input Function to a two-track-input Function for better function-composition-ability in the presence of Option types. Inspired by Maybe Monad.
// Convert a One-Track-Input Function to Two-Track-Input Function for better
// Composability of Function in the presence of Option types
def bindOption[A, B](f: A => Option[B]): Option[A] => Option[B] = {
(input) => input match {
case Some(a) => f(a)
case None => None
}
}
@Sam-Serpoosh
Sam-Serpoosh / FunWithFunctors.hs
Last active November 29, 2015 21:06
This code snippet shows a very powerful concept provided by Haskell's type system.
twice :: Int -> Int
twice = (*2)
twicePossibleNums :: Maybe [Int] -> Maybe [Int]
twicePossibleNums = (fmap . fmap) twice
main :: IO ()
main = putStrLn $ show $ twicePossibleNums (Just [1, 2, 3]) -- => Just [2, 4, 6]
-- How it Works?
@Sam-Serpoosh
Sam-Serpoosh / ComposeFunc.hs
Created June 7, 2016 23:20
Implementation of Functor and Applicative Functor for a Compose type.
{-# LANGUAGE InstanceSigs #-}
newtype Compose f g a = Compose { getCompose :: f (g a) }
instance (Functor f, Functor g) => Functor (Compose f g) where
fmap :: (a -> b) -> Compose f g a -> Compose f g b
fmap f (Compose fga) = Compose $ fmap (fmap f) fga
instance (Applicative f, Applicative g) => Applicative (Compose f g) where
pure :: a -> Compose f g a
@Sam-Serpoosh
Sam-Serpoosh / k_largest_elements.py
Last active September 5, 2016 19:41
Find K largest elements in a given collection of numbers.
import sys
import random
# O(n * lg(k))
def k_largest_elements(arr, k):
k_elems = arr[0:k]
k_elems_heaped = min_heapify(k_elems)
for idx in range(k + 1, len(arr)):
if arr[idx] > k_elems_heaped[0]:
k_elems_heaped[0] = arr[idx]
@Sam-Serpoosh
Sam-Serpoosh / sub1.py
Last active October 17, 2016 03:58
Implementing SUB1 function in Lambda Calculus using Python via the "Wisdom Tooth Trick"!
# This is the implementation of SUB1 in Lambda Calculus (LC)
# using the "Wisdom Tooth" trick! Apparently for some time,
# even Alonzo Church didn't believe SUB1 is a feasible operation
# in LC until one day his student Kleene came up with this idea.
# It's called "Wisdom Tooth" because he came up with it while he
# was at dentist :)
#
# It simply starts counting up from ZERO and at each step, remebers
# the previous value and when reaches the target, returns the previous
# value as the SUB1 result: