Skip to content

Commit

Permalink
Data.NList
Browse files Browse the repository at this point in the history
  • Loading branch information
zliu41 committed Feb 19, 2020
1 parent f9d823b commit c649363
Show file tree
Hide file tree
Showing 11 changed files with 638 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Revision history for indexed-containers

## 0.1.0.0 -- 2020-02-18

* Initial release.
4 changes: 4 additions & 0 deletions Main.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module Main where

main :: IO ()
main = putStrLn "Hello, Haskell!"
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
# indexed-containers
# indexed-containers: simple, no-frills indexed lists.

This library contains lists whose types are indexed by their lengths. The implementation is a simple
wrapper around a regular list.

If the lengths of your lists are known statically, using indexed lists improves type safety
with no runtime overhead.
2 changes: 2 additions & 0 deletions Setup.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import Distribution.Simple
main = defaultMain
52 changes: 52 additions & 0 deletions TestGen.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/env stack
-- stack --resolver lts-15.0 script --package filepath --package directory --package extra
-- To run: ./TestGen.hs

module TestGen (main) where

import Data.List.Extra (replace, stripPrefix, trim)
import Data.Maybe (mapMaybe)
import System.Directory
import System.FilePath

import Prelude hiding (mod)

main :: IO ()
main = genTestsFor "Data.NList"

genTestsFor :: String -> IO ()
genTestsFor mod = do
let inputFile = "src" </> replace "." [pathSeparator] mod <.> "hs"
outputFile = "test/hspec" </> (replace "." [pathSeparator] mod ++ "Spec.hs")
src <- readFile inputFile
createDirectoryIfMissing True (takeDirectory outputFile)
let lns = fmap trim (lines src)
tests = mapMaybe (stripPrefix "-- > ") lns
writeFile outputFile . unlines $ header mod ++ fmap (indent 6) tests

header :: String -> [String]
header mod =
[ "-- Generated code, do not modify by hand. Generate by running TestGen.hs."
, ""
, "{-# LANGUAGE DataKinds #-}"
, "{-# LANGUAGE TypeApplications #-}"
, "{-# LANGUAGE TypeFamilies #-}"
, "{-# OPTIONS_GHC -w #-}"
, "module " ++ mod ++ "Spec where"
, ""
, "import Test.Hspec"
, "import Prelude hiding (concat, drop, head, init, last, length, replicate, reverse, splitAt, tail, take, unzip, zip, zipWith)"
] ++ ["import " ++ mod] ++
[ ""
, "infix 4 ==="
, "(===) :: (HasCallStack, Show a, Eq a) => a -> a -> Expectation"
, "(===) = shouldBe"
, ""
, "spec :: Spec"
, "spec = do"
, " describe \"Testing " ++ mod ++ "\" $ do"
, " it \"\" $ do"
]

indent :: Int -> String -> String
indent n = (replicate n ' ' ++)
30 changes: 30 additions & 0 deletions default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{ nixpkgs ? import <nixpkgs> {}, compiler ? "default", doBenchmark ? false }:

let

inherit (nixpkgs) pkgs;

f = { mkDerivation, base, hspec, hspec-discover, stdenv }:
mkDerivation {
pname = "indexed-containers";
version = "0.1.0.0";
src = ./.;
libraryHaskellDepends = [ base ];
testHaskellDepends = [ base hspec ];
testToolDepends = [ hspec-discover ];
homepage = "https://github.com/zliu41/indexed-containers#readme";
description = "Simple, no-frills indexed lists";
license = stdenv.lib.licenses.bsd3;
};

haskellPackages = if compiler == "default"
then pkgs.haskellPackages
else pkgs.haskell.packages.${compiler};

variant = if doBenchmark then pkgs.haskell.lib.doBenchmark else pkgs.lib.id;

drv = variant (haskellPackages.callPackage f {});

in

if pkgs.lib.inNixShell then drv.env else drv
45 changes: 45 additions & 0 deletions indexed-containers.cabal
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
cabal-version: 2.4
-- Initial package description 'indexed-containers.cabal' generated by
-- 'cabal init'. For further documentation, see
-- http://haskell.org/cabal/users-guide/

name: indexed-containers
version: 0.1.0.0
synopsis: Simple, no-frills indexed lists.
description: If the lengths of your lists are known statically, using indexed lists improves type safety with no runtime overhead.
category: Data Structures
homepage: https://github.com/zliu41/indexed-containers#readme
bug-reports: https://github.com/zliu41/indexed-containers/issues
author: Ziyang Liu <[email protected]>
maintainer: Ziyang Liu <[email protected]>
copyright: 2020 Ziyang Liu
license: BSD-3-Clause
license-file: LICENSE
build-type: Simple
extra-source-files: CHANGELOG.md, README.md
tested-with: GHC==8.10.1, GHC==8.8.2, GHC==8.6.5, GHC==8.4.4

library
exposed-modules:
Data.NList
hs-source-dirs:
src
ghc-options: -Wall
build-depends:
base >=4.7 && <5
default-language: Haskell2010

test-suite hspec
type: exitcode-stdio-1.0
main-is: Main.hs
other-modules:
Data.NListSpec
hs-source-dirs:
test/hspec
ghc-options: -Wall -threaded -rtsopts -with-rtsopts=-N
build-depends:
base >=4.7 && <5
, hspec >=2.4.8 && <2.8
, indexed-containers
default-language: Haskell2010
build-tool-depends: hspec-discover:hspec-discover == 2.*
8 changes: 8 additions & 0 deletions release.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{ compiler ? "default" }:

let
pkgs = import <nixpkgs> { };

in
{ indexed-containers = pkgs.haskellPackages.callPackage ./default.nix { inherit compiler; };
}
Loading

0 comments on commit c649363

Please sign in to comment.