Skip to content

Instantly share code, notes, and snippets.

View joshcc3's full-sized avatar

Joshua Coutinho joshcc3

  • Imperial College London
View GitHub Profile
@joshcc3
joshcc3 / type_map.cpp
Created February 25, 2024 11:33
example of a type_map in cpp, to enable retrieval of values based on enum classes for e.g.
#include <atomic>
#include <functional>
#include <iostream>
#include <unistd.h>
#include <type_traits>
#include <sys/eventfd.h>
#include <cstdarg>
using namespace std;
@joshcc3
joshcc3 / typelisteg.cpp
Created February 16, 2024 09:35
Example of a type list and mapping sizeof(obj) to standard int types
template<int T>
constexpr int powVal = powVal<T/2> + 1;
template<>
constexpr int powVal<0> = 0;
template<>
constexpr int powVal<1> = 0;
template<typename... Ts>
struct TList;
{-# LANGUAGE MultiParamTypeClasses #-}
data State s a = State (s -> (a, s))
instance Functor (State s) where
fmap f (State g) = State $ \s -> case g s of
(a, s') -> (f a, s')
instance Monad (State s) where
atoi :: String -> Integer
atoi = uncurry (*) . ((bool 1 (-1) . (=="-")) *** foldl (\v x -> v*10 + ord x - 48) 0) . break isDigit
{-# LANGUAGE TypeFamilies, MultiParamTypeClasses, NoMonomorphismRestriction, FlexibleInstances, FlexibleContexts #-}
class First a where
ret :: a
instance First (a -> a) where
ret = id
instance First (r -> y) => First (r -> x -> y) where
ret r _ = ret r
module Tmp where
import Data.Monoid
import Control.Monad
import Data.Char
import Control.Applicative
import Control.Lens
initMap :: [(Char, (Int, Int))]
initMap = ('0', (1, 3)) : zip (map (chr . (ord '0' +)) [1..9]) (flip (,) <$> [0..2] <*> [0..2])
replaceWithMax :: [Int] -> [Int]
replaceWithMax l = ans
where
(ans, maximum) = f (l, head l)
f (l:ls, m) = Control.Arrow.first (maximum:) $ f (ls, max m l)
f x = x
relabel :: Tree Int -> Tree Int
relabel t = ans
type Zip' m a = m (Seq a, a, Seq a, Producer a m ())
type Zip a = Zip' Identity a
forward :: Monad m => Zip' m a -> Zip' m a
forward m = do
(s, a, s', p) <- m
case viewl s' of
EmptyL -> next p >>= \x -> case x of
Left _ -> error "Stream dried up"
//things to do- have to increase the size of the compressed array..
//too much compression doesn't allow for distinction between similar characters.
//to isolate characters on a line we must form
//lets get this straight-
//
import java.io.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
public class OCR{