Skip to content

Instantly share code, notes, and snippets.

@techtangents
techtangents / gist:4137760
Created November 23, 2012 23:57
Thesaurus
return
loggedUnit("Initializing thesaurus from file: " + zipFile)
.anonBindStrict(setLicense())
.anonBind(loadFromZipF(zipFile))
.appendLogIfNone("Unable to intialise Thesaurus.");
type Stepper v = StepperImpl v s
data StepperImpl v s = StepperImpl {
initial :: s,
next :: s -> (v -> s -> StepState v s) -> StepState v s -> StepState v s
}
data StepState v s = More v s | Done
step1 :: StepperImpl v s -> StepState v s
@techtangents
techtangents / fundipitive.hs
Created December 21, 2012 11:21
Fundiptive
infixl 4 -*-, -$-
class Fundipitive f where
(-*-) :: f (a -> b) -> f a -> f b
(-$-) :: (a -> b) -> f a -> f b
liftFd2 ::
Fundipitive f =>
(a -> b -> r)
-> f a
{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances #-}
import Data.Distributive
class (Functor a, Functor b) => Bidistributive a b where
dist :: a (b c) -> b (a c)
tsid :: b (a c) -> a (b c)
instance (Distributive x, Distributive y) => Bidistributive x y where
dist = distribute
@techtangents
techtangents / Nel.hs
Created January 24, 2013 12:21
Just learning how to instance Comonad on Non-Empty List
module Nel where
import Control.Comonad
infixr 5 :|
data Nel a = a :| [a]
toList :: Nel a -> [a]
toList (a :| as) = a : as
{-# LANGUAGE NoImplicitPrelude, TypeOperators #-}
import Prelude(undefined, Either(..), either, Functor(..))
data Store a b =
Store (a -> b) a
instance Functor (Store a) where
fmap f (Store p g) =
Store (f . p) g
@techtangents
techtangents / counter.elm
Created March 9, 2013 13:06
A signal that counts in whole numbers
const x _ = x
counter = counter' 0
counter' n =
foldp' (\_ s -> s + 1) (const n)
@techtangents
techtangents / Cycling.elm
Created March 9, 2013 13:44
A cycling Automaton in Elm. This Automaton produces values off a non-empty list. When the list is exhausted, it starts again. The input signal is just used as a source of events - its values are ignored.
-- non-empty list
data NonEmpty x = NonEmpty x [x]
-- neFromList :: [x] -> NonEmpty x
neFromList list = case list of [] -> Maybe.Nothing
(x:xs) -> Maybe.Just (NonEmpty x xs)
-- neFromListOr :: NonEmpty x -> [x] -> NonEmpty x
neFromListOr other qs = maybeOr other (neFromList xs)
Absolutely:
- any integer
- Incorrect - any integer except Integer.MIN_VALUE
Bytes:
- on each line, the numbers [0..128], [-127..-1]
- Incorrect - infinite loop
Constants:
- characters on lines: STUVWXYZ
{-
Recall that function composition is:
f . g = f (g x)
Consider, (f . g) as the application of g to an parameter destined for f.
Let's decribe g as the 'mapper' function and f as the 'primary function'
This gist generalises this idea to primary functions of different arities (in curried form).
Each parameter to the primary function has its own mapper function.
The API forms a similar style to that of Applicative, where infix functions chain values together in an expression.