Skip to content

Instantly share code, notes, and snippets.

View axman6's full-sized avatar

Alex Mason axman6

View GitHub Profile
@axman6
axman6 / partsOf template.hs
Created January 22, 2023 05:26
Lens partsOf template
>>> ("Hello", ("there", ["are","several"]),Left "Strings", "in", "here")
& partsOf template %~ (reverse :: [String] -> [String])
("here",("in",["Strings","several"]),Left "are","there","Hello")
>>> ("Hello", ("there", ["are","several"]),Left "Strings", "in", "here")
& partsOf template %~ (reverse :: String -> String)
("erehn",("isgni",["rtS","lareves"]),Left "eraereh","to","lleH")
{-# LANGUAGE ExistentialQuantification #-}
module MyLib where
import Data.Foldable (toList)
import Control.Applicative
@axman6
axman6 / popcounts.cpp
Last active July 15, 2022 03:20
pop count experiments
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
// #include <benchmark/benchmark.h>
const size_t popcount(uint16_t x) {
return __builtin_popcount(x);
@axman6
axman6 / Primes.hs
Created November 1, 2021 23:34
An infinite list of primes, using mutual recursion
module Primes where
primes :: [Integer]
primes = 2 : 3 : filter isPrime [5,7..]
isPrime :: Integer -> Bool
isPrime n = all (\x -> n `mod` x /= 0) $ takeWhile (\x -> x*x <= n) primes
@axman6
axman6 / cont-regex.hs
Last active December 30, 2019 02:51
An example of how Foreign.Marshal.ContT can make FFI code nicer
compile :: ByteString -> [PCREOption] -> Either String Regex
compile str flags = unsafePerformIO $
useAsCString str $ \pattern -> do
alloca $ \errptr -> do
alloca $ \erroffset -> do
pcre_ptr <- c_pcre_compile pattern (combineOptions flags) errptr erroffset nullPtr
if pcre_ptr == nullPtr
then do
err <- peekCString =<< peek errptr
return (Left err)
{-- 1
1 1
1 2 1
1 3 3 1
1 4 6 4 1 --}
-- printRow :: (Integral a, Show a) => a -> [a] -> IO ()
printRow :: Int -> [Int] -> IO ()
printRow pad nums = do
@axman6
axman6 / SOPTest.hs
Created August 11, 2019 08:19
Playing with genetics-sop
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE DataKinds #-}
import Generics.SOP
import Data.List (intercalate)
import Control.Applicative
import Control.Monad
import Data.Map.Strict (Map, (!))
import qualified Data.Map.Strict as Map
import Data.Monoid
import Data.String
import Data.Text
import Language.Haskell.TH
import System.Exit
import System.IO
@axman6
axman6 / CSSScaling.hs
Created March 8, 2018 06:42
Fast CSS hex scaling
-- Scales colours found in a given file by a given factor, manages ~150MB/s
{-# LANGUAGE OverloadedStrings #-}
import Data.Attoparsec.ByteString (Parser, satisfy)
import Data.Attoparsec.ByteString.Lazy as A
import Data.ByteString.Lazy as BSL hiding (concatMap, map)
import Data.Monoid ((<>))
import Data.Word (Word8)
import Prelude hiding (readFile, writeFile)
import System.Environment (getArgs)
@axman6
axman6 / database.hs
Last active April 22, 2017 00:32
Haskell multiline string problem
allPowerStationsQuery :: Query () (Vector PowerStation2)
allPowerStationsQuery =
statement
"SELECT participant, station_name, region, dispatch_type, \
\ category, classification, fuel_source_primary,\
\ fuel_source_descriptor, tech_type_primary,\
\ physical_unit_no, unit_size_m_w, aggregation,\
\ duid, reg_cap_m_w, max_cap_m_w, max_r_o_c_per_min\
\FROM duid_location;"
enc dec True