@hackage unboxed-references0.1.0

A library for reference cells backed by unboxed-vectors

  • Installation

  • Dependencies (2)

  • Dependents (0)

This package contains an interface to references backed by unboxed vectors

For an example of how to use it:

  module Main where

  import           Data.STRef.Unboxed
  import           Control.Monad.ST

  main :: IO ()
  main =
    do
      print counter
      print mySum
      print strictModify

  -- |
  -- Runs a for loop each time modifying the reference cell to add
  -- one.
  counter :: Int
  counter =
   runST $
     do
       ref <- newSTRefU 0
       for_ [1..100] $ modifySTRefU ref (+ 1)
       readSTRefU ref
  -- |
  -- Runs a for loop to add all the numbers between 1 and 10.
  mySum :: Int
  mySum =
    runST $
      do
        ref <- newSTRefU 0
        for_ [1..10] $ \i -> modifySTRefU ref (+ i)
        readSTRefU ref
  -- |
  -- This shows that modifySTRefU is strict in its argument.
  strictModify :: Int
  strictModify =
    runST $
      do
        ref <- newSTRefU 0
        modifySTRefU ref (\_ -> Unsafe.unsafePerformIO $ print "Got here!" >> pure 10)
        writeSTRefU ref 42
         readSTRefU ref

This gives the following:

> main
100
55
"Got here!"
42