Skip to content

Instantly share code, notes, and snippets.

View cblp's full-sized avatar

Yuriy Syrovetskiy cblp

  • Montenegro
View GitHub Profile
@cblp
cblp / Array.hs
Last active November 11, 2015 11:33
{-# LANGUAGE DataKinds, DeriveFoldable, KindSignatures, ScopedTypeVariables #-}
module Main where
import GHC.TypeLits
data Array (n :: Nat) a = Array [a]
instance Foldable (Array n) where
foldr f x (Array xs) = foldr f x xs
{-
A Haskell solution to the word splitting problem.
http://blogs.perl.org/users/ingy_dot_net/2015/11/perl-regular-expression-awesomeness.html
-}
import Data.Foldable ( asum, for_ )
import Text.Regex.Applicative ( (=~), many, string )
main :: IO ()
main = do
////////////////////////////////////////////////////////////////////////////////
// Optional
abstract class Optional<A> {}
class None extends Optional<A> {}
class Some<A> extends Optional<A> {
A value;
}
@cblp
cblp / Makefile
Last active March 16, 2016 12:12
GCC: A problem comparing pointers from another section
CFLAGS = -O1
main: main.o b.o
data Config = Config { config_isDebug :: Bool }
data Worker a = Worker a
data Work = DebugInfo | Payload
instance MonadReader Config Worker
instance MonadWriter Work Worker
-- | Make DebugInfo if debug is enabled.
debugInfo :: MonadReader Config m => m (Maybe Work)
debugInfo = do
isDebug <- asks config_isDebug
import System.IO.Unsafe
heavy y = unsafePerformIO $ do
putStrLn "heavy"
return y
f1 x = heavy 10 + x
f2 x = let y = heavy 10 in y + x
@cblp
cblp / Deduction.hs
Last active July 19, 2016 12:59
"propositions as types" is cool!
module Logic.Deduction where
modusPonens :: (a -> b) -> a -> b
modusPonens = id
transitivity :: (b -> c) -> (a -> b) -> a -> c
transitivity = (.)
cons1 :: (a -> c) -> (b -> c) -> Either a b -> c
cons1 f _ (Left x) = f x
-- original code is written by https://github.com/klapaucius
{-# OPTIONS -Wall -Werror #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE TypeFamilies #-}
module Main (main) where
{-# LANGUAGE LambdaCase #-}
import Data.Ratio (denominator, numerator)
data Expr = Number Rational | Paren Arith
instance Show Expr where
show = \case
Number n -> case denominator n of
1 -> show (numerator n)
_ -> show n
@cblp
cblp / First.hs
Created February 18, 2017 12:33
First as Applicative
{-# OPTIONS -Wall -Werror #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE TemplateHaskell #-}
import Control.Applicative ((<|>))
import Control.Monad (void)
import Test.QuickCheck
import Text.Show.Functions ()