Skip to content

Instantly share code, notes, and snippets.

@patrickpang
patrickpang / scratchpad.hs
Created October 20, 2019 14:55
Haskell Cheatsheet
dup :: [a] -> [a]
dup [] = []
dup [x] = [x]
dup (x : y : xs) = x : y : y : dup xs
dup xs = concat [ replicate (if odd i then 1 else 2) n | (i, n) <- zip [1 ..] xs ]
last :: [a] -> a
last xs = head $ drop (length xs - 1) xs
@patrickpang
patrickpang / dt-clj.md
Created September 5, 2019 06:45
Data.table and Clojure

Data.table and Clojure

Background

This article compares the approaches of data manipulation using data.table in R and standard functions in Clojure, in order to evaluate the necessity of a Clojure library providing similar functionality of data.table. In the conceptual level, data.table provides a DSL in R specifically for data analysis, with a unified syntax similar to SQL for selecting, grouping, updating and joining tabular data. In contrast, the standard libraries of Clojure provide basic building blocks for general purpose, including persistent data structures (e.g. sequence, vector, set, map) and generic transformation functions (e.g. map, filter, reduce). This article aims to illustrate the impact of the two approaches on data manipulation using common use cases.

Load Dataset

The dataset used in this article is the NYC-flights14 data, which is On-Time flights data from the Bureau of Transporation Stati

@patrickpang
patrickpang / cards.py
Last active October 23, 2018 03:32
3 cards with numbers from 1 to 9 are distributed to 3 people in a few rounds. The final sums of numbers in cards received by them are 13, 15, 23 respectively. Find the numbers on the cards.
#!/usr/bin/env python3
from random import shuffle
def trial(x, y, z):
numbers = [x, y, z]
sums = [0, 0, 0]
while all([n < 23 for n in sums]):