Commit 8f71397f authored by Erin van der Veen's avatar Erin van der Veen 💛

Merge branch 'add-optimized-functions' into 'master'

Add optimized functions (strict folds on Map; concat for 3-5 strings)

See merge request !363
parents 389aa65c 2ff6c4da
Pipeline #45555 passed with stage
in 2 minutes and 54 seconds
......@@ -241,6 +241,9 @@ delU :: !k !w:(Map k u:v) -> x:(?u:v, !y:(Map k u:v)) | == k & < k, [ w y <= u,
*/
foldrWithKey :: !(k v u:a -> u:a) !u:a !(Map k v) -> u:a
//* Like `foldrWithKey`, but with strict application of the operator.
foldrWithKey` :: !(k v u:a -> u:a) !u:a !(Map k v) -> u:a
/**
* Folds the keys and values in the map using the given left-associative binary
* operator, such that
......@@ -249,6 +252,9 @@ foldrWithKey :: !(k v u:a -> u:a) !u:a !(Map k v) -> u:a
*/
foldlWithKey :: !(.a -> .(k -> .(v -> .a))) !.a !(Map k v) -> .a
//* Like `foldlWithKey`, but with strict application of the operator.
foldlWithKey` :: !(.a -> .(k -> .(v -> .a))) !.a !(Map k v) -> .a
//* @type (v a -> a) a (Map k v) -> a
foldrNoKey f x m :== foldrWithKey (\_ v acc -> f v acc) x m
......
......@@ -339,10 +339,24 @@ foldrWithKey :: !(k v u:a -> u:a) !u:a !(Map k v) -> u:a
foldrWithKey f z` Tip = z`
foldrWithKey f z` (Bin _ kx x l r) = foldrWithKey f (f kx x (foldrWithKey f z` r)) l
foldrWithKey` :: !(k v u:a -> u:a) !u:a !(Map k v) -> u:a
foldrWithKey` f e Tip = e
foldrWithKey` f e (Bin _ kx x l r)
#! e = foldrWithKey` f e r
#! e = f kx x e
= foldrWithKey` f e l
foldlWithKey :: !(.a -> .(k -> .(v -> .a))) !.a !(Map k v) -> .a
foldlWithKey f z` Tip = z`
foldlWithKey f z` (Bin _ kx x l r) = foldlWithKey f (f (foldlWithKey f z` l) kx x) r
foldlWithKey` :: !(.a -> .(k -> .(v -> .a))) !.a !(Map k v) -> .a
foldlWithKey` f e Tip = e
foldlWithKey` f e (Bin _ kx x l r)
#! e = foldlWithKey` f e l
#! e = f e kx x
= foldlWithKey f e r
//////////////////////////////////////////////////////////////////////
// List variations
//////////////////////////////////////////////////////////////////////
......
......@@ -152,6 +152,10 @@ instance Text String
instance Text [Char]
instance + String
concat3 :: !String !String !String -> String
concat4 :: !String !String !String !String -> String
concat5 :: !String !String !String !String !String -> String
/**
* Concatenate two things.
* This is a simple wrapper around the {{`+++`}} instance for `String` which
......
......@@ -16,10 +16,6 @@ instance Text String
concat` [] dst _ = dst
concat` [x:xs] dst offset = concat` xs (copyChars offset (size x-1) x dst) (offset + size x)
copyChars :: !Int !Int !String !*String -> *String
copyChars _ -1 _ dst = dst
copyChars offset i src dst = copyChars offset (i-1) src {dst & [offset+i]=src.[i]}
split :: !String !String -> [String]
split sep s = splitAfter 0 (size s-1) sep s
where
......@@ -251,6 +247,49 @@ where
dropChars :: !Int ![Char] -> [Char]
dropChars n cs = drop n cs
concat3 :: !String !String !String -> String
concat3 a b c =
copyChars 0 (sa-1) a $
copyChars sa (sb-1) b $
copyChars (sa+sb) (sc-1) c $
'StdArray'._createArray (sa+sb+sc)
where
sa = size a
sb = size b
sc = size c
concat4 :: !String !String !String !String -> String
concat4 a b c d =
copyChars 0 (sa-1) a $
copyChars sa (sb-1) b $
copyChars (sa+sb) (sc-1) c $
copyChars (sa+sb+sc) (sd-1) d $
'StdArray'._createArray (sa+sb+sc+sd)
where
sa = size a
sb = size b
sc = size c
sd = size d
concat5 :: !String !String !String !String !String -> String
concat5 a b c d e =
copyChars 0 (sa-1) a $
copyChars sa (sb-1) b $
copyChars (sa+sb) (sc-1) c $
copyChars (sa+sb+sc) (sd-1) d $
copyChars (sa+sb+sc+sd) (se-1) e $
'StdArray'._createArray (sa+sb+sc+sd)
where
sa = size a
sb = size b
sc = size c
sd = size d
se = size e
copyChars :: !Int !Int !String !*String -> *String
copyChars _ -1 _ dst = dst
copyChars offset i src dst = copyChars offset (i-1) src {dst & [offset+i]=src.[i]}
instance + String
where
(+) s1 s2 = s1 +++ s2
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment