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 / 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 / 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 / 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 / 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))
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 / 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
}
@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 / wtf.sh
Created October 30, 2015 15:55
So both `export` and `local` swallow failure in both Bash and zsh.
$ foo=$(false); echo $?
1
$ export foo=$(false); echo $?
0
$ function bar { local foo=$(false); echo $?; }; bar
0
@SamirTalwar
SamirTalwar / social.fs
Last active September 4, 2015 19:22
Solution to the Social Network kata in F#.
module Social
open FSharpx.State
open NodaTime
type User = {Name: string}
type Quack = {User: User; Text: string; Timestamp: Instant}
type Quacks = Quack list
type Timeline = Quack list
type Timelines = Map<User, Quacks * Timeline>
type Following = Map<User, User list>
@SamirTalwar
SamirTalwar / bankaccount.fs
Last active August 29, 2015 14:20
Bank Account Kata with Transactions in F#
module BankAccount
module Prod =
type Result<'T> =
| Success of 'T
| Failure of string
let (|>>) (result: Result<'T>) (f: 'T -> Result<'U>) =
match result with
| Success(value) -> f value
| Failure(error) -> Failure(error)