Skip to content

Instantly share code, notes, and snippets.

View SamirTalwar's full-sized avatar
🦖
Hunting for bugs.

Samir Talwar SamirTalwar

🦖
Hunting for bugs.
View GitHub Profile
@SamirTalwar
SamirTalwar / ClassHierarchy.scala
Created February 3, 2016 14:46
Print the hierarchy of a given class.
import scala.collection.SortedSet
sealed trait Tree[T] {
val value: T
}
case class Leaf[T](value: T) extends Tree[T]
case class Node[T](value: T, children: SortedSet[Tree[T]]) extends Tree[T]
object ClassHierarchy {
def main(args: Array[String]) =
@SamirTalwar
SamirTalwar / ExpressionProblem.scala
Created February 14, 2016 13:52
A working implementation of Wadler's solution to the Expression Problem (http://homepages.inf.ed.ac.uk/wadler/papers/expression/expression.txt).
import scala.language.higherKinds
trait Exp[Visitor[_]] {
def visit[R](visitor: Visitor[R]): R
}
trait VisitorF[R] {
def forNum(n: Int): R
}
FROM ruby
RUN mkdir /app
WORKDIR /app
COPY Gemfile Gemfile
RUN bundle install
COPY google.rb google.rb
RUN chmod +x google.rb
@SamirTalwar
SamirTalwar / rot.hs
Created June 8, 2016 22:31
String rotation (rot13, etc.) in Haskell.
-- Usage:
-- rot 13 "Uryyb, jbeyq!"
-- --> "Hello, world!"
import Control.Applicative ((<|>))
import Data.Maybe (fromMaybe)
import System.Environment (getArgs)
caesarCipher :: Int -> [a] -> [(a, a)]
caesarCipher n xs = zip xs (drop n (cycle xs))
@SamirTalwar
SamirTalwar / jsonp.js
Created October 21, 2016 11:43
A quickly-implemented, barely-tested JSONP client implementation, without jQuery.
function getJSONP(address, callback) {
const random = Math.floor(Math.random() * 65536)
const callbackName = 'getJSONP_callback_' + random
window[callbackName] = function() {
document.body.removeChild(script)
delete window[callbackName]
callback.apply(null, arguments)
}
const script = document.createElement('script')
@SamirTalwar
SamirTalwar / all.js
Last active October 25, 2016 09:35
Implementations of functions that work on Promise arrays, including `all` and `race`.
const all = promises => {
if (promises.length === 0)
return Promise.resolve([])
let [head, ...tail] = promises
return head.then(h =>
all(tail).then(t =>
[h].concat(t)))
}
@SamirTalwar
SamirTalwar / update-yarn-lock.sh
Created March 2, 2017 17:18
Update yarn.lock files in Greenkeeper branches.
set -ex
for branch in $(git branch -a | fgrep greenkeeper | gsed -r 's#^ *remotes/origin/##'); do
git checkout $branch
yarn install
git add yarn.lock
git commit -m 'Update yarn.lock.'
git push
git checkout master
git branch -d $branch
@SamirTalwar
SamirTalwar / sudoku.pl
Created December 22, 2017 13:04
Sudoku solver in Prolog.
% vim: set syntax=prolog
:- use_module(library(clpfd)).
:- initialization(main, main).
main(_) :- main.
main :-
current_input(S),
@SamirTalwar
SamirTalwar / Tree.hs
Created May 10, 2019 16:46
A tree implementation in Haskell.
module Tree where
data Tree a
= Node [Tree a]
| Leaf a
deriving (Eq, Show)
instance Functor Tree where
f `fmap` Leaf value = Leaf $ f value
f `fmap` Node children = Node $ fmap (fmap f) children
@SamirTalwar
SamirTalwar / random-hacks.ts
Last active August 19, 2019 23:42
Random hacks you can do with TypeScript.
type Status = "passed" | "failed" | "error" | "unknown";
interface Things {
a: string;
b: number;
c: {
d: {
e: string;
};
f: Array<{ g: number }>;