Skip to content

Instantly share code, notes, and snippets.

@pyrtsa
pyrtsa / example.cpp
Last active August 29, 2015 13:56
C++1y: Using user-defined literals for making `std::chrono::duration`s
#include <chrono>
constexpr auto operator "" _days (unsigned long long n) { return std::chrono::hours(24 * n); }
constexpr auto operator "" _h (unsigned long long n) { return std::chrono::hours(n); }
constexpr auto operator "" _min (unsigned long long n) { return std::chrono::minutes(n); }
constexpr auto operator "" _s (unsigned long long n) { return std::chrono::seconds(n); }
constexpr auto operator "" _ms (unsigned long long n) { return std::chrono::milliseconds(n); }
constexpr auto operator "" _µs (unsigned long long n) { return std::chrono::microseconds(n); }
constexpr auto operator "" _ns (unsigned long long n) { return std::chrono::nanoseconds(n); }
@pyrtsa
pyrtsa / ret.clj
Last active August 29, 2015 14:04
A short-circuiting let expression in Clojure
;; This is probably either a silly idea or a foolish implementation of an okay one.
(defn ret*
[bindings & body]
(if-let [[k v & more] (not-empty bindings)]
`(let [k# ~v]
(if (reduced? k#)
k#
(let [~k k#]
~(apply ret* more body))))
{-# LANGUAGE Arrows #-}
import Control.Arrow
import Control.Category
import Prelude hiding (id, (.))
-- Only for testing
import Text.Printf
newtype SF a b = SF (Float -> a -> (SF a b, b))
// The following code is in public domain. Re-releases are free to choose
// whether to attribute the code to its original author or not.
// Author: Pyry Jahkola <pyry.jahkola@iki.fi>
#include <sstream>
#include <iostream>
#include <map>
#include <cassert>
#include <stack>
#include <vector>
@pyrtsa
pyrtsa / holiday.py
Last active October 9, 2015 14:48
Finnish holidays
def is_holiday(d):
"""Check whether the given dateutil.date is a Finnish national holiday"""
import dateutil.easter
from datetime import date, timedelta as td
assert isinstance(d, date)
md = '{}.{}'.format(d.day, d.month)
y = d.year
# Fixed-date holidays
if md in '1.1 6.1 1.5 6.12 24.12 25.12 26.12'.split(): return True
# Finnish midsummer (eve on Fri the 19..25th, day on Sat the 20..26th)
@pyrtsa
pyrtsa / get_at_it.js
Last active December 11, 2015 06:08
Get at it!
// Public domain.
// Attribution welcome, but not required. -- Pyry Jahkola.
// What's the point in all this? To get the equivalent of CoffeeScript's
// (?.) operator into vanilla JavaScript.
// get(x, key1, key2, ...) -- Get the result of x[key1][key2]..., or undefined.
// Will short circuit if a key is not found.
module Main where
import Control.Monad
import Control.Monad.ST
import Data.Array.ST
import Data.Array.Unboxed
import Criterion.Main (defaultMain, bgroup, bench, whnf)
import Debug.Trace
@zudov
zudov / ConstraintFunctor.hs
Last active December 29, 2015 22:42
ConstraintKinds, Functor, Set
-- | https://jeltsch.wordpress.com/2013/02/14/the-constraint-kind/
{-# LANGUAGE ConstraintKinds, TypeFamilies #-}
import Prelude hiding (Functor(..))
import Data.Set (Set)
import qualified Data.Set as Set
import GHC.Exts (Constraint)
class Functor f where
type Object f a :: Constraint
@pyrtsa
pyrtsa / .git-ps1.bash
Last active November 4, 2016 08:00
git-ps1
function git-ps1 {
case $1 in
y|yes|on)
export GIT_PS1_SHOWDIRTYSTATE=yes
export GIT_PS1_SHOWUNTRACKEDFILES=yes
if [ -n "$SSH_CLIENT" ] || [ -n "$SSH_TTY" ]; then
export PS1='\[\e[0m\e[36m\]\u@\h \[\e[1;39m\]\w\[\e[0;32m\]$(__git_ps1 " (%s)")\[\e[0m\] \\$ ';
else
export PS1='\[\e[0m\e[31m\]\u \[\e[1;39m\]\w\[\e[0;32m\]$(__git_ps1 " (%s)")\[\e[0m\] \\$ ';
fi
@pyrtsa
pyrtsa / Managing-optionals-in-Swift.swift
Last active March 7, 2017 18:39
Managing optionals in Swift
// Hello, fellow Swift programmer!
//
// Here's a demonstration of a few ways of cleaning up the unwrapping of
// optionals in Swift.
//
// Swift is known to have both OO and functional background. Thus, there is
// probably some middle ground programming style that is *more* functional than
// Objective-C, and probably less so than Haskell. This playground tries to
// compare their differences in the pretty common scenario of dealing with
// errors or missing input.