Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
kamil-adam committed Jan 24, 2025
1 parent 2992b0c commit afb0ca9
Show file tree
Hide file tree
Showing 51 changed files with 1,339 additions and 120 deletions.
3 changes: 3 additions & 0 deletions docs/developers/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# 📅 Revision history for HelVM Common

## 0.2.0.0 -- 2025-01-24
* Refactor

## 0.1.2.5 -- 2025-01-11
* Update libraries

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style type="text/css">
span.lineno { color: white; background: #aaaaaa; border-right: solid white 12px }
span.nottickedoff { background: yellow}
span.istickedoff { background: white }
span.tickonlyfalse { margin: -1px; border: 1px solid #f20913; background: #f20913 }
span.tickonlytrue { margin: -1px; border: 1px solid #60de51; background: #60de51 }
span.funcount { font-size: small; color: orange; z-index: 2; position: absolute; right: 20 }
span.decl { font-weight: bold }
span.spaces { background: white }
</style>
</head>
<body>
<pre>
<span class="decl"><span class="nottickedoff">never executed</span> <span class="tickonlytrue">always true</span> <span class="tickonlyfalse">always false</span></span>
</pre>
<pre>
<span class="lineno"> 1 </span>module HelVM.HelIO.Collections.MapList where
<span class="lineno"> 2 </span>
<span class="lineno"> 3 </span>import HelVM.HelIO.Containers.LLIndexSafe
<span class="lineno"> 4 </span>import HelVM.HelIO.Containers.LLInsertDef
<span class="lineno"> 5 </span>import HelVM.HelIO.Control.Safe
<span class="lineno"> 6 </span>
<span class="lineno"> 7 </span>import Control.Monad.Extra
<span class="lineno"> 8 </span>
<span class="lineno"> 9 </span>import Data.Default
<span class="lineno"> 10 </span>
<span class="lineno"> 11 </span>import qualified Data.IntMap as IntMap
<span class="lineno"> 12 </span>import qualified Data.List.Index as List
<span class="lineno"> 13 </span>import qualified Data.ListLike as LL
<span class="lineno"> 14 </span>import qualified GHC.Exts as I (IsList (..))
<span class="lineno"> 15 </span>import qualified Text.Show
<span class="lineno"> 16 </span>
<span class="lineno"> 17 </span>-- | Construction
<span class="lineno"> 18 </span>mapListEmpty :: MapList a
<span class="lineno"> 19 </span><span class="decl"><span class="nottickedoff">mapListEmpty = mapListFromList []</span></span>
<span class="lineno"> 20 </span>
<span class="lineno"> 21 </span>mapListFromList :: [a] -&gt; MapList a
<span class="lineno"> 22 </span><span class="decl"><span class="istickedoff">mapListFromList = fromIndexedList &lt;$&gt; List.indexed</span></span>
<span class="lineno"> 23 </span>
<span class="lineno"> 24 </span>fromIndexedList :: IndexedList a -&gt; MapList a
<span class="lineno"> 25 </span><span class="decl"><span class="istickedoff">fromIndexedList = fromIntMap &lt;$&gt; IntMap.fromList</span></span>
<span class="lineno"> 26 </span>
<span class="lineno"> 27 </span>fromIntMap :: IntMap a -&gt; MapList a
<span class="lineno"> 28 </span><span class="decl"><span class="istickedoff">fromIntMap = MapList</span></span>
<span class="lineno"> 29 </span>
<span class="lineno"> 30 </span>-- | DeConstruction
<span class="lineno"> 31 </span>mapListToList :: Default a =&gt; MapList a -&gt; [a]
<span class="lineno"> 32 </span><span class="decl"><span class="istickedoff">mapListToList = listFromDescList &lt;$&gt; toDescList</span></span>
<span class="lineno"> 33 </span>
<span class="lineno"> 34 </span>toDescList :: MapList a -&gt; IndexedList a
<span class="lineno"> 35 </span><span class="decl"><span class="istickedoff">toDescList = IntMap.toDescList &lt;$&gt; unMapList</span></span>
<span class="lineno"> 36 </span>
<span class="lineno"> 37 </span>-- | Internal function
<span class="lineno"> 38 </span>listFromDescList :: Default a =&gt; IndexedList a -&gt; [a]
<span class="lineno"> 39 </span><span class="decl"><span class="istickedoff">listFromDescList = loop act &lt;$&gt; ([] , ) where</span>
<span class="lineno"> 40 </span><span class="spaces"> </span><span class="istickedoff">act :: Default a =&gt; AccWithIndexedList a -&gt; Either (AccWithIndexedList a) [a]</span>
<span class="lineno"> 41 </span><span class="spaces"> </span><span class="istickedoff">act (acc , [] ) = <span class="nottickedoff">Right acc</span></span>
<span class="lineno"> 42 </span><span class="spaces"> </span><span class="istickedoff">act (acc , [(i , v)] ) = Right $ consDef i $ v : acc</span>
<span class="lineno"> 43 </span><span class="spaces"> </span><span class="istickedoff">act (acc , (i1 , v1) : (i2 , v2) : l ) = Left (consDef (i1 - i2 - 1) $ v1 : acc , (i2 , v2) : l)</span></span>
<span class="lineno"> 44 </span>
<span class="lineno"> 45 </span>consDef :: Default a =&gt; Key -&gt; [a] -&gt; [a]
<span class="lineno"> 46 </span><span class="decl"><span class="istickedoff">consDef i l = (check &lt;$&gt; compare i) 0 where</span>
<span class="lineno"> 47 </span><span class="spaces"> </span><span class="istickedoff">check LT = <span class="nottickedoff">error &quot;MapList.consDef index is negative&quot;</span></span>
<span class="lineno"> 48 </span><span class="spaces"> </span><span class="istickedoff">check EQ = l</span>
<span class="lineno"> 49 </span><span class="spaces"> </span><span class="istickedoff">check GT = consDef (i - 1) (def : l)</span></span>
<span class="lineno"> 50 </span>
<span class="lineno"> 51 </span>-- | Types
<span class="lineno"> 52 </span>type AccWithIndexedList a = ([a] , IndexedList a)
<span class="lineno"> 53 </span>type Key = IntMap.Key
<span class="lineno"> 54 </span>type IndexedList a = [(Key , a)]
<span class="lineno"> 55 </span>type MapString = MapList Char
<span class="lineno"> 56 </span>
<span class="lineno"> 57 </span>newtype MapList a = MapList {<span class="istickedoff"><span class="decl"><span class="istickedoff">unMapList</span></span></span> :: IntMap a}
<span class="lineno"> 58 </span> deriving stock (<span class="decl"><span class="nottickedoff"><span class="decl"><span class="nottickedoff">Eq</span></span></span></span> , <span class="decl"><span class="nottickedoff"><span class="decl"><span class="nottickedoff"><span class="decl"><span class="nottickedoff"><span class="decl"><span class="nottickedoff"><span class="decl"><span class="nottickedoff"><span class="decl"><span class="nottickedoff"><span class="decl"><span class="nottickedoff">Ord</span></span></span></span></span></span></span></span></span></span></span></span></span></span>, <span class="decl"><span class="nottickedoff"><span class="decl"><span class="nottickedoff"><span class="decl"><span class="nottickedoff"><span class="decl"><span class="nottickedoff">Read</span></span></span></span></span></span></span></span>)
<span class="lineno"> 59 </span> deriving newtype (<span class="decl"><span class="nottickedoff"><span class="decl"><span class="nottickedoff"><span class="decl"><span class="nottickedoff">Semigroup</span></span></span></span></span></span> , <span class="decl"><span class="nottickedoff"><span class="decl"><span class="nottickedoff"><span class="decl"><span class="nottickedoff">Monoid</span></span></span></span></span></span>)
<span class="lineno"> 60 </span>
<span class="lineno"> 61 </span>-- | Standard instances
<span class="lineno"> 62 </span>instance <span class="decl"><span class="nottickedoff"><span class="decl"><span class="nottickedoff">(Default a , Show a) =&gt; Show (MapList a)</span></span></span></span> where
<span class="lineno"> 63 </span> <span class="decl"><span class="nottickedoff">show = show &lt;$&gt; I.toList</span></span>
<span class="lineno"> 64 </span>
<span class="lineno"> 65 </span>instance IsString MapString where
<span class="lineno"> 66 </span> <span class="decl"><span class="nottickedoff">fromString = mapListFromList</span></span>
<span class="lineno"> 67 </span>
<span class="lineno"> 68 </span>instance Default a =&gt; IsList (MapList a) where
<span class="lineno"> 69 </span> type (Item (MapList a)) = a
<span class="lineno"> 70 </span> <span class="decl"><span class="istickedoff">toList = mapListToList</span></span>
<span class="lineno"> 71 </span> <span class="decl"><span class="istickedoff">fromList = mapListFromList</span></span>
<span class="lineno"> 72 </span> <span class="decl"><span class="nottickedoff">fromListN n = mapListFromList &lt;$&gt; fromListN n</span></span>
<span class="lineno"> 73 </span>
<span class="lineno"> 74 </span>-- | ListLike instances
<span class="lineno"> 75 </span>instance <span class="decl"><span class="nottickedoff"><span class="decl"><span class="nottickedoff"><span class="decl"><span class="nottickedoff"><span class="decl"><span class="nottickedoff">Default a =&gt; LL.FoldableLL (MapList a) a</span></span></span></span></span></span></span></span> where
<span class="lineno"> 76 </span> <span class="decl"><span class="nottickedoff">foldl f b = IntMap.foldl f b &lt;$&gt; unMapList</span></span>
<span class="lineno"> 77 </span> <span class="decl"><span class="nottickedoff">foldr f b = IntMap.foldr f b &lt;$&gt; unMapList</span></span>
<span class="lineno"> 78 </span>
<span class="lineno"> 79 </span>-- | My instances
<span class="lineno"> 80 </span>instance {-# OVERLAPPING #-} IndexSafe (MapList a) a where
<span class="lineno"> 81 </span> <span class="decl"><span class="nottickedoff">findWithDefault e i = IntMap.findWithDefault e i &lt;$&gt; unMapList</span></span>
<span class="lineno"> 82 </span> <span class="decl"><span class="nottickedoff">findMaybe = mapListFindMaybe</span></span>
<span class="lineno"> 83 </span> <span class="decl"><span class="nottickedoff">indexMaybe = mapListIndexMaybe</span></span>
<span class="lineno"> 84 </span> <span class="decl"><span class="nottickedoff">findSafe i = liftMaybeOrError &quot;MapList.findSafe: index is not correct&quot; &lt;$&gt; mapListFindMaybe i</span></span>
<span class="lineno"> 85 </span> <span class="decl"><span class="nottickedoff">indexSafe l = liftMaybeOrError &quot;MapList.LLIndexSafe: index is not correct&quot; &lt;$&gt; mapListIndexMaybe l</span></span>
<span class="lineno"> 86 </span>
<span class="lineno"> 87 </span>instance InsertDef (MapList a) a where
<span class="lineno"> 88 </span> <span class="decl"><span class="nottickedoff">insertDef i e = fromIntMap . IntMap.insert i e &lt;$&gt; unMapList</span></span>
<span class="lineno"> 89 </span>
<span class="lineno"> 90 </span>-- | Internal functions
<span class="lineno"> 91 </span>mapListFindMaybe :: Key -&gt; MapList a -&gt; Maybe a
<span class="lineno"> 92 </span><span class="decl"><span class="nottickedoff">mapListFindMaybe i = IntMap.lookup i &lt;$&gt; unMapList</span></span>
<span class="lineno"> 93 </span>
<span class="lineno"> 94 </span>mapListIndexMaybe :: MapList a -&gt; Key -&gt; Maybe a
<span class="lineno"> 95 </span><span class="decl"><span class="nottickedoff">mapListIndexMaybe l i = unMapList l IntMap.!? i</span></span>

</pre>
</body>
</html>
Loading

0 comments on commit afb0ca9

Please sign in to comment.