https://twitter.com/inamiy/status/1489256068995710976
この記事では、補助的な関数として以下のものを用いる。
-- Special case of Control.Arrow.&&&
https://twitter.com/inamiy/status/1489256068995710976
この記事では、補助的な関数として以下のものを用いる。
-- Special case of Control.Arrow.&&&
#!/usr/bin/env cabal | |
{- cabal: | |
build-depends: base, vector | |
-} | |
module Main where | |
import Data.Foldable (foldl') | |
import Control.Monad (guard) | |
import qualified Data.Vector.Unboxed as V |
{-# LANGUAGE DeriveFunctor #-} | |
{-# LANGUAGE TypeFamilies #-} | |
{-# LANGUAGE ScopedTypeVariables #-} | |
{-# LANGUAGE FlexibleContexts #-} | |
module Logistic where | |
import Data.Kind ( Type ) | |
import Data.Functor.Const | |
import Data.Functor.Contravariant |
{-# LANGUAGE DeriveTraqversable #-} | |
import Witherable | |
newtype T a = MkT [a] | |
deriving (Show, Eq, Functor, Foldable, Traversable) | |
instance Filterable T where | |
-- ひとつでもNothingがあれば空リスト、 | |
-- すべてJustならJustを除いた元のリスト | |
catMaybes tpa = case sequenceA tpa of |
{-# LANGUAGE TypeOperators #-} | |
{-# LANGUAGE FlexibleInstances #-} | |
{-# LANGUAGE UndecidableInstances #-} | |
{-# LANGUAGE TypeFamilies #-} | |
{-# LANGUAGE FlexibleContexts #-} | |
{-# LANGUAGE DataKinds #-} | |
{-# LANGUAGE PolyKinds #-} | |
{-# LANGUAGE StandaloneKindSignatures #-} | |
{-# LANGUAGE GADTs #-} | |
module CoerceUnder where |
sum''''' :: Num a => [a] -> a | |
sum''''' xs = snd $ foldr h (\s -> ((), s)) xs 0 | |
where | |
h :: Num a => a -> (a -> b) -> a -> b | |
h x k = \s -> k (s + x) | |
{- | |
snd $ foldr h (\s -> ((), s)) xs 0 |
$ ghc-9.0.1 -fno-code -ddump-to-file -dsuppress-module-prefixes -dsuppress-type-applications -ddump-ds -ddump-splices Main.hs | |
[1 of 2] Compiling THPatTest ( THPatTest.hs, /tmp/ghc31722_0/ghc_4.o, /tmp/ghc31722_0/ghc_4.dyn_o ) | |
[2 of 2] Compiling Main ( Main.hs, /tmp/ghc31722_0/ghc_2.o, /tmp/ghc31722_0/ghc_2.dyn_o ) | |
$ runghc Main.hs | |
(2,2,2) | |
(1,2,1) |
module Lib | |
( largestPowersInt ) where | |
class Eq a => Iterable a where | |
zer :: a | |
inc :: a -> a | |
dec :: a -> a | |
instance Iterable Int where | |
zer = 0 |
module Summation where | |
import Data.List (sortBy) | |
import Data.Ord (comparing) | |
import qualified Data.Vector.Unboxed as V | |
type Query = (Int, Int, Int) | |
test1 :: [Query] | |
test1 = |