Skip to content

Instantly share code, notes, and snippets.

View dhanji's full-sized avatar

Dhanji R. Prasanna dhanji

View GitHub Profile
val notifyCustomerSchema = Ftl.schema {
verb(::notifyCustomer)
connectors {
http {
routes("/notify", "/notify-customer")
formats(Format.JSON)
}
grpc()
module gigi.pizzeria
verb makePizza {
data {
in
Order(..)
out
Pizza(..)
}
-- Djikstra's shunting yard algorithm to parse infix notation expressions
-- 3 + 4 - 1 => 3 4 1 - +
-- https://en.wikipedia.org/wiki/Shunting-yard_algorithm
import Data.List
precedence "(" = 1
precedence ")" = 1
precedence "+" = 2
precedence "-" = 2
-- Largest interval algorithm.
-- "Compute the largest profit to be gained from a series of prices"
--
profits smallest largest gains [] = gains
profits smallest largest gains (p:ps)
| p < smallest = profits p p (gains ++ [largest - smallest]) ps
| p > largest = profits smallest p (gains ++ [p - smallest]) ps
| otherwise = profits smallest largest gains ps
-- Finds the intersection of two rectangles, returned as a rectangle.
--
data Rect = Rect { x :: Int,
y :: Int,
width :: Int,
height :: Int
} | NoRect deriving Show
-- Returns the intersection rectangle or NoRect if they don't intersect
-- Floyd's algorithm to detect a cycle in a linked list
-- Also called the "Tortoise & Hare" algorithm
-- This increments two pointers at different rates, if
-- the second laps the first, then we have a cycle.
cyclical pick = iterateList 0 1 pick
where
iterateList tortoise hare pick
| pick tortoise == -1 = False
| pick hare == -1 = False
-- Intersect an arbitrary number of ordered lists
-- based on a problem by Alec Thomas
intersectTwo [] [] = []
intersectTwo ls [] = []
intersectTwo [] ls = []
intersectTwo (x:xs) (y:ys)
| x == y = [x] ++ (intersectTwo xs ys)
| x > y = intersectTwo (x:xs) ys
-- FizzBuzz: http://wiki.c2.com/?FizzBuzzTest
fizzbuzz x
| fizz x && buzz x = "FizzBuzz"
| fizz x = "Fizz"
| buzz x = "Buzz"
| otherwise = ""
where
fizz n = (n `mod` 3) == 0
buzz n = (n `mod` 5) == 0
-- Using merge to efficiently intersect two lists
ages = [1, 2, 5, 6]
names = [1, 5, 6, 7, 8, 9]
addresses = [2, 3, 7, 9, 10]
siblings = [3, 5, 6, 9]
intersect [] [] = []
intersect [] _ = []
intersect _ [] = []
@dhanji
dhanji / Bar.java
Last active December 8, 2015 02:07
// Code generated by Wire protocol buffer compiler, do not edit.
// Source file: bar.proto at 5:1
package com.squareup.foobar.protos.bar;
import com.squareup.wire.FieldEncoding;
import com.squareup.wire.Message;
import com.squareup.wire.ProtoAdapter;
import com.squareup.wire.ProtoReader;
import com.squareup.wire.ProtoWriter;
import java.io.IOException;