Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ example ] Sorted tree examples improvements #195

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions examples/sorted-tree-indexed/src/Data/SortedBinTree.idr
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module Data.SortedBinTree

import public Data.Nat
import Data.String

%default total

Expand All @@ -13,3 +14,17 @@ export
toList : SortedBinTree1 mi ma -> List Nat
toList (Leaf x) = [x]
toList (Node left right) = toList left ++ toList right

export
Interpolation (SortedBinTree1 mi ma) where
interpolate $ Leaf x = "\{show x}"
interpolate $ Node l r = """
.
\{ind "|" $ interpolate l}
\{ind " " $ interpolate r}
"""
where
ind : (pref : String) -> String -> String
ind k s = do
let f::fs = lines s | [] => ""
joinBy "\n" $ "|" :: ("|- " ++ f) :: (("\{k} " ++) <$> fs)
20 changes: 20 additions & 0 deletions examples/sorted-tree-indexed/tests/gens/sorted-big/Main.idr
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import Data.SortedBinTree.Gen
import Data.Fuel
import Data.List
import Data.List.Lazy
import Data.Primitives.Interpolation

import System.Random.Pure.StdGen

%default total

main : IO ()
main = do
let vals = unGenTryN 10 someStdGen $ genSortedBinTree1 $ limit 5
Lazy.for_ vals $ \(mi ** ma ** tree) => do
putStrLn "--------------"
let list = toList tree
putStrLn "min: \{mi}, max: \{ma}, length: \{length list}"
putStrLn "tree:\n\{tree}"
putStrLn "as list: \{show list}"
putStrLn "sorted: \{show $ sorted list}"
196 changes: 196 additions & 0 deletions examples/sorted-tree-indexed/tests/gens/sorted-big/expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
--------------
min: 0, max: 9, length: 5
tree:
.
|
|- 0
|
|- .
|
|- .
| |
| |- .
| | |
| | |- 1
| | |
| | |- 2
| |
| |- 3
|
|- 9
as list: [0, 1, 2, 3, 9]
sorted: True
--------------
min: 3, max: 7, length: 3
tree:
.
|
|- .
| |
| |- 3
| |
| |- 4
|
|- 7
as list: [3, 4, 7]
sorted: True
--------------
min: 0, max: 6, length: 4
tree:
.
|
|- .
| |
| |- .
| | |
| | |- 0
| | |
| | |- 1
| |
| |- 2
|
|- 6
as list: [0, 1, 2, 6]
sorted: True
--------------
min: 3, max: 6, length: 3
tree:
.
|
|- .
| |
| |- 3
| |
| |- 4
|
|- 6
as list: [3, 4, 6]
sorted: True
--------------
min: 0, max: 7, length: 5
tree:
.
|
|- .
| |
| |- 0
| |
| |- 1
|
|- .
|
|- .
| |
| |- 3
| |
| |- 4
|
|- 7
as list: [0, 1, 3, 4, 7]
sorted: True
--------------
min: 0, max: 9, length: 6
tree:
.
|
|- .
| |
| |- .
| | |
| | |- 0
| | |
| | |- 1
| |
| |- .
| |
| |- .
| | |
| | |- 2
| | |
| | |- 3
| |
| |- 4
|
|- 9
as list: [0, 1, 2, 3, 4, 9]
sorted: True
--------------
min: 0, max: 6, length: 4
tree:
.
|
|- .
| |
| |- 0
| |
| |- 1
|
|- .
|
|- 2
|
|- 6
as list: [0, 1, 2, 6]
sorted: True
--------------
min: 0, max: 9, length: 5
tree:
.
|
|- .
| |
| |- .
| | |
| | |- .
| | | |
| | | |- 0
| | | |
| | | |- 1
| | |
| | |- 2
| |
| |- 4
|
|- 9
as list: [0, 1, 2, 4, 9]
sorted: True
--------------
min: 0, max: 7, length: 4
tree:
.
|
|- .
| |
| |- .
| | |
| | |- 0
| | |
| | |- 1
| |
| |- 4
|
|- 7
as list: [0, 1, 4, 7]
sorted: True
--------------
min: 0, max: 9, length: 5
tree:
.
|
|- .
| |
| |- .
| | |
| | |- .
| | | |
| | | |- 0
| | | |
| | | |- 1
| | |
| | |- 2
| |
| |- 4
|
|- 9
as list: [0, 1, 2, 4, 9]
sorted: True
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ main = do
putStrLn "--------------"
let list = toList tree
putStrLn "min: \{mi}, max: \{ma}, length: \{length list}"
putStrLn "tree:\n\{tree}"
putStrLn "as list: \{show list}"
putStrLn "sorted: \{show $ sorted list}"
Original file line number Diff line number Diff line change
@@ -1,40 +1,148 @@
--------------
min: 0, max: 7, length: 5
tree:
.
|
|- 0
|
|- .
|
|- .
| |
| |- 1
| |
| |- 2
|
|- .
|
|- 3
|
|- 7
as list: [0, 1, 2, 3, 7]
sorted: True
--------------
min: 1, max: 6, length: 3
tree:
.
|
|- .
| |
| |- 1
| |
| |- 2
|
|- 6
as list: [1, 2, 6]
sorted: True
--------------
min: 0, max: 7, length: 4
tree:
.
|
|- .
| |
| |- 0
| |
| |- .
| |
| |- 2
| |
| |- 3
|
|- 7
as list: [0, 2, 3, 7]
sorted: True
--------------
min: 0, max: 5, length: 5
tree:
.
|
|- .
| |
| |- .
| | |
| | |- .
| | | |
| | | |- 0
| | | |
| | | |- 1
| | |
| | |- 2
| |
| |- 3
|
|- 5
as list: [0, 1, 2, 3, 5]
sorted: True
--------------
min: 1, max: 5, length: 4
tree:
.
|
|- .
| |
| |- .
| | |
| | |- 1
| | |
| | |- 2
| |
| |- 3
|
|- 5
as list: [1, 2, 3, 5]
sorted: True
--------------
min: 0, max: 8, length: 3
tree:
.
|
|- 0
|
|- .
|
|- 3
|
|- 8
as list: [0, 3, 8]
sorted: True
--------------
min: 2, max: 2, length: 1
tree:
2
as list: [2]
sorted: True
--------------
min: 2, max: 7, length: 3
tree:
.
|
|- .
| |
| |- 2
| |
| |- 3
|
|- 7
as list: [2, 3, 7]
sorted: True
--------------
min: 0, max: 4, length: 3
tree:
.
|
|- 0
|
|- .
|
|- 3
|
|- 4
as list: [0, 3, 4]
sorted: True
--------------
min: 2, max: 2, length: 1
tree:
2
as list: [2]
sorted: True
Loading