Skip to content

Commit

Permalink
Add explicit dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
JoelLefkowitz committed Jun 25, 2024
1 parent 541093a commit 5f1a7f0
Show file tree
Hide file tree
Showing 23 changed files with 68 additions and 115 deletions.
48 changes: 0 additions & 48 deletions .github/workflows/docs.yml

This file was deleted.

2 changes: 1 addition & 1 deletion .github/workflows/review.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
uses: ./.github/actions/install

- name: Run linters
run: npm run lint
run: yarn lint

- name: Run tests
run: yarn spago test
2 changes: 1 addition & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
npm run lint
yarn lint
yarn spago test
2 changes: 2 additions & 0 deletions .trufflehog3.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ exclude:

- message: Tooling outputs
paths:
- .nyc_output
- generated-docs
- output
- package-lock.json
- spago.lock
- yarn-error.log
- yarn.lock
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ spago install huffman

## Documentation

Documentation and more detailed examples are hosted on [Github Pages](https://joellefkowitz.github.io/huffman).
Documentation and more detailed examples are hosted on [Pursuit](https://pursuit.purescript.org/packages/purescript-huffman).

## Usage

```purs
import Huffman (composeCodewords, encodeWith, decodeWith)
import Data.Huffman.Codewords (composeCodewords)
import Data.Huffman.Encode (decodeWith, encodeWith)
str = "A strongly-typed functional programming language that compiles to JavaScript"
```
Expand Down Expand Up @@ -102,15 +103,15 @@ yarn spago docs
To run linters:

```bash
npm run lint
yarn lint
```

### Formatters

To run formatters:

```bash
npm run format
yarn format
```

## Contributing
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"packageManager": "[email protected]",
"scripts": {
"postinstall": "husky",
"format": "prettier . --write && echo src test | xargs -n1 npx purty --write",
"format": "prettier . --write && echo src test | xargs -n1 purty --write",
"lint": "cspell . --dot --gitignore && eslint . --fix --no-error-on-unmatched-pattern && trufflehog3"
},
"resolutions": {
Expand Down
5 changes: 5 additions & 0 deletions spago.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ workspace:
huffman:
path: ./
dependencies:
- arrays: ">=5.0.0 <6.0.0"
- foldable-traversable: ">=5.0.1 <6.0.0"
- integers: ">=5.0.0 <6.0.0"
- lists: ">=5.0.0 <6.0.0"
- maybe: ">=5.0.0 <6.0.0"
- ordered-collections: ">=2.0.2 <3.0.0"
- prelude: ">=5.0.1 <6.0.0"
- repr: ">=0.4.0 <1.0.0"
- strings: ">=5.0.0 <6.0.0"
- stringutils: ">=0.0.11 <1.0.0"
- tuples: ">=5.0.0 <6.0.0"
test_dependencies:
- assert
- debug
Expand Down
18 changes: 11 additions & 7 deletions spago.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,21 @@ package:
githubRepo: huffman

dependencies:
- arrays: ">=5.0.0 <6.0.0"
- foldable-traversable: ">=5.0.1 <6.0.0"
- integers: ">=5.0.0 <6.0.0"
- lists: ">=5.0.0 <6.0.0"
- maybe: ">=5.0.0 <6.0.0"
- ordered-collections: ">=2.0.2 <3.0.0"
- prelude: ">=5.0.1 <6.0.0"
- repr: ">=0.4.0 <1.0.0"
- strings: ">=5.0.0 <6.0.0"
- stringutils: ">=0.0.11 <1.0.0"
- tuples: ">=5.0.0 <6.0.0"

build:
pedanticPackages: true
strict: true

test:
main: Test.Main
Expand All @@ -30,11 +39,6 @@ package:
- debug
- effect

bundle:
extra_args:
- --minify
- --outfile=dist/bundle.js

metadata:
publisher: Pursuit
languages:
Expand All @@ -47,10 +51,10 @@ metadata:
lifecycle: Alpha

workspace:
package_set:
packageSet:
registry: 41.2.0

extra_packages:
extraPackages:
repr:
git: https://github.com/joellefkowitz/repr.git
ref: 98ac605cebeba1c5082009d527e14daab7fdefd2
8 changes: 4 additions & 4 deletions src/Data/Huffman/Alphabet.purs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import Prelude
import Data.Set (Set, fromFoldable)
import Data.String (Pattern(..), split)
import Data.String.Unsafe (char)
import Data.Huffman.Symbol (Symbol(..))
import Data.Huffman.Letter (Letter(..))
import Data.String.Repr (joinWith)

newtype Alphabet
= Alphabet (Set Symbol)
= Alphabet (Set Letter)

instance eqAlphabet :: Eq Alphabet where
eq (Alphabet x) (Alphabet y) = x == y
Expand All @@ -17,6 +17,6 @@ instance showAlphabet :: Show Alphabet where
show (Alphabet set) = "[" <> joinWith ", " set <> "]"

fromString :: String -> Alphabet
fromString str = Alphabet (fromFoldable symbols)
fromString s = Alphabet (fromFoldable symbols)
where
symbols = Symbol <<< char <$> split (Pattern "") str
symbols = Symbol <<< char <$> split (Pattern "") s
7 changes: 3 additions & 4 deletions src/Data/Huffman/Codewords.purs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import Prelude
import Data.Array (concat)
import Data.Foldable (foldl)
import Data.Huffman.Occurrences (countOccurrences)
import Data.Huffman.Symbol (toString)
import Data.Huffman.Symbol as Symbol
import Data.Huffman.Letter (toString, Letter)
import Data.Huffman.Tree (HuffmanTree(..), fromWeights)
import Data.Huffman.Weights (fromOccurrences)
import Data.List (zip)
Expand All @@ -16,7 +15,7 @@ import Data.Tuple (Tuple(..))
foreign import utf16ToBinary :: String -> String

newtype Codewords
= Codewords (Map Symbol.Symbol String)
= Codewords (Map Letter String)

instance eqCodewords :: Eq Codewords where
eq (Codewords x) (Codewords y) = eq x y
Expand Down Expand Up @@ -51,7 +50,7 @@ fromHuffmanTree (Node arr w) = Codewords <<< fromFoldable $ codewords
where
codewords = collapseTree (Node arr w) ""

collapseTree :: HuffmanTree -> String -> Array (Tuple Symbol.Symbol String)
collapseTree :: HuffmanTree -> String -> Array (Tuple Letter String)
collapseTree (Leaf s _) code = [ Tuple s code ]

collapseTree (Node [ x, y ] _) code = concat [ xx, yy ]
Expand Down
14 changes: 7 additions & 7 deletions src/Data/Huffman/Encode.purs
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,25 @@ import Data.String.Pattern (Pattern(..))
import Data.String.Unsafe (char)
import Data.String.Utils (startsWith)
import Data.Huffman.Codewords (Codewords(..))
import Data.Huffman.Symbol (Symbol(..), toString)
import Data.Huffman.Letter (Letter(..), toString)
import Data.String.Repr (trimStart)

encodeWith :: String -> Codewords -> String
encodeWith str (Codewords codewords) = foldl replace "" symbols
encodeWith s (Codewords codewords) = foldl replace "" symbols
where
symbols = Symbol <<< char <$> split (Pattern "") str
symbols = Symbol <<< char <$> split (Pattern "") s

replace acc x = acc <> (fromMaybe "" (lookup x codewords))

decodeWith :: String -> Codewords -> String
decodeWith str (Codewords codewords)
| length str == 0 = ""
decodeWith s (Codewords codewords)
| length s == 0 = ""
| otherwise = toString symbol <> decodeWith remaining (Codewords codewords)
where
match = filter (\i -> startsWith i str) codewords
match = filter (\i -> startsWith i s) codewords

symbol = fromMaybe (Symbol '_') <<< head <<< toUnfoldable $ keys match

codeword = fromMaybe "" $ lookup symbol match

remaining = trimStart codeword str
remaining = trimStart codeword s
7 changes: 0 additions & 7 deletions src/Data/Huffman/Huffman.purs

This file was deleted.

12 changes: 6 additions & 6 deletions src/Data/Huffman/Symbol.purs → src/Data/Huffman/Letter.purs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
module Data.Huffman.Symbol where
module Data.Huffman.Letter where

import Prelude
import Data.String.CodeUnits (singleton)

newtype Symbol
newtype Letter
= Symbol Char

instance eqSymbol :: Eq Symbol where
instance eqLetter :: Eq Letter where
eq (Symbol x) (Symbol y) = eq x y

instance ordSymbol :: Ord Symbol where
instance ordLetter :: Ord Letter where
compare (Symbol x) (Symbol y) = compare x y

instance showSymbol :: Show Symbol where
instance showLetter :: Show Letter where
show (Symbol x) = singleton x

toString :: Symbol -> String
toString :: Letter -> String
toString (Symbol s) = singleton s
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import Data.Maybe (fromMaybe)
import Data.String.Common (split)
import Data.String.Pattern (Pattern(..))
import Data.String.Unsafe (char)
import Data.Huffman.Symbol (Symbol(..))
import Data.Huffman.Letter (Letter(..))

newtype Occurrences
= Occurrences (Map Symbol Int)
= Occurrences (Map Letter Int)

instance eqOccurrences :: Eq Occurrences where
eq (Occurrences x) (Occurrences y) = eq x y
Expand All @@ -19,9 +19,9 @@ instance showOccurrences :: Show Occurrences where
show (Occurrences x) = show x

countOccurrences :: String -> Occurrences
countOccurrences str = Occurrences $ foldl increment empty symbols
countOccurrences s = Occurrences $ foldl increment empty symbols
where
symbols = Symbol <<< char <$> split (Pattern "") str
symbols = Symbol <<< char <$> split (Pattern "") s

increment acc x = insert x (lookupInt x acc + 1) acc

Expand Down
8 changes: 4 additions & 4 deletions src/Data/Huffman/Tree.purs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import Prelude
import Data.Array (foldl, insert, length, slice, sort)
import Data.Map.Internal (toUnfoldable)
import Data.Tuple (Tuple(..))
import Data.Huffman.Symbol (Symbol)
import Data.Huffman.Letter (Letter)
import Data.Huffman.Weights (Weights(..))
import Data.String.Repr (joinWith)

data HuffmanTree
= Node (Array (HuffmanTree)) Number
| Leaf Symbol Number
| Leaf Letter Number

instance eqHuffmanTree :: Eq HuffmanTree where
eq (Node arr1 w1) (Node arr2 w2) = eq arr1 arr2 && eq w1 w2
Expand All @@ -25,9 +25,9 @@ instance showHuffmanTree :: Show HuffmanTree where
show (Leaf s w) = show s <> ": " <> show w

weight :: HuffmanTree -> Number
weight (Node arr w) = w
weight (Node _ w) = w

weight (Leaf s w) = w
weight (Leaf _ w) = w

sum :: Array HuffmanTree -> Number
sum arr = foldl (\acc x -> acc + weight x) 0.0 arr
Expand Down
4 changes: 2 additions & 2 deletions src/Data/Huffman/Weights.purs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import Data.Foldable (sum)
import Data.Int (toNumber)
import Data.Map (Map, values)
import Data.Huffman.Occurrences (Occurrences(..))
import Data.Huffman.Symbol (Symbol)
import Data.Huffman.Letter (Letter)

newtype Weights
= Weights (Map Symbol Number)
= Weights (Map Letter Number)

instance eqWeights :: Eq Weights where
eq (Weights x) (Weights y) = eq x y
Expand Down
2 changes: 1 addition & 1 deletion test/Data/Huffman/Alphabet.purs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Prelude
import Data.Set (fromFoldable)
import Effect (Effect)
import Data.Huffman.Alphabet (Alphabet(..), fromString)
import Data.Huffman.Symbol (Symbol(..))
import Data.Huffman.Letter (Letter(..))
import Test.Assert (assertEqual)

testAlphabet :: Effect Unit
Expand Down
2 changes: 1 addition & 1 deletion test/Data/Huffman/Codewords.purs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Data.Map (empty, fromFoldable, singleton)
import Data.Tuple (Tuple(..))
import Effect (Effect)
import Data.Huffman.Codewords (Codewords(..), composeCodewords)
import Data.Huffman.Symbol (Symbol(..))
import Data.Huffman.Letter (Letter(..))
import Test.Assert (assertEqual)

testCodewords :: Effect Unit
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
module Test.Data.Huffman.Symbol where
module Test.Data.Huffman.Letter where

import Prelude
import Effect (Effect)
import Data.Huffman.Symbol (Symbol(..))
import Data.Huffman.Letter (Letter(..))
import Test.Assert (assertEqual)

testSymbol :: Effect Unit
testSymbol = do
testLetter :: Effect Unit
testLetter = do
assertEqual
{ actual: show $ Symbol 'a'
, expected: "a"
Expand Down
Loading

0 comments on commit 5f1a7f0

Please sign in to comment.