Git Product home page Git Product logo

realworld's Introduction

realworld

Introduction

A simple levity polymorphic State#-token-passing monad for writing state-token-passing code in a more idiomatic โ€“ monadic โ€“ style.
Currently only supports the basic monadic operations (fmap, pure, >>=).

Provides drop-in replacements for GHC.Exts and ghc-bignum.

Crudely bypasses levity polymorphism restrictions using typeclasses.

Quick start

Here's a simple before/after example (converting a Natural to base 256 representation stored in a ByteArray):

import qualified GHC.Exts          as GHC
import qualified GHC.Num.Natural   as GHC
import qualified GHC.Num.WordArray as GHC

intoBytes_ghc :: Int -> Natural -> ByteArray#
intoBytes_ghc (I# nbBytes) i = GHC.runRW# \ s1 ->
  case GHC.newByteArray# nbBytes s1 of
    (# s2, mba #) -> case GHC.naturalToMutableByteArray# i mba 0## 0# s2 of
      (# s3, word2Int# -> bytesWritten #) ->
        case GHC.setByteArray# mba bytesWritten ( nbBytes -# bytesWritten ) 0# s3 of
          s4 -> case GHC.unsafeFreezeByteArray# mba s4 of
            (# _, ba #) -> ba
import qualified RealWorld.Monad       as RW -- typeclasses providing monadic operations
import qualified RealWorld.Monad.State as RW -- StateS# monad
import qualified RealWorld.GHC         as RW -- drop-in replacement for GHC.Exts
import qualified RealWorld.GHC.BigNum  as RW -- drop-in replacement for ghc-bignum

intoBytes_rw :: Int -> Natural -> ByteArray#
intoBytes_rw (I# nbBytes) i = RW.runRWS# RW.do
  mba <- RW.newByteArray# nbBytes
  ( word2Int# -> bytesWritten ) <- RW.naturalToMutableByteArray# i mba 0## 0#
  RW.setByteArray# mba bytesWritten ( nbBytes -# bytesWritten ) 0#
  RW.unsafeFreezeByteArray# mba

realworld's People

Contributors

sheaf avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.