Skip to content

Instantly share code, notes, and snippets.

@xgrommx
xgrommx / LensExample.purs
Created December 4, 2018 02:54
LensExample
module LensExample where
import Prelude
import Data.Array (filter)
import Data.Generic.Rep (class Generic)
import Data.Generic.Rep.Show (genericShow)
import Data.Lens (Traversal, traversed, wander, (.~), (^..), (+~))
import Data.Traversable (traverse)
import Effect (Effect)
@xgrommx
xgrommx / FAlgebra.purs
Created October 8, 2018 21:37
FAlgebras
data Append t = Append t t
derive instance functorAppend :: Functor Append
type Semigroup' r = (append :: V.FProxy Append | r)
newtype Semigroup t = Semigroup (V.VariantF (Semigroup' ()) t)
derive instance functorSemigroup :: Functor Semigroup
_append = SProxy :: SProxy "append"
append :: forall a. Algebra Semigroup a => a -> a -> a
newtype Circle = Circle { circleRadius :: Number, circleCenter :: Tuple Number Number }
derive instance genericCircle :: Generic Circle _
instance showCircle :: Show Circle where
show c = genericShow c
instance eqCircle :: Eq Circle where
eq x y = genericEq x y
type MuV t = Mu (VariantF t)
inv :: forall a b f s. RowCons s (FProxy f) a b
=> IsSymbol s
=> Functor f
=> SProxy s
-> f (Mu (VariantF b))
-> Mu (VariantF b)
inv x = In <<< (inj x)
-- | A “flushing” 'stream', with an additional coalgebra for flushing the
-- remaining values after the input has been consumed. This also allows us to
-- generalize the output away from lists.
fstream
:: (Cursive t (XNor a), Cursive u f, Corecursive u f, Traversable f)
=> Coalgebra f b -> (b -> a -> b) -> Coalgebra f b -> b -> t -> u
fstream ψ g ψ' = go
where
go c x =
let fb = ψ c
@xgrommx
xgrommx / tree.purs
Created February 28, 2018 22:11
Recursion schemes (derive Functor/Traversable/Foldable via catamorphism and Bifunctor/Bitraversable/Bifoldable)
module Tree where
import Control.Monad.Writer
import Data.Bifoldable
import Data.Bifunctor
import Data.Bitraversable
import Data.Foldable
import Data.Generic
import Data.Monoid
import Data.Monoid.Additive
@xgrommx
xgrommx / streams.hs
Created January 19, 2018 05:18 — forked from michaelt/streams.hs
little streaming library
{-#LANGUAGE BangPatterns #-}
import GHC.Magic
import Data.IORef
import Control.Monad
import Control.Monad.Trans
data Stream a m r = Yield a (Stream a m r) | Done r | Delay (() -> m (Stream a m r))
instance Functor m => Functor (Stream a m) where
fmap f (Done r) = Done (f r)
@xgrommx
xgrommx / HRecursionSchemes.hs
Last active December 9, 2021 07:30
HRecursionSchemes
{-# LANGUAGE StandaloneDeriving, DataKinds, PolyKinds, GADTs, RankNTypes, TypeOperators, FlexibleContexts, TypeFamilies, KindSignatures #-}
-- http://www.timphilipwilliams.com/posts/2013-01-16-fixing-gadts.html
module HRecursionSchemes where
import Control.Applicative
import Data.Functor.Identity
import Data.Functor.Const
import Text.PrettyPrint.Leijen hiding ((<>))
@xgrommx
xgrommx / http.hs
Created October 30, 2017 22:10 — forked from markandrus/http.hs
Sketch for a testable, free monad-based HTTP client
{-#LANGUAGE DataKinds #-}
{-#LANGUAGE DeriveDataTypeable #-}
{-#LANGUAGE DeriveFoldable #-}
{-#LANGUAGE DeriveFunctor #-}
{-#LANGUAGE DeriveGeneric #-}
{-#LANGUAGE DeriveTraversable #-}
{-#LANGUAGE GADTs #-}
{-#LANGUAGE GeneralizedNewtypeDeriving #-}
{-#LANGUAGE KindSignatures #-}
{-#LANGUAGE TypeFamilies #-}
@xgrommx
xgrommx / Fix.hs
Last active March 9, 2018 19:04
Fix
{-# LANGUAGE RankNTypes, ScopedTypeVariables, DeriveTraversable, PatternSynonyms, UndecidableInstances, FlexibleInstances, ViewPatterns, InstanceSigs #-}
module Fix where
import Control.Monad (ap, join, (<=<))
import Control.Applicative (empty, Alternative, (<|>))
import Control.Arrow
import Data.Functor.Compose
-- Free f a = Mu x. a + f x