Avoid IO for tree sum

This commit is contained in:
George Thomas 2026-02-19 21:35:35 +00:00
parent 71e4ffaede
commit 20b47594c6
2 changed files with 4 additions and 3 deletions

View File

@ -21,6 +21,7 @@ import Foreign.C
import GarnetRs qualified as Raw
import GarnetRs.Safe qualified as Raw
import HsBindgen.Runtime.PtrConst
import System.IO.Unsafe
data T = T
{ a :: Bool
@ -65,5 +66,5 @@ helloShape = Raw.hello_shape . convertShape
add :: Int64 -> Int64 -> Int64
add = Raw.add
sumTree :: BTree Int64 -> IO Int64
sumTree = flip withBTree Raw.sum_tree
sumTree :: BTree Int64 -> Int64
sumTree = unsafePerformIO . flip withBTree Raw.sum_tree

View File

@ -10,4 +10,4 @@ main = do
helloShape $ Circle 3.14
helloShape $ Rectangle 10.0 5.0
putStrLn $ "3 + 4 = " <> show (add 3 4)
putStrLn . ("Tree sum: " <>) . show =<< sumTree (Fork (Fork (Leaf 1) (Fork (Leaf 2) (Leaf 3))) (Leaf 4))
putStrLn $ "Tree sum: " <> show (sumTree (Fork (Fork (Leaf 1) (Fork (Leaf 2) (Leaf 3))) (Leaf 4)))