Skip to content

Instantly share code, notes, and snippets.

@Sam-Serpoosh
Sam-Serpoosh / TumblingEventTimeWindowTest.java
Last active August 12, 2019 04:03
Testing TumblingEventTimeWindow of Flink
package com.uber.gairos.flink.operator.flink_operator;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;
import lombok.ToString;
import org.apache.flink.api.common.ExecutionConfig;
import org.apache.flink.api.common.functions.AggregateFunction;
@Sam-Serpoosh
Sam-Serpoosh / SimpleParser.hs
Created March 13, 2017 06:12
Making a simple parametric Parser which is both a Functor and Applicative in Haskell!
{-# LANGUAGE InstanceSigs #-}
import Data.Char
import Control.Applicative
newtype Parser a = Parser { runParser :: String -> Maybe (a, String) }
instance Functor Parser where
fmap :: (a -> b) -> Parser a -> Parser b
fmap f pa = Parser $ \s -> fmap (first f) (runParser pa s)
@Sam-Serpoosh
Sam-Serpoosh / longest_sub_array_sum_k.py
Created December 11, 2016 21:01
Find the longest sub-array in which the sum of elements is equal to a given number.
# Shape a matrix and we only use the half
# in which the col-number >= row-number!
# m[i][j] stores the sum of values in the
# sub-array from i to j (i.e nums[i:(j + 1)])
# and to calculate each new sub-array we can
# leverage the previously calculated sub-array
# which didn't have this new element and add
# this new element on top of that:
#
# m[i][j] = m[i][j - 1] + nums[j]
@Sam-Serpoosh
Sam-Serpoosh / Pred.hs
Last active October 23, 2016 04:56
PRED/SUB1 function implemented in pure Lambda Calculus using Haskell!
zero = \f -> \x -> x
mySucc = \n -> \f -> \x -> f $ n f x
add = \n -> \m -> n mySucc m
mkPair = \a -> \b -> \s -> s a b
myFst = \a -> \b -> a
mySnd = \a -> \b -> b
step = \p -> mkPair (p mySnd) (mySucc (p mySnd))
nSteps = \n -> \p -> n step p
zz = mkPair zero zero
myPred = \n -> (nSteps n zz) myFst
@Sam-Serpoosh
Sam-Serpoosh / factorial.py
Last active October 17, 2016 22:37
Full implementation of Factorial in Lambda Calculus using Python.
#!/usr/bin/env python
# As you know there is NO Assignment in Lambda Calculus (LC),
# and all those functions must be inlined using LC's
# substitution rule. But if I do that this code will look extremely
# gross and unreadable. So I keep it this way for comprehensibility
# reasons.
#
# This was SO MUCH fun to implement and quite an interesting puzzle
# to solve. This whole thing was completely inspired by Gary Bernhardt's
@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:
@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 / 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 / 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 / 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
}
}