Skip to content

Instantly share code, notes, and snippets.

View iurii-kyrylenko's full-sized avatar

Iurii Kyrylenko iurii-kyrylenko

  • Kharkiv, Ukraine
View GitHub Profile
data Tree a = Leaf a | Branch (Tree a) (Tree a) deriving Show
bt = Branch (Leaf 42) (Branch (Leaf 1) (Leaf 2))
fmap' :: (a -> b) -> Tree a -> Tree b
fmap' f (Leaf x) = Leaf (f x)
fmap' f (Branch l r) = Branch (fmap' f l) (fmap' f r)
foldMap' :: Monoid m => (a -> m) -> Tree a -> m
foldMap' f (Leaf x) = f x
@iurii-kyrylenko
iurii-kyrylenko / r-tree.hs
Last active March 3, 2019 13:47
RoseTree as Foldable
data RoseTree a = Rose a [RoseTree a]
deriving Show
-- 5
-- / \
-- 3 7
-- / \
-- 1 4
rt = Rose 5 [Rose 3 [Rose 1 [], Rose 4[]], Rose 7 []]
@iurii-kyrylenko
iurii-kyrylenko / array-to-map.js
Created February 28, 2019 12:34
Array to map
function flatten(arr) {
return [].concat(...arr);
}
function combine(keys, values) {
return keys.reduce((acc, key, i) =>
({ ...acc, [key]: values[i] || null }), {}
);
}
@iurii-kyrylenko
iurii-kyrylenko / trees-dfs-bfs.hs
Created February 16, 2019 16:58
Trees DFS/BFS
-- https://web.cecs.pdx.edu/~mpj/pubs/springschool95.pdf
-- https://patternsinfp.wordpress.com/2015/03/05/breadth-first-traversal/
-- https://researchspace.auckland.ac.nz/handle/2292/3470
class Tree t where
subtrees :: t -> [t]
data BinTree a = Leaf a
| BinTree a :^: BinTree a
@iurii-kyrylenko
iurii-kyrylenko / trans1.hs
Created February 10, 2019 12:57
Monad Transformers 1
import Control.Monad
import Control.Monad.Trans
import Control.Monad.Trans.Maybe
isValid :: String -> Bool
isValid [] = False
isValid (x:_) = x == 'a'
getPsw :: MaybeT IO String
getPsw = do s <- lift $ getLine
@iurii-kyrylenko
iurii-kyrylenko / kleisli-fmap.hs
Created February 9, 2019 15:59
Kleisli Arrow -> fmap
-- Prelude Control.Monad> :t (>=>)
-- (>=>) :: Monad m => (a -> m b) -> (b -> m c) -> a -> m c
-- Prelude Control.Monad> :t (id >=>)
-- (id >=>) :: Monad m => (b -> m c) -> m b -> m c
-- Prelude Control.Monad> :t \f -> (>=> \x -> return (f x))
-- \f -> (>=> \x -> return (f x))
-- :: Monad m => (t -> c) -> (a -> m t) -> a -> m c
@iurii-kyrylenko
iurii-kyrylenko / profunctor.hs
Created February 9, 2019 15:57
Haskell/Profunctor
class Profunctor p where
dimap :: (a -> b) -> (c -> d) -> p b c -> p a d
dimap f g = lmap f . rmap g
lmap :: (a -> b) -> p b c -> p a c
lmap f = dimap f id
rmap :: (b -> c) -> p a b -> p a c
rmap = dimap id
instance Profunctor (->) where
-- dimap :: (a -> b) -> (c -> d) -> (b -> c) -> (a -> d)
@iurii-kyrylenko
iurii-kyrylenko / cont-01.hs
Last active January 20, 2019 14:38
Haskell/Continuation passing style
-- https://en.wikibooks.org/wiki/Haskell/Continuation_passing_style
add :: Int -> Int -> Int
add x y = x + y
square :: Int -> Int
square x = x * x
pithagoras :: Int -> Int -> Int
pithagoras x y = add (square x) (square y)
@iurii-kyrylenko
iurii-kyrylenko / rand.hs
Last active January 12, 2019 11:43
Haskell State Exercises
-- exercices for
-- https://en.wikibooks.org/wiki/Haskell/Understanding_monads/State
import Control.Monad
import Control.Monad.Trans.State
import System.Random
rollDie :: State StdGen Int
rollDie = state $ randomR (1,6)
@iurii-kyrylenko
iurii-kyrylenko / app.hs
Last active January 7, 2019 18:03
Functor-Applicative-Monad
--- functor from applicative ---
-- fmap2 (2*) [1,2,3,4]
fmap2 :: Applicative f => (a -> b) -> f a -> f b
-- fmap2 f x = pure f <*> x
-- fmap2 f = (<*>) (pure f)
fmap2 = (<*>) . pure
--- functor from monad (liftM)
-- fmap3 (2*) [1,2,3,4]
fmap3 :: Monad m => (a -> b) -> m a -> m b