FRP-ish / Reactive programming inspired by React hooks

I’m excited to announce a pre-release of a new library called hooks:

The goal of this library is to make it easy to write programs that produces streams of values, with do notation being used to combine the latest values from multiple streams. This library is basically a port of React Hooks to Haskell, so hopefully the model should be familiar to those who’ve worked with React Hooks.

Really excited to be working with the QualifiedDo extension in this library! A quick sample of how the library works:

{-# LANGUAGE QualifiedDo #-}

-- increment a counter steadily every interval (microseconds)
useTick :: Int -> Hooks _ Int
useTick interval = Hook.do
  (tick, setTick) <- Use (State 0)

  Use $ Effect interval $ do
    thread <- async $ forever $ do
      threadDelay interval
      setTick $ Modify (+1)
    return $ cancel thread

  Hook.return tick 

-- create a variable number of counters incrementing at different speeds 
testProg :: Hooks _ [Int]
testProg = Hook.do
  (num, updateNum) <- Use $ State (0 :: Int)

  Use $ Effect () $ do -- read a number from the command line
    thread <- async . forever $ do
      x <- getLine
      updateNum . Set $ read x
    return $ cancel thread

  Use $ Map () [1..num] $ \i -> Hook.do
    (randomOffset, updateRandomOffset) <- Use $ State 0
    Use $ Effect () $ do
      randomValue <- randomRIO (-1000000, 1000000)
      updateRandomOffset $ Set randomValue
      return $ return ()
    useTick (1000000 + randomOffset)

Any feedback is much appreciated :smiley:

4 Likes