Skip to content

Instantly share code, notes, and snippets.

{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE NoStarIsType #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
@duairc
duairc / net.nix
Last active October 31, 2024 06:06
IP address arithmetic and validation in Nix
{ lib ? null, ... }:
let
net = {
ip = {
# add :: (ip | mac | integer) -> ip -> ip
#
# Examples:
{-# LANGUAGE RankNTypes #-}
import Control.Lens hiding (lens)
import qualified Control.Lens as L (lens)
import Data.Bifunctor
import Data.Functor.Alt
------------------------------------------------------------------------------
split
class Method
def memoize!(time = nil)
name = self.name
owner = self.owner
method = self.to_proc
self.unbind
owner.send(:define_method, name) do |*args, &block|
@results ||= {}
if !@time || (time && @time + time < Time.now)
result = method.call(*args, &block)
@duairc
duairc / table-rows.scss
Last active August 29, 2015 14:06
SCSS selectors for the first and last row of a table
// http://www.w3.org/TR/html5/tabular-data.html#the-table-element
@mixin first-row() {
& >,
> thead >,
> tbody:first-child >,
> caption:first-child + tbody >,
> caption:first-child ~ colgroup + tbody >,
> colgroup:first-child + tbody >,
> colgroup:first-child ~ colgroup + tbody >,
@duairc
duairc / fonts.css
Created February 15, 2014 02:26
Given the name of a font family, generate webfonts + a CSS file on a Linux system
#!/bin/sh
clean () {
perl -pe 's/(\([si]\)\s*)+//' | tr '"' ' ' | awk '{$1=""; print $0}' | xargs echo
}
subset () {
python /usr/share/googlefontdirectory-tools/tools/subset/subset.py --subset=latin+latin-ext+greek+greek-ext+cyrillic+cyrillic-ext --nmr --null --roundtrip --script "$1" "$2"
}
import Control.Applicative
import Data.Attoparsec
import Data.IORef
import System.IO.Unsafe
------------------------------------------------------------------------------
wouldSucceed :: Parser a -> Parser Bool
wouldSucceed p = do
result <- return $! unsafePerformIO (newIORef False)
(do
@duairc
duairc / IDNA.hs
Created January 10, 2014 03:59
Fast IDNA implementation in Haskell using attoparsec.
{-# LANGUAGE BangPatterns #-}
module Text.IDNA
( toASCII
, toUnicode
)
where
-- attoparsec ----------------------------------------------------------------
import Data.Attoparsec.Text
loadTemplates path = readDirectoryWith reader path
>>= (free
-- DirTree (Maybe (FilePath, Either String ByteString))
>>> F.toList
-- [Maybe (String, Either String ByteString)]
>>> catMaybes
-- [(String, Either String ByteString)]
>>> unzip
-- ([String], [Either String ByteString])
>>> second partitionEithers
{-# LANGUAGE OverloadedStrings #-}
{-|
'Snap.Extension.Clever.Impl' is an implementation of the 'MonadClever'
interface defined in 'Snap.Extension.Clever'.
As always, to use, add 'CleverState' to your application's state, along with
an instance of 'HasCleverState' for your application's state, making sure to
use 'cleverInitializer' in your application's 'Initializer', and then you're