Skip to content

Commit

Permalink
Use MinLenVect
Browse files Browse the repository at this point in the history
  • Loading branch information
albertprz committed Nov 23, 2023
1 parent 4a26edc commit 14fef83
Show file tree
Hide file tree
Showing 19 changed files with 100 additions and 555 deletions.
8 changes: 4 additions & 4 deletions packages.dhall
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
let upstream =
https://github.com/purescript/package-sets/releases/download/psc-0.15.12-20231120/packages.dhall
sha256:4c066f08ee174c39f5a65f7899f8e3dafdf75c416747eda8a6c6f47e4ac4faaa
https://github.com/purescript/package-sets/releases/download/psc-0.15.12-20231123/packages.dhall
sha256:95ecd1a23305f270971f4d4f2040541559116de6e21aba773d368787f7f1ed35

let overrides =
{ spec-discovery.version = "v8.2.0"
, spec-discovery.repo
= "https://github.com/purescript-spec/purescript-spec-discovery"
, spec-discovery.dependencies
=
[ "aff"
Expand All @@ -13,8 +15,6 @@ let overrides =
, "prelude"
, "spec"
]
, spec-discovery.repo
= "https://github.com/purescript-spec/purescript-spec-discovery"
}

in upstream // overrides
2 changes: 0 additions & 2 deletions spago.dhall
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
, "console"
, "control"
, "debug"
, "distributive"
, "effect"
, "either"
, "enums"
Expand Down Expand Up @@ -40,7 +39,6 @@
, "tree-rose"
, "tuples"
, "typelevel-prelude"
, "unfoldable"
, "unordered-collections"
, "unsafe-coerce"
, "web-clipboard"
Expand Down
4 changes: 2 additions & 2 deletions src/Components/Table/Cell.purs
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,10 @@ swapTableMapRow origin target tableDict =
(\cell -> cell.row == origin || cell.row == target)
(HashMap.keys tableDict)

allColumns :: NonEmptyArray Column
allColumns :: MinLenVect 1 Column
allColumns = enumValues

allRows :: NonEmptyArray Row
allRows :: MinLenVect 1 Row
allRows = enumValues

mkColumn :: Char -> Column
Expand Down
3 changes: 1 addition & 2 deletions src/Components/Table/Handler.purs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ import Web.UIEvent.WheelEvent (deltaX, deltaY)
handleAction
:: forall slots o m
. MonadAff m
=> Action
-> HalogenM AppState Action slots o m Unit
=> Action -> HalogenM AppState Action slots o m Unit

handleAction Initialize = do
loadPrelude
Expand Down
9 changes: 5 additions & 4 deletions src/Components/Table/HandlerHelpers.purs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import App.Interpreter.Module (reloadModule)
import App.Utils.Dom (class IsEvent, emptyFormulaBox, focusCell, getClipboard, getFormulaBoxContents, getVisibleCols, getVisibleRows, parseElements, scrollCellLeft, scrollCellRight, shiftKey, withPrevent)
import App.Utils.HashMap (bulkDelete, lookup2, updateJust) as HashMap
import Bookhound.Parser (runParser)
import Data.Array as Array
import Data.HashMap (insert, keys, lookup, union, unionWith) as HashMap
import Data.List.NonEmpty (NonEmptyList)
import Data.Set as Set
Expand Down Expand Up @@ -147,12 +148,12 @@ goToCellHelper
-> Effect Unit
goToCellHelper cols origin { column, row } visibleCols

| last' cols == Just column && origin.column /= top
, Just element <- head' visibleCols = do
| Array.last cols == Just column && origin.column /= top
, Just element <- Array.head visibleCols = do
scrollCellRight element

| head' cols == Just column && origin.column /= bottom
, Just element <- head' visibleCols = do
| Array.head cols == Just column && origin.column /= bottom
, Just element <- Array.head visibleCols = do
scrollCellLeft element

| otherwise = focusCell { column, row }
Expand Down
2 changes: 1 addition & 1 deletion src/Components/Table/Models.purs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type AppState =
, tableFormulas :: HashMap Cell FormulaId
, tableDependencies :: HashMap Cell (NonEmptySet FormulaId)
, formulaCache :: HashMap FormulaId Formula
, rows :: NonEmptyArray Row
, rows :: MinLenVect 1 Row
, multiSelection :: MultiSelection
, selectionState :: SelectionState
, draggedHeader :: Maybe Header
Expand Down
2 changes: 1 addition & 1 deletion src/Components/Table/Renderer.purs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module App.Components.Table.Renderer where

import FatPrelude hiding (div, span)
import FatPrelude hiding (div)
import Prim hiding (Row)

import App.CSS.ClassNames (aboveSelection, atLeftSelection, atRightSelection, belowSelection, columnHeader, copySelection, cornerHeader, formulaBox, formulaBoxContainer, formulaCellInput, formulaContainer, formulaSignature, inSelection, mainContainer, rowHeader, selectedCellInput, selectedHeader, selectedSheetCell, sheetCell)
Expand Down
11 changes: 6 additions & 5 deletions src/Components/Table/Selection.purs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import FatPrelude
import Prim hiding (Row)

import App.Components.Table.Cell (Cell, CellMove(..), CellValue, Column, Row, allColumns, allRows, getCell, getColumnCell, getRowCell, nextRowCell, parseCellValue, prevColumnCell)
import Data.Array as Array
import Data.HashMap as HashMap
import Data.String.Pattern (Pattern(..))

Expand Down Expand Up @@ -124,23 +125,23 @@ deserializeSelectionValues
:: Cell -> String -> HashMap Cell CellValue
deserializeSelectionValues selectedCell str = HashMap.fromArray
do
rowValues /\ row <- zip' values (selectedCell.row .. top)
value /\ column <- zip' rowValues (selectedCell.column .. top)
rowValues /\ row <- Array.zip values (toArray (selectedCell.row .. top))
value /\ column <- Array.zip rowValues (toArray (selectedCell.column .. top))
pure $ { row, column } /\ parseCellValue value
where
values = split (Pattern tab) <$> split (Pattern newline) str

getTargetCells
:: MultiSelection
-> Cell
-> (NonEmptyArray (NonEmptyArray Cell))
-> (MinLenVect 1 (MinLenVect 1 Cell))
getTargetCells selection selectedCell =
fromMaybe (singleton $ singleton selectedCell)
(getSelectionCells selection)

getSelectionCells
:: MultiSelection
-> Maybe (NonEmptyArray (NonEmptyArray Cell))
-> Maybe (MinLenVect 1 (MinLenVect 1 Cell))
getSelectionCells selection = do
columnBounds /\ rowBounds <- getSelectionBounds selection
pure do
Expand All @@ -166,7 +167,7 @@ isRowsSelection _ = false

getSelectionBounds
:: MultiSelection
-> Maybe (NonEmptyArray Column /\ NonEmptyArray Row)
-> Maybe (MinLenVect 1 Column /\ MinLenVect 1 Row)
getSelectionBounds NoSelection = Nothing
getSelectionBounds AllSelection =
Just $ allColumns /\ allRows
Expand Down
7 changes: 4 additions & 3 deletions src/Components/Table/SyntaxAtom.purs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import Bookhound.Parsers.Char (alpha, alphaNum, lower, quote, underscore)
import Bookhound.Parsers.Char as Parsers
import Bookhound.Parsers.Number (double, int)
import Bookhound.Parsers.String (betweenDoubleQuotes, betweenQuotes)
import Data.Array as Array
import Data.String.CodeUnits (singleton) as String
import Data.String.Unsafe (char) as String
import Web.HTML.Common (ClassName)
Expand All @@ -36,9 +37,9 @@ condenseSyntaxAtoms :: Array SyntaxAtom -> Array SyntaxAtom
condenseSyntaxAtoms = foldl go []
where
go xs (OtherText y)
| Just { init, last: (OtherText x) } <- unsnoc' xs =
toArray $ snoc' init $ OtherText (x <> y)
go xs y = toArray $ snoc' xs y
| Just { init, last: (OtherText x) } <- Array.unsnoc xs =
Array.snoc init $ OtherText (x <> y)
go xs y = Array.snoc xs y

syntaxAtomParser :: Parser (Array SyntaxAtom)
syntaxAtomParser = (|+) atom
Expand Down
10 changes: 6 additions & 4 deletions src/Evaluator/Builtins.purs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ import Data.Array as Array
import Data.Array.NonEmpty.Internal (NonEmptyArray(..))
import Data.Bifunctor (rmap)
import Data.EuclideanRing as Ring
import Data.Foldable (foldl)
import Data.HashMap as HashMap
import Data.Int (toNumber)
import Data.List (List(..))
import Data.List as List
import Data.Set as Set
import Data.String.CodeUnits (drop, dropRight, length, singleton, slice, take, takeRight, toCharArray) as String
import Data.Tuple.Nested (type (/\))
import FatPrelude (HashMap, Maybe(..), all, arr2, bimap, elem, fold, foldl1, fromCharArray, fromMaybe, toCharArray, traverse, ($), (&&), (*), (+), (-), (..), (/), (/=), (/\), (<), (<$>), (<..), (<<<), (<=), (<>), (==), (>), (>=), (||))
import FatPrelude (HashMap, Maybe(..), all, bimap, elem, fold, fromCharArray, fromMaybe, toArray, toCharArray, traverse, ($), (&&), (*), (+), (-), (..), (/), (/=), (/\), (<), (<$>), (<..), (<<<), (<=), (<>), (==), (>), (>=), (||))
import Partial.Unsafe (unsafePartial)
import Prelude as Prelude

Expand Down Expand Up @@ -250,7 +251,8 @@ snocSig = [ Var "xs" /\ arrayOf a, Var "y" /\ a ] /\ arrayOf a

concat :: Function
concat [ ListObj xs ] = concat [ ArrayObj $ Array.fromFoldable xs ]
concat [ ArrayObj xs ] = foldl1 (append <.. arr2) $ NonEmptyArray xs
concat [ ArrayObj xs ] = foldl (append <.. \x y -> [x, y]) NullObj
$ NonEmptyArray xs

concatSig :: Sig
concatSig = [ Var "xss" /\ (arrayOf $ arrayOf a) ] /\ arrayOf a
Expand All @@ -276,8 +278,8 @@ containsSig :: Sig
containsSig = [ Var "x" /\ a, Var "ys" /\ arrayOf a ] /\ bool

range :: Function
range [ IntObj x, IntObj y ] = ArrayObj $ IntObj <$> (x .. y)
range [ CharObj x, CharObj y ] = ArrayObj $ CharObj <$> (x .. y)
range [ IntObj x, IntObj y ] = ArrayObj $ IntObj <$> toArray (x .. y)
range [ CharObj x, CharObj y ] = ArrayObj $ CharObj <$> toArray (x .. y)

rangeSig :: Sig
rangeSig = [ Var "start" /\ a, Var "end" /\ a ] /\ arrayOf a
Expand Down
8 changes: 4 additions & 4 deletions src/Evaluator/Common.purs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ registerBindings bindings = do
{ scope, scopeLoc } <- get
let (Scope maxScope) = fromMaybe scope $ maximum $ toTree scopeLoc
let scopes = Scope <<< (_ + maxScope) <$> (1 .. length bindings)
traverse_ (\(n /\ x) -> registerLocalFn n x) (scopes `zip'` bindings)
traverse_ (\(n /\ x) -> registerLocalFn n x) ((toArray scopes) `Array.zip` bindings)
modify_ \st -> st
{ scopeLoc = appendChildren (mkLeaf <$> List.fromFoldable scopes)
st.scopeLoc
Expand Down Expand Up @@ -135,12 +135,12 @@ getAvailableFns
. (Maybe Module -> a -> b)
-> (Maybe Module /\ a)
-> LocalFormulaCtx
-> NonEmptyArray b
-> MinLenVect 1 b
getAvailableFns
ctor
(fnModule /\ fnName)
{ module', importedModulesMap, aliasedModulesMap } =
flip ctor fnName <<< pure <$> cons' module' (Array.fromFoldable modules)
flip ctor fnName <<< pure <$> cons module' (fromFoldable modules)
where
modules = case fnModule of
Just alias -> fromMaybe Set.empty
Expand Down Expand Up @@ -191,7 +191,7 @@ getNewFnState (FnInfo { id: maybeFnId, scope, params, argsMap }) fnArgs =
, returnType: Nothing
}
)
<$> zip' ((scope /\ _) <<< fst <$> params) args
<$> Array.zip ((scope /\ _) <<< fst <$> params) args
args =
if isJust maybeFnId then resetScope <$> fnArgs
else fnArgs
Expand Down
46 changes: 23 additions & 23 deletions src/Evaluator/Expression.purs
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ evalExpr
evalExpr $ Array' (Array' <$> matrix)
where
matrix = do
row <- rowX .. rowY
row <- toArray (rowX .. rowY)
pure $ do
column <- colX .. colY
column <- toArray (colX .. colY)
pure $ Cell' { column, row }

evalExpr
Expand All @@ -93,18 +93,18 @@ evalExpr
)
| rowX == rowY =
evalExpr $ Array' $ Cell' <<< { column: _, row: rowX }
<$> (colX .. colY)
<$> toArray (colX .. colY)
| colX == colY =
evalExpr $ Array' $ Cell' <<< { column: colX, row: _ }
<$> (rowX .. rowY)
<$> toArray (rowX .. rowY)
| otherwise =
raiseError $ TypeError' $ InvalidCellArrayRange x y

evalExpr (ArrayRange x y) = evalExpr $ FnApply (varFn "range") [ x, y ]

evalExpr (Array' array) =
evalExpr
$ foldl (FnApply (varFn "snoc") <.. arr2)
$ foldl (FnApply (varFn "snoc") <.. \x y -> [x, y])
(Object' $ ArrayObj [])
array

Expand Down Expand Up @@ -148,20 +148,20 @@ evalFn (FnInfo fnInfo@{ body, params, id: maybeFnId }) args = do
put st
if isJust maybeFnId then
pure $ resetFnScope
$ substituteFnArgs result (map fst params `zip'` args)
$ substituteFnArgs result (map fst params `Array.zip` args)
else
modify_ _ { scopeLoc = newScopeLoc } *> pure result

else if unappliedArgsNum > 0 then
pure $ FnObj $ FnInfo $ fnInfo
{ params = takeEnd' unappliedArgsNum params
{ params = Array.takeEnd unappliedArgsNum params
, argsMap = newSt.argsMap
}

else do
let
{ before: preArgs, after: postArgs } =
splitAt' (length params) args
Array.splitAt (length params) args
fn <- evalFn (FnInfo fnInfo) preArgs
evalExpr $ FnApply (Object' fn) postArgs

Expand All @@ -179,7 +179,7 @@ evalBuiltinFn fnInfo@{ fn, params, defaultParams } args =
else if unappliedArgsNum > 0 then
pure $ BuiltinFnObj $ fnInfo
{ fn = \newArgs -> fn (args <> newArgs)
, params = takeEnd' unappliedArgsNum params
, params = Array.takeEnd unappliedArgsNum params
, defaultParams = Set.filter zeroOrPos
$ Set.map (_ - length args) defaultParams
}
Expand Down Expand Up @@ -210,20 +210,20 @@ nestInfixFns fnOps args = do
let
indexFn =
case associativity of
L -> findIndex'
R -> findLastIndex'
L -> Array.findIndex
R -> Array.findLastIndex
idx <- indexFn
( \x -> x.associativity == associativity
&& (x.precedence == precedence)
)
fnOps
{ fnName } <- index' fnOps idx
{ fnName } <- Array.index fnOps idx
let
newFns = fold $ deleteAt' idx fnOps
redexArgs = sliceNext' 2 idx args
newArgs = fold $ deleteAt' (idx + 1)
newFns = fold $ Array.deleteAt idx fnOps
redexArgs = Array.slice idx (idx + 2) args
newArgs = fold $ Array.deleteAt (idx + 1)
$ fold
$ updateAt' idx (FnApply (FnVar fnName) redexArgs) args
$ Array.updateAt idx (FnApply (FnVar fnName) redexArgs) args
nestInfixFns newFns newArgs

evalCaseBinding
Expand Down Expand Up @@ -299,7 +299,7 @@ evalPatternBinding pattern@(ArrayPattern _) (ListObj xs) =
evalPatternBinding pattern (ArrayObj $ Array.fromFoldable xs)

evalPatternBinding (ArrayPattern patterns) result
| Just idx <- findIndex' isSpread patterns
| Just idx <- Array.findIndex isSpread patterns
, length (filter isSpread patterns) == 1
, ArrayObj results <- result =
do
Expand All @@ -311,18 +311,18 @@ evalPatternBinding (ArrayPattern patterns) result
(ArrayPattern $ patternsBegin <> patternsEnd)
(ArrayObj $ resultsBegin <> resultsEnd)
where
{ before, after } = splitAt' idx patterns
(patternsBegin /\ patternsEnd) = (before /\ fold (tail' after))
resultsBegin = take' (length patternsBegin) results
resultsBetween = slice' (length patternsBegin)
{ before, after } = Array.splitAt idx patterns
(patternsBegin /\ patternsEnd) = (before /\ fold (Array.tail after))
resultsBegin = Array.take (length patternsBegin) results
resultsBetween = Array.slice (length patternsBegin)
(length results - length patternsEnd)
results
resultsEnd = takeEnd' (length patternsEnd) results
resultsEnd = Array.takeEnd (length patternsEnd) results

evalPatternBinding (ArrayPattern patterns) result
| Just results <- extractNList (length patterns) result =
and <$> traverse (\x -> uncurry evalPatternBinding x)
(patterns `zip'` results)
(patterns `Array.zip` results)
| otherwise = pure false

evalPatternBinding Wildcard _ =
Expand Down
5 changes: 3 additions & 2 deletions src/FatPrelude.purs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ module FatPrelude
( module X
) where

import App.Utils.Array (arr2, deleteAt', drop', dropEnd', findIndex', findLastIndex', head', index', init', insertAt', intersperse', last', slice', sliceNext', splitAt', tail', take', takeEnd', toArray', uncons', unsnoc', updateAt', zip', zipWith') as X
import Data.FastVect.MinLenVect (MinLenVect, cons, drop, fromVect, fromArray, fromUnsizedArray, generate, head, index, indexModulo, last, mapWithTerm, reifyMinLenVect, replicate, set, singleton, snoc, splitAt, take, toArray, toNonEmptyArray, toVect) as X
import App.Utils.MinLenVect (fromFoldable, sort, zip, zipGT, zipLT) as X

import App.Utils.Bounded (clampBounded, enumValues, getInBoundedRange, inBoundedRange, inRange, (..)) as X
import App.Utils.Char (fromUpper, nextChar, prevChar, toUpper, upperEndCode, upperStartCode) as X
import App.Utils.Common (partialMaybe) as X
Expand All @@ -20,7 +22,6 @@ import Control.Monad.State (class MonadState, StateT(..), evalState, evalStateT,
import Control.Monad.Trans.Class (class MonadTrans, lift) as X
import Control.MonadPlus (class Alt, class Alternative, class MonadPlus, class Plus, alt, empty, guard, (<|>)) as X
import Data.Array (length) as X
import Data.Array.NonEmpty (NonEmptyArray, alterAt, appendArray, concat, concatMap, cons, cons', delete, deleteAt, deleteBy, difference, difference', drop, dropEnd, dropWhile, elemIndex, elemLastIndex, filterA, findIndex, findLastIndex, foldRecM, fromArray, group, groupAll, groupAllBy, groupBy, head, index, init, insert, insertAt, insertBy, intersect, intersect', intersectBy, intersectBy', intersperse, last, modifyAt, modifyAtIndices, nub, nubBy, nubByEq, nubEq, prependArray, replicate, reverse, singleton, slice, snoc, snoc', sort, sortBy, sortWith, span, splitAt, tail, take, takeEnd, takeWhile, toArray, transpose, transpose', uncons, union, union', unionBy, unionBy', unsafeIndex, unsnoc, unzip, updateAt, updateAtIndices, zip, zipWith, zipWithA, (!!), (\\)) as X
import Data.Bifunctor (class Bifunctor, bimap, lmap, rmap) as X
import Data.Bitraversable (class Bifoldable, class Bitraversable, biall, biany, bifold, bifoldMap, bifoldl, bifoldr, bifor, bifor_, bisequence, bisequence_, bitraverse, bitraverse_, lfor, ltraverse, rfor, rtraverse) as X
import Data.Char (fromCharCode, toCharCode) as X
Expand Down
Loading

0 comments on commit 14fef83

Please sign in to comment.