Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / 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 / bing_search.rb
Last active February 4, 2016 17:30
Getting results from Bing Search new API! (Azure data market key approach)
require 'net/http'
require 'json'
module Bing
class Search
BASE_URL = "https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/Composite?"
SINGLE_QUOTE_ENCODED = "%27"
def initialize(api_key)
@api_key = api_key
@Sam-Serpoosh
Sam-Serpoosh / swap_pairs.clj
Created July 10, 2013 01:58
Cool way of swapping pairs in a collection in Clojure!!!
(defn swap-pairs [items]
(into (empty items)
(interleave (take-nth 2 (drop 1 items))
(take-nth 2 items))))
(swap-pairs [1 2 3 4 5 6]) ;=> [2 1 4 3 6 3]
@Sam-Serpoosh
Sam-Serpoosh / mutator.rb
Created January 10, 2013 00:57
Very simple Mutator for the concept of Mutation-Testing in Ruby! Just for fun :)
class Mutator
def initialize(ruby_file)
@ruby_file = ruby_file
end
def run_test
spec_file_name = @ruby_file.split(/\./)[0]
system("rspec #{spec_file_name}_spec.rb")
end
@Sam-Serpoosh
Sam-Serpoosh / pair-car-cdr.lisp
Created November 20, 2012 04:54
Just a very simple program from SICP book both in Lisp and Ruby to show how much more elegant the code will be when the philosophy of "Code as Data" exist in a language!
(define make-pair (a, b)
(lambda (pick)
(cond (= pick 1) a)
(cond (= pick 2) b)))
(define car (x) (x 1))
(define cdr (x) (x 2))
@Sam-Serpoosh
Sam-Serpoosh / closest_to_zero.rb
Created November 14, 2012 05:09
Closest to zero! Just a short program for fun!
class Closest
MAX = 2000000000000
def to_zero(input)
raise IllegalArgumentException if input.nil? || input.empty?
closest_to_zero = MAX
@least_distance = MAX
input.each do |num|
if closer_to_zero(num)