varying: FRP through value streams and monadic splines.

[ control, frp, library, mit, program ] [ Propose Tags ]

Varying is a FRP library aimed at providing a simple way to describe values that change over a domain. It allows monadic, applicative and arrow notation and has convenience functions for tweening.


[Skip to Readme]

Downloads

Note: This package has metadata revisions in the cabal description newer than included in the tarball. To unpack the package including the revisions, use 'cabal get'.

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 0.1.0.0, 0.1.0.1, 0.1.0.2, 0.1.0.3, 0.1.1.0, 0.1.1.1, 0.1.1.2, 0.1.2.0, 0.1.3.0, 0.1.4.0, 0.1.5.0, 0.2.0.0, 0.3.0.0, 0.3.0.1, 0.4.0.0, 0.5.0.0, 0.5.0.2, 0.5.0.3, 0.6.0.0, 0.7.0.0, 0.7.0.1, 0.7.0.2, 0.7.0.3, 0.7.1.0, 0.7.1.1, 0.8.0.0, 0.8.1.0 (info)
Change log changelog.md
Dependencies base (>=4.6 && <4.11), time (>=1.4), transformers (>=0.3), varying [details]
License MIT
Author Schell Scivally
Maintainer efsubenovex@gmail.com
Revised Revision 1 made by HerbertValerioRiedel at 2018-10-04T21:25:31Z
Category Control, FRP
Home page https://github.com/schell/varying
Source repo head: git clone https://github.com/schell/varying.git
Uploaded by SchellScivally at 2016-12-12T05:11:18Z
Distributions LTSHaskell:0.8.1.0, NixOS:0.8.1.0, Stackage:0.8.1.0
Reverse Dependencies 2 direct, 1 indirect [details]
Executables varying-example
Downloads 15687 total (70 in the last 30 days)
Rating 2.0 (votes: 1) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs uploaded by user
Build status unknown [no reports yet]

Readme for varying-0.7.0.3

[back to package description]

varying

Hackage Build Status

This library provides automaton based value streams and sequencing useful for functional reactive programming (FRP) and locally stateful programming (LSP).

Getting started

module Main where

import Control.Varying
import Control.Applicative
import Control.Concurrent (forkIO, killThread)
import Data.Functor.Identity
import Data.Time.Clock

-- | A simple 2d point type.
data Point = Point { px :: Float
                   , py :: Float
                   } deriving (Show, Eq)

newtype Delta = Delta { unDelta :: Float }

-- An exponential tween back and forth from 0 to 50 over 1 seconds that
-- loops forever. This spline takes float values of delta time as input,
-- outputs the current x value at every step.
tweenx :: (Applicative m, Monad m) => TweenT Float Float m Float
tweenx = do
    -- Tween from 0 to 50 over 1 second
    tween_ easeOutExpo 0 50 1
    -- Chain another tween back to the starting position
    tween_ easeOutExpo 50 0 1
    -- Loop forever
    tweenx

-- A quadratic tween back and forth from 0 to 50 over 1 seconds that never
-- ends.
tweeny :: (Applicative m, Monad m) => TweenT Float Float m Float
tweeny = do
    tween_ easeOutExpo 50 0 1
    tween_ easeOutExpo 0 50 1
    tweeny

-- Our time signal counts input delta time samples.
time :: (Applicative m, Monad m) => VarT m Delta Float
time = var unDelta

-- | Our Point value that varies over time continuously in x and y.
backAndForth :: (Applicative m, Monad m) => VarT m Delta Point
backAndForth =
    -- Turn our splines into continuous output streams. We must provide
    -- a starting value since splines are not guaranteed to be defined at
    -- their edges.
    let x = tweenStream tweenx 0
        y = tweenStream tweeny 0
    in
    -- Construct a varying Point that takes time as an input.
    (Point <$> x <*> y)
        -- Stream in a time signal using the 'plug left' combinator.
        -- We could similarly use the 'plug right' (~>) function
        -- and put the time signal before the construction above. This is needed
        -- because the tween streams take time as an input.
        <~ time

main :: IO ()
main = do
    putStrLn "An example of value streams using the varying library."
    putStrLn "Enter a newline to continue, and then a newline to quit"
    _ <- getLine

    t   <- getCurrentTime
    tId <- forkIO $ loop backAndForth t

    _ <- getLine
    killThread tId

loop :: Var Delta Point -> UTCTime -> IO ()
loop v t = do
  t1 <- getCurrentTime
  -- Here we'll run in the Identity monad using a time delta provided by
  -- getCurrentTime and diffUTCTime.
  let dt = realToFrac $ diffUTCTime t1 t
      Identity (Point x y, vNext) = runVarT v $ Delta dt
      xStr = replicate (round x) ' ' ++ "x" ++ replicate (50 - round x) ' '
      yStr = replicate (round y) ' ' ++ "y" ++ replicate (50 - round y) ' '
      str  = zipWith f xStr yStr
      f 'x' 'y' = '|'
      f 'y' 'x' = '|'
      f a ' ' = a
      f ' ' b = b
      f _ _ = ' '
  putStrLn str
  loop vNext t1