Skip to content

Instantly share code, notes, and snippets.

@qoelet
qoelet / remove-last-vowels.clj
Created March 9, 2020 13:22
PF.tv challenge: remove-last-vowels
(require '[clojure.string :as string])
(defn is-vowel? [character]
(contains? #{\a \e \i \o \u} character))
(defn remove-last-vowel [word]
(defn go [xs flag]
(cond
(or (empty? xs) flag)
xs
@qoelet
qoelet / Arrows.hs
Created May 14, 2019 09:26
Playing with arrows
module Arrows where
import Control.Arrow
import System.Directory (doesDirectoryExist)
-- `arr` lifts a function to an arrow
-- arr :: (b -> c) -> a b c
-- e.g. pure function (+ 2) :: Integer -> Integer
-- b :: Integer
<?php
class WithFileContents {
private $resource = null;
private function open($f) {
$this->resource = fopen($f, "r");
}
public function run($resource, $fun) {
@qoelet
qoelet / sum_factors.R
Created April 29, 2014 04:12
Summing up numbers coded as factors
total = sum(
sapply(
sapply(my_df$ratios, as.character),
as.numeric)
)
library(RMySQL)
library(GetoptLong)
# had to work with a secure connection to a MySQL
# make a option file, e.g. opts.cnf
"
[client]
user=kenny
host=mysqldb
ssl-ca=ca.crt
# hit a snag time when working with session ids (represented as bigint in a database)
df <- read.csv("myData.csv", header=TRUE) # sid is the column
"
> tail(df, n=1)$sid
[1] 2e+18
"
# WTF moment
CREATE OR REPLACE FUNCTION fa.double_book_keeping_check() RETURNS trigger
AS $$
BEGIN
IF TG_TABLE_NAME='reports' THEN
IF NEW.total_assets = (NEW.total_equity + NEW.total_liabilities) THEN
RETURN NEW;
ELSE
RAISE EXCEPTION 'BOOK KEEPING ERROR: TOTAL ASSETS DOES NOT MATCH SUM(TOTAL LIABILITIES, TOTAL EQUITY)!';
END IF;
END IF;
# calculating annual maxima from a time series (df)
df$year <- floor_date(df$date, "year")
df_maxima <- ddply(df, "year", summarise, orders=max(orders))
head(arrange(df_maxima, desc(orders), 3) # top 3
lhs = reshape([2, -1, 0, -1, 2, -3, 0, -1, 4], 3, 3)
rhs = reshape([0, -1, 4], 3, 1)
soln = lhs\rhs
print((lhs * soln) == rhs) # prove it so
# So R can solve linear algebra too!
lhs <- matrix(c(2, -1, 0, -1, 2, -3, 0, -1, 4), nrow=3)
"
[,1] [,2] [,3]
[1,] 2 -1 0
[2,] -1 2 -1
[3,] 0 -3 4
"