Skip to content

Commit 2bc0a58

Browse files
author
Marco Zocca
committed
add IsString instance for Document and update Readme
1 parent 1e3465f commit 2bc0a58

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

README.md

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,24 @@ VP-trees are a space-partitioning data structure that significantly accelerates
3333
```haskell
3434
{-# LANGUAGE OverloadedStrings #-}
3535

36-
import qualified Data.ByteString.Lazy.Char8 as BL
37-
import Data.NCDTree
36+
from Data.NCDTree import Document, mkVPTree, knnSearch
3837

39-
-- Create documents from bytestrings
40-
let docs = [ Document (BL.pack "hello world")
41-
, Document (BL.pack "hello universe")
42-
, Document (BL.pack "goodbye world")
43-
, Document (BL.pack "hello there")
44-
]
38+
-- Create documents using string literals (thanks to IsString instance)
39+
docs :: [Document]
40+
docs = [ "hello world"
41+
, "hello universe"
42+
, "goodbye world"
43+
, "hello there"
44+
]
4545

4646
-- Build the VP-tree index with a leaf threshold of 4
47-
let tree = mkVPTree 4 docs
47+
tree = mkVPTree 4 docs
4848

4949
-- Search for the 2 nearest neighbors of "hello"
50-
let query = Document (BL.pack "hello")
51-
let results = knnSearch 2 query tree
50+
query :: Document
51+
query = "hello"
52+
53+
results = knnSearch 2 query tree
5254

5355
-- results is a max-heap with the closest documents
5456
```

src/Data/NCDTree.hs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ module Data.NCDTree
1010
) where
1111

1212
import Control.Monad.ST
13+
import Data.String (IsString(..))
1314
import Data.Ord (Down(..), comparing)
1415

1516
import qualified Data.ByteString.Lazy as BL
17+
import qualified Data.ByteString.Lazy.Char8 as BLC
1618

1719
import qualified Data.Heap as H
1820

@@ -26,6 +28,9 @@ newtype Document = Document
2628
{ docText :: BL.ByteString
2729
} deriving (Eq, Show)
2830

31+
instance IsString Document where
32+
fromString = Document . BLC.pack
33+
2934
data VPTree = VPNode { pivot :: Document
3035
, threshold :: Double
3136
, inside :: VPTree

0 commit comments

Comments
 (0)