Skip to content

Instantly share code, notes, and snippets.

View drchaos's full-sized avatar

Yura Sheliah drchaos

View GitHub Profile
@drchaos
drchaos / genBoyerMoore.hs
Created July 31, 2023 12:44
generic Boyer-Moore
genericMatchUp :: [ByteString] -> ByteString -> Bool
genericMatchUp pats = isJust . foldr1 (>=>) . fmap strip pats
strip :: ByteString -> ByteString -> Maybe ByteString
strip pat
| null pat = Just
strip pat = breaker
where
!patLen = length pat
searcher = nonOverlappingIndices pat
@drchaos
drchaos / globBoyerMoore.hs
Created July 31, 2023 12:19
Glob-like pattern Boyer-Moore
-- Эта функция очень похожа на предыдущую. Она находит первое
-- вхожение подстроки и возвращает остаток
strip :: ByteString -> ByteString -> Maybe ByteString
strip pat
| null pat = Just
strip pat = breaker
where
!patLen = length pat
searcher = nonOverlappingIndices pat
breaker str = case searcher str of
@drchaos
drchaos / matcherBoyerMoore.hs
Last active July 31, 2023 11:09
Boyer-Moore matcher
matchUp :: ByteString
-> ByteString
-> Bool
matchUp pat
| null pat = null
matchUp pat = searcher
where
searcher = case nonOverlappingIndicies pat of -- это библиотечная функция
[] -> False
_ -> True
------------------ My code --------------------------
fnv1aHash32Word32# :: Word# -> Word# -> Word#
fnv1aHash32Word32# base word =
let b0 = word `and#` 0xFF##
b1 = (word `uncheckedShiftRL#` 8#) `and#` 0xFF##
b2 = (word `uncheckedShiftRL#` 16#) `and#` 0xFF##
b3 = (word `uncheckedShiftRL#` 24#) `and#` 0xFF##
in base <<>> b0 <<>> b1 <<>> b2 <<>> b3
where
(<<>>) = fnv1aHash32Base#
hashBS :: ByteString -> Word32
hashBS = B.foldl fnv1aHash32Base fnvOffsetBasis32
hashBS_unpack :: ByteString -> Word32
hashBS_unpack = foldl' fnv1aHash32Base fnvOffsetBasis32 . B.unpack
hashBS_С :: ByteString -> Word32
hashBS_С bs = B.unsafeDupablePerformIO $!
B.unsafeUseAsCStringLen bs (\ (ptr, l) -> fnv1a_32 (castPtr ptr) l)
--------------------- Idiomatic ------------------------
fnvPrime32 :: Word32
fnvPrime32 = 16777619
fnvOffsetBasis32 :: Word32
fnvOffsetBasis32 = 2166136261
fnv1aHash32Word32 :: Word32 -> Word32 -> Word32
fnv1aHash32Word32 base word =
-- ghc 8.10.7
-- Server code
runServer ctx = do
bracket (spawnWorkers ctx) (mapM_ killThread) $ const
liftIO $ useContext ctx -- At this point context looks uninialized
where
spawnWorkers = replicateM (ctx ^. number) (fork $ worker ctx)
-- The code that works with optimization disabled but but don't with optimization enabled
{
"languageserver": {
"haskell": {
"command": "haskell-language-server-wrapper",
"args": ["--lsp"],
"rootPatterns": [
"*.cabal",
"stack.yaml",
"cabal.project",
"package.yaml",
Возьмем для примера Influx Line Protocol, поскольку он прост. Приходят к нам в порт данные и
мы должны их как-то распарсить, а потом что-то с ними сделать. Для простоты возьмём только поля (Fileds) и засунем их
в Map.
> type Name = Text
> type Value = Text
> type Fields = Map Name Value
Это нетипизированные данные, а мы хотим прочитать оттуда RADIUS Packet Attributes которые весьма даже типизированны
https://hackage.haskell.org/package/radius-0.6.1.0/docs/Network-RADIUS-Types.html
@drchaos
drchaos / Maybe.hs
Last active January 6, 2020 08:12
Maybe examples
-- Вариант с последовательным поиском
lookupParam :: String -> [(String,String)] -> Maybe String
-- реализация
lookupEnvVar :: String -> [(String,String)] -> Maybe String
-- реализация
seqLookup :: String -> [(String,String)] -> [(String,String)] -> Maybe String
seqLookup paramName params envVars = do