From 1c4cc2105949c92b3a7aeceda64e34ecae0dbe31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ondr=CC=8Cej=20S=CC=8Cebek?= Date: Sat, 7 Oct 2023 17:20:51 +0200 Subject: [PATCH] Use unsnocNE for HLint --- src/Swarm/Language/Pretty.hs | 6 ++---- src/Swarm/Util.hs | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/Swarm/Language/Pretty.hs b/src/Swarm/Language/Pretty.hs index a8bbac21d..7dcde5077 100644 --- a/src/Swarm/Language/Pretty.hs +++ b/src/Swarm/Language/Pretty.hs @@ -9,7 +9,6 @@ -- Pretty-printing for the Swarm language. module Swarm.Language.Pretty where -import Control.Lens (unsnoc) import Control.Lens.Combinators (pattern Empty) import Control.Unification import Control.Unification.IntVar @@ -17,7 +16,6 @@ import Data.Bool (bool) import Data.Functor.Fixedpoint (Fix, unFix) import Data.List.NonEmpty qualified as NE import Data.Map.Strict qualified as M -import Data.Maybe (fromJust) import Data.Set (Set) import Data.Set qualified as S import Data.String (fromString) @@ -32,7 +30,7 @@ import Swarm.Language.Parse (getLocRange) import Swarm.Language.Syntax import Swarm.Language.Typecheck import Swarm.Language.Types -import Swarm.Util (showEnum, showLowT) +import Swarm.Util (showEnum, showLowT, unsnocNE) import Witch ------------------------------------------------------------ @@ -160,7 +158,7 @@ instance ((UnchainableFun t), (PrettyPrec t)) => PrettyPrec (TypeF t) where prettyPrec p (TyCmdF ty) = pparens (p > 9) $ "cmd" <+> prettyPrec 10 ty prettyPrec _ (TyDelayF ty) = braces $ ppr ty prettyPrec p (TyFunF ty1 ty2) = - let (iniF, lastF) = fromJust . unsnoc $ ty1 : unchainFun ty2 + let (iniF, lastF) = unsnocNE $ ty1 NE.:| unchainFun ty2 funs = (prettyPrec 1 <$> iniF) <> [ppr lastF] inLine l r = l <+> "->" <+> r multiLine l r = l <+> "->" <> hardline <> r diff --git a/src/Swarm/Util.hs b/src/Swarm/Util.hs index 08424dbc5..785466c50 100644 --- a/src/Swarm/Util.hs +++ b/src/Swarm/Util.hs @@ -27,6 +27,7 @@ module Swarm.Util ( surfaceEmpty, applyWhen, hoistMaybe, + unsnocNE, -- * Directory utilities readFileMay, @@ -239,6 +240,23 @@ applyWhen False _ x = x hoistMaybe :: (Applicative m) => Maybe b -> MaybeT m b hoistMaybe = MaybeT . pure +-- | Like 'unsnoc', but for 'NonEmpty' so without the 'Maybe' +-- +-- Taken from Cabal-syntax Distribution.Utils.Generic. +-- +-- Example: +-- +-- >>> unsnocNE (1 :| [2, 3]) +-- ([1,2],3) +-- +-- >>> unsnocNE (1 :| []) +-- ([],1) +unsnocNE :: NonEmpty a -> ([a], a) +unsnocNE (x :| xs) = go x xs + where + go y [] = ([], y) + go y (z : zs) = let ~(ws, w) = go z zs in (y : ws, w) + ------------------------------------------------------------ -- Directory stuff