Code Monkey home page Code Monkey logo

markdown-doctest's Introduction

Markdown doctest

Installation

$ cabal install markdown-doctest

Usage

$ ./markdown-doctest <filename.md>

Note: The ghc environment file should exist.

Syntax

3 type of codeblocks are parsed,

  • haskell
  • haskell ghci
  • haskell docspec

All the codeblocks share the same ghci session

Example,


# Examples

## Example 1,

Multiple `ghci` blocks share the same ghci session.

```haskell ghci
import Data.Function ((&))
import qualified Streamly.Data.Fold as Fold
import qualified Streamly.Data.Stream as Stream
import qualified Streamly.Data.Stream.Concurrent as Concur

f1 x =
    Prelude.map ($ x) [return . (+ 1), return . (+ 2)] -- List of actions
        & Stream.fromList                              -- Stream of actions
        & Concur.sequenceWith (Concur.ordered True)    -- Sequence the actions concurrently
        & Stream.fold Fold.toList                      -- Fold the resulting stream into a list
```

```haskell ghci
import Data.Maybe (fromJust)

f2 x =
  let actionStream1 = Stream.fromPure (return $ show x)
      actionStream2 = Stream.fromPure (return $ x + 1)
      actionStream3 = Stream.fromPure (return $ fromIntegral x / 2)
   in Stream.zipWith (\a (b, c) -> (a, b, c))
          (Concur.sequence actionStream1)
          (Stream.zipWith (,)
               (Concur.sequence actionStream2)
               (Concur.sequence actionStream3))
          & Stream.fold Fold.one
          & fmap fromJust
```

## Example 2,

`haskell` blocks are not run in ghci directly but loaded in as a file.  And
hence, these blocks cannot share the context and should be complete on their
own.

```haskell
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}

import Control.Monad.Catch (MonadThrow(..))
import Control.Monad.IO.Class (MonadIO(..))
import Control.Monad.Reader.Class (MonadReader(..))
import Control.Monad.Trans.Class (MonadTrans(..))
import Data.Function ((&))
import Data.Maybe (fromJust)
import Streamly.Data.Stream (Stream)
import Streamly.Data.Stream.Concurrent (MonadAsync)

import qualified Streamly.Data.Fold as Fold
import qualified Streamly.Data.Stream
import qualified Streamly.Data.Stream as Stream
import qualified Streamly.Data.Stream.Concurrent
import qualified Streamly.Data.Stream.Concurrent as Concur
import qualified Streamly.Internal.Data.Stream.TypeGen as StreamTypeGen

applyConcurrent :: MonadAsync m => Stream m (a -> b) -> Stream m a -> Stream m b
applyConcurrent = Concur.zipWith ($)

$(StreamTypeGen.mkZippingType "ZipConcurrent" "applyConcurrent" True)

f2 x =
  let actionStream1 = Stream.fromEffect (return $ show x)
      actionStream2 = Stream.fromEffect (return $ x + 1)
      actionStream3 = Stream.fromEffect (return $ fromIntegral x / 2)
   in ((,,,)
           <$> actionStream1
           <*> actionStream2
           <*> actionStream3)
          & Stream.fold Fold.one
          & fmap fromJust
```

## Example 3,

`docspec` blocks can be used to check results after execution. The `ghci` block
is a simpler version of `docspec` block.

```haskell docspec
>>> import qualified Streamly.Internal.Data.Array as Array
>>> :{
  Stream.fromList [1,2,3,4,5::Int]
& Stream.scan (Array.writeLastN 2)
& Stream.fold Fold.toList
:}
[fromList [],fromList [1],fromList [1,2],fromList [2,3],fromList [3,4],fromList [4,5]]

>>> g f2 [1,2,3,4::Int]
[("1",2,0.5),("2",3,1.0),("3",4,1.5),("4",5,2.0)]
```

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.