Skip to content

Commit

Permalink
Group equal variables in the list output, closes #56
Browse files Browse the repository at this point in the history
  • Loading branch information
sharkdp committed Mar 25, 2017
1 parent 98dd538 commit b0c7f23
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "insect",
"version": "2.3.3",
"version": "2.4.0",
"description": "REPL-style scientific calculator",
"author": "David Peter <[email protected]>",
"license": "MIT",
Expand Down
1 change: 0 additions & 1 deletion src/Insect/Environment.purs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ initialEnvironment = fromFoldable
, Tuple "eps0" ε0
, Tuple "ε0" ε0
, Tuple "electronCharge" electronCharge
, Tuple "elementaryCharge" electronCharge
, Tuple "electronMass" electronMass
, Tuple "G" gravitationalConstant
, Tuple "g0" g0
Expand Down
30 changes: 20 additions & 10 deletions src/Insect/Interpreter.purs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ module Insect.Interpreter

import Prelude hiding (degree)

import Data.Array ((:), concat)
import Data.Array ((:), fromFoldable, singleton)
import Data.Bifunctor (lmap)
import Data.Either (Either(..))
import Data.Foldable (foldMap, intercalate)
import Data.List (sortBy, groupBy)
import Data.List.NonEmpty (head)
import Data.Maybe (Maybe(..))
import Data.StrMap (lookup, insert, foldMap)
import Data.Tuple (Tuple(..), fst)
import Data.String (toLower)
import Data.StrMap (lookup, insert, toList)
import Data.Tuple (Tuple(..), fst, snd)

import Quantities (Quantity, UnificationError(..), pow, scalar', qNegate, qAdd,
qDivide, qMultiply, qSubtract, quantity, toScalar', sqrt,
Expand Down Expand Up @@ -193,13 +197,19 @@ runInsect env (Command List) =
{ msg: Message Info list
, newEnv: env }
where
list = [ F.text "List of variables:", F.nl ] <> foldMap toLine env
toLine k v = concat [ [ F.nl, F.text " "
, F.ident k
, F.text " = "
],
prettyPrint v
]
envTuples = sortBy (comparing (fst <<< prettyPrint' <<< snd)) $ toList env
envGrouped = groupBy (\x y → snd x == snd y) envTuples
envSorted = sortBy (comparing (toLower <<< fst <<< head)) envGrouped
list = [ F.text "List of variables:", F.nl ] <> foldMap toLine envSorted
toLine kvPairs =
[ F.nl, F.text " " ]
<> identifiers
<> [ F.text " = " ]
<> prettyPrint val
where
identifiers = fromFoldable $ intercalate [ F.text " = " ] $
(singleton <<< F.ident <<< fst) <$> kvPairs
val = snd (head kvPairs)
runInsect env (Command Reset) =
{ msg: Message Info [F.text "Environment has been reset."]
, newEnv: initialEnvironment }
Expand Down

0 comments on commit b0c7f23

Please sign in to comment.