Skip to content

Instantly share code, notes, and snippets.

@cleichner
Last active August 29, 2015 14:02
Show Gist options
  • Save cleichner/ffed0f7a0e39664a899c to your computer and use it in GitHub Desktop.
Save cleichner/ffed0f7a0e39664a899c to your computer and use it in GitHub Desktop.
module KleisliComp where
import Control.Arrow
import Data.Foldable
import Data.Monoid
import Data.Monoid.Endomorphism
-- After explicitly writing the KleisliEndo instance, I realized that it could
-- be built using existing infrastructure, namely the Kleisli category instance
-- and the Monoid instance for category endomorphism composition.
-- The Kleisli category instance composes in the opposite of the order that I
-- would like, so I use the Dual instance to straighten things out.
kleisliComp :: (Foldable f, Monad m) => f (a -> m a) -> a -> m a
kleisliComp = (runKleisli . getEndomorphism . getDual) .
foldMap (Dual . Endomorphism . Kleisli)
import Data.Maybe
import System.IO
import KleisliComp
-- import KleisliCompExplicit
hasChar :: Eq a => a -> [a] -> Maybe [a]
hasChar c word = case dropWhile (/= c) word of
[] -> Nothing
(_:word') -> Just word'
match :: Eq a => [[a]] -> [a] -> [[a]]
match words' search = let
-- hasAllChars search' = isJust . ((foldr (>=>) return) . map (hasChar)) search'
-- hasAllChars = (isJust .) . foldr (>=>) return . map hasChar
hasAllChars = (isJust .) . kleisliComp . map hasChar
select search' = map (hasAllChars search')
display = map snd . filter fst
in
display $ zip (select search words') words'
loop :: [String] -> String -> IO ()
loop dictionary search = do
hSetBuffering stdin NoBuffering
c <- getChar
putStrLn ""
case c of
'\EOT' -> return ()
'\DEL' -> continue dictionary (if null search then [] else init search)
c' -> continue dictionary (search ++ [c'])
where
continue dict search' = do
putStr $ search' ++ " "
print $ match dictionary search'
loop dict search'
main :: IO ()
main = do
let dictionary = ["hello", "world", "mellow", "haeiloluo", "nom", "whirld"]
loop dictionary []
module KleisliCompExplicit where
import Control.Monad
import Data.Monoid
import Data.Foldable
-- I wrote this version first, explicitly joining the Kleisli-part and
-- Endo-part together.
newtype KleisliEndo m a = KleisliEndo { appKleisliEndo :: a -> m a }
instance (Monad m) => Monoid (KleisliEndo m a) where
mempty = KleisliEndo return
KleisliEndo f `mappend` KleisliEndo g = KleisliEndo (f >=> g)
-- kleisliComp :: [a -> Maybe a] -> a -> Maybe a
kleisliComp :: (Foldable f, Monad m) => f (a -> m a) -> a -> m a
kleisliComp = appKleisliEndo . foldMap KleisliEndo
The MIT License (MIT)
Copyright (c) 2014 Chas Leichner
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit aapersons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment