diff options
author | Joe Crayne <joe@jerkface.net> | 2019-05-24 21:11:14 -0400 |
---|---|---|
committer | Joe Crayne <joe@jerkface.net> | 2019-05-24 21:11:14 -0400 |
commit | 786e58ccbb05ced78c5421b53fbc469971d7db82 (patch) | |
tree | 0c60c0bbfd357c3d21d849dd61ecfb50d7ac1cc7 /SmallRing.hs | |
parent | a2aa65ffc5a4fc6cd0f41ccab1516f85a27989b2 (diff) |
Add curvature-coloring.
Diffstat (limited to 'SmallRing.hs')
-rw-r--r-- | SmallRing.hs | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/SmallRing.hs b/SmallRing.hs new file mode 100644 index 0000000..d792899 --- /dev/null +++ b/SmallRing.hs | |||
@@ -0,0 +1,39 @@ | |||
1 | {-# LANGUAGE RankNTypes #-} | ||
2 | module SmallRing where | ||
3 | |||
4 | import Data.Ratio | ||
5 | |||
6 | odds :: (Integral i, Num a) => i -> [a] | ||
7 | odds x = [ 1 + 2 * fromIntegral n | n <- [0..x] ] | ||
8 | |||
9 | pts :: (Fractional a, Integral b) => b -> [a] | ||
10 | pts i = let d = 2^i in [ k / d | k <- odds (2^(i-1) - 1) ] | ||
11 | |||
12 | fractions :: Fractional b => [b] | ||
13 | fractions = concatMap pts [1..] | ||
14 | |||
15 | vis :: Int -> Ratio Int -> [Char] | ||
16 | vis w α = let i = numerator α | ||
17 | n = denominator α | ||
18 | x = (w * i) `div` n | ||
19 | in replicate (x-1) '.' ++ "x" ++ replicate (w-max x 1) '.' ++ show α | ||
20 | |||
21 | data Giver a | ||
22 | = Give0 | ||
23 | | Give1 (forall b. (a -> b) -> b) | ||
24 | | Give2 (forall b. (a -> a -> b) -> b) | ||
25 | | Give3 (forall b. (a -> a -> a -> b) -> b) | ||
26 | |||
27 | pushFront :: a -> Giver a -> Giver a | ||
28 | pushFront a Give0 = Give1 ($ a) | ||
29 | pushFront a (Give1 f) = Give2 (\g -> f (g a)) | ||
30 | pushFront a (Give2 f) = Give3 (\g -> f (g a)) | ||
31 | pushFront a (Give3 f) = f $ \b c _ -> Give3 $ \g -> g a b c | ||
32 | |||
33 | take3 :: (a -> a -> a -> b) -> Giver a -> Maybe b | ||
34 | take3 f (Give3 g) = Just (g f) | ||
35 | take3 _ _ = Nothing | ||
36 | |||
37 | with3 :: Applicative f => Giver a -> (a -> a -> a -> f ()) -> f () | ||
38 | with3 (Give3 g) f = g f | ||
39 | with3 _ _ = pure () | ||