Skip to content

Instantly share code, notes, and snippets.

View Heimdell's full-sized avatar
🔨
Right tool for the right job

Андреев Кирилл Heimdell

🔨
Right tool for the right job
  • Ульяновск
View GitHub Profile
-- Wrapper for recursive types
data Fix f = In { out :: f (Fix f) }
-- 1-layer fold and unfold
type Algebra f a = f a -> a
type Coalgebra f a = a -> f a
cata :: Functor f => Algebra f b -> Fix f -> b
ana :: Functor f => Coalgebra f a -> a -> Fix f
@Heimdell
Heimdell / parser.py
Last active September 18, 2015 12:28
def split(list):
"[a] -> (a, [a])"
return [list[0], list[1:]]
def reduce(list, zero, add):
"([a], b, (b, a) -> b) -> b"
for i in range(0, len(list)):
zero = add(zero, list[i])
{-# LANGUAGE
TemplateHaskell,
Rank2Types,
NoMonomorphismRestriction,
TypeOperators,
StandaloneDeriving,
DeriveFunctor #-}
import Control.Applicative
@Heimdell
Heimdell / ke.hs
Last active September 4, 2015 16:56
--module MyZipper
-- ( File()
-- , Position
-- , up
-- , downAtLeft
-- , downAtRight
-- , left
-- , right
-- , comeIn
@Heimdell
Heimdell / it.hs
Last active August 29, 2015 14:26
SEND + MORE = MONEY
import Data.List (permutations)
import Control.Monad (guard)
main = putStrLn $ head solutions
solutions = do
let range = [0.. 9]
[s,e,n,d,m,o,r,y,_,_] <- permutations range
import Data.List (sortBy)
import Data.Function (on)
merge :: [(Int, a)] -> [(Int, b)] -> [(Maybe a, Maybe b)]
merge az bz = case (az, bz) of
((i, a) : az', (j, b) : bz') ->
case i `compare` j of
LT -> (Just a, Nothing) : merge az' bz
EQ -> (Just a, Just b) : merge az' bz
import Data.List (foldl')
avg list =
let (count, sum) = foldl' collect (0, 0) list
in fromIntegral sum / count
where
collect (count, sum) item =
count `seq`
@Heimdell
Heimdell / list_lol.rb
Last active August 29, 2015 14:24
Не думаю, что программирование на Scheme имеет какой-либо эффект на программиста ;)
def list_induction list, stop, step, ret
return stop.(ret) if list.empty?
x = list.shift
return list_induction list, stop, step, -> (args) {
step.(x, args, ret)
}
end
list_induction [1,2,3,4,5],
@Heimdell
Heimdell / lc.prolog
Last active August 29, 2015 14:23
Prolog lambda-calculus-to-js translator #2
:- op(1030, xfx, is).
:- op(1045, yfx, @).
:- op(1040, yfx, after).
:- op(1050, xfy, to).
:- op(1175, xfy, in).
compile
--> simplify
, toJS
, !
import Color exposing (..)
import Dict exposing (..)
import Graphics.Element exposing (..)
import Graphics.Collage exposing (..)
import Keyboard exposing (..)
import Maybe exposing (..)
import Signal exposing (..)
type alias Game =
{ player : Point