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
@iurii-kyrylenko
iurii-kyrylenko / logic-02.js
Last active December 11, 2024 23:38
Counting of Arms
/*
* The inhabitants of Mars have 1, 2 or 3 arms.
* Once 5 Martians gathered and counted their arms: they had 10 arms in total.
* How many one-armed, two-armed and three-armed Martians gathered?
* Find all the options.
*/
// (15, 5) -> [ 0, 0, 1, 2, 0 ]
const intToTernary = (i, size) => [...i.toString(3).padStart(size, "0")].map(Number);
@iurii-kyrylenko
iurii-kyrylenko / logic-01.js
Last active December 2, 2024 09:58
Liars of the Round Table
/*
* There are 6 dwarfs sitting at a round table.
* Each of them said: "Both of my neighbors are liars."
* What is the smallest number of liars that can sit at this table?
* What is the largest number of liars that can sit at this table?
*/
// (12, 6) -> [0, 0, 1, 1, 0, 0]
const intToBits = (i, size) => [...i.toString(2).padStart(size, "0")].map(Number);
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)