Skip to content

Instantly share code, notes, and snippets.

for haskell repl:
:set prompt "\ESC[34mλ> \ESC[m"
for python repl:
sys.ps1 = '\x01\x1b[1;49;31m\x02λ>\x01\x1b[0m\x02'
sys.ps2 = '\x01\x1b[1;49;31m\x02λ>\x01\x1b[0m\x02'
@evgenii-malov
evgenii-malov / fif.sh
Created December 17, 2022 16:14
fzf search in files with open is visual studio code (ctrl-o)
fif() {
rg \
--column \
--no-heading \
--fixed-strings \
--ignore-case \
--hidden \
--follow \
--glob '!.git/*' \
--glob '!.vscode-server/*' \
@evgenii-malov
evgenii-malov / marrays.hs
Last active September 6, 2022 15:57
Work with mutable arrays in haskell examples
-- video https://youtu.be/uj2N5fBZnCA
import Data.Array.MArray
import Data.Array.IO.Internals (IOArray(IOArray))
import Control.Monad.ST
import Data.Array.ST
workio = do
a <- newArray (0,3) 'a' :: IO ( IOArray Int Char)
writeArray a 3 'x'
a_ <- getAssocs a
@evgenii-malov
evgenii-malov / iarray.hs
Last active September 6, 2022 15:57
work with Immutable array in Haskell
-- video https://youtu.be/RPEtIZ44RSc
import Data.Array.IArray
import Data.Char (toUpper)
-- elements that is associated with some kind of numerical index
-- truly constant time (O(1)) access.
-- store their data more efficiently in memory.
-- modification function must create an entirely new array (partially can be fixed with Monad context)
-- math look:
@evgenii-malov
evgenii-malov / bsort_st.hs
Last active September 5, 2022 09:00
Haskell bubble sort with ST monad and STref
-- video https://www.youtube.com/watch?v=9JDPGvc6UmI
import Control.Monad.ST
import Data.IntMap.Strict
import Data.STRef (newSTRef, readSTRef, writeSTRef, modifySTRef)
import Control.Monad
until_ :: Monad m => m a -> m Bool -> m ()
until_ a p = do
a
b <- p
@evgenii-malov
evgenii-malov / linked_list.hs
Created September 5, 2022 05:53
Haskell linked list with ST monad
-- see video https://www.youtube.com/watch?v=MJscn-m4KIo&t=3943s
import Data.STRef
import Control.Monad.ST
import Data.Maybe
import Control.Monad
import Data.Either (fromRight, isLeft, fromLeft)
import Debug.Trace
data Llist s a = Llist {llen:: STRef s Int, lhead:: STRef s (Maybe (Node s a)) }
@evgenii-malov
evgenii-malov / cartesiant_ik.hs
Last active April 22, 2022 13:17
Cartesian tree with implcit key in Haskell
-- explain video https://youtu.be/Ln_tVErialQ
-- GHCi, version 8.8.4
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TypeOperators #-}
{-# OPTIONS_GHC -Wno-incomplete-patterns #-}
import PrettyT -- https://www.youtube.com/watch?v=Ud-1Z0hBlB8&t=379s
-- data Btree a = Empty | Node a (Btree a) (Btree a) deriving Show
import Data.Ord
import Data.List
@evgenii-malov
evgenii-malov / cartesian_tree.hs
Last active May 23, 2024 02:31
Build cartesian tree in Haskell in 3 ways (O(N^2), O(N*LogN) and O(N))
-- video https://youtu.be/E8Fxtpr24Zg
-- GHCi, version 8.8.4
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TypeOperators #-}
{-# OPTIONS_GHC -Wno-incomplete-patterns #-}
import PrettyT -- https://www.youtube.com/watch?v=Ud-1Z0hBlB8&t=379s
-- data Btree a = Empty | Node a (Btree a) (Btree a) deriving Show
import Data.Ord
import Data.List
@evgenii-malov
evgenii-malov / segment_tree.hs
Last active April 7, 2022 14:54
segment tree build and query
-- video https://www.youtube.com/watch?v=nckWiHmeNXU&feature=youtu.be
{-# LANGUAGE RankNTypes #-}
-- GHCi, version 8.8.4
-- data Btree a = Empty | Node a (Btree a) (Btree a) deriving Show
-- https://www.youtube.com/watch?v=Ud-1Z0hBlB8&t=379s
-- data Btree a = Empty | Node a (Btree a) (Btree a) deriving Show
{-# OPTIONS_GHC -Wno-incomplete-patterns #-}
import PrettyT -- https://www.youtube.com/watch?v=Ud-1Z0hBlB8&t=3s
import Data.List.Split (chunksOf, whenElt)
@evgenii-malov
evgenii-malov / merkle_tree.hs
Created April 1, 2022 16:43
merkle tree and merkle path build and transaction in a block proof function
-- GHCi, version 8.8.4
-- data Btree a = Empty | Node a (Btree a) (Btree a) deriving Show
import PrettyT -- https://www.youtube.com/watch?v=Ud-1Z0hBlB8&t=379s
import Data.Hashable
import Data.List.Split
h v = show $ hash v
merkle :: (String -> String) -> [String] -> Btree String
merkle h d = go nl