Giter VIP home page Giter VIP logo

lifted's Introduction

Functions Lifted over Functor or Applicative

Haskell Programming Language BSD3 License

Description

Functions from base, including few that aren't, lifted over Functor or Applicative.

These functions have proved useful to the author over the years. All of them have, at some point, being used in a production code.

Examples

Conversion of Error Handling

Various forms of error handling are used in Haskell libraries, and applications. Some lifted functions allow us to convert between them with ease. While the syntactic overhead is bigger then using functions customized to such tasks; these functions are much more generic and so the code can be adjusted to different error handling strategies with minimal changes.

Convert Either a b to Maybe b where Left _ is interpreted as Nothing:

fromRightA_ Nothing :: Either a b -> Maybe b

Convert the same Either a b to IO b and throw exception when Left is encountered:

fromRightA_ (throwIO MissingValueException) :: Either a b -> IO b

-- or when:
--   MissingValueException :: String -> MissingValueException
-- then:

fromRightA (throwIO . MissingValueException) :: Either String b -> IO b

Convert Maybe a in to Either String a where the String argument contains error message in case when Nothing is encountered:

fromJustA (Left "Error message.") :: Maybe a -> Either String a

Throw an exception on Nothing:

fromJustA (throwIO MissingValueException) :: Maybe a -> IO a

Predicates

Simplified predicates:

import Data.Bool.Lifted ((<||>))
import Data.Eq.Lifted (is)


isSquareBracket :: Char -> Bool
isSquareBracket = is '[' <||> is ']'    -- Same as: (== '[') <||> (== ']')

Effectful conditions:

import System.Directory (doesFileExist, executable, getPermissions)
import Data.Bool.Lifted ((<&&>))


execute :: FilePath -> IO ()
execute fp = do
    isExe <- doesFileExist fp <&&> isExecutable fp
    if isExe
        then do
            -- -->8--
        else do
            -- -->8--
  where
    isExecutable = fmap executable . getPermissions

License

The BSD 3-Clause License, see LICENSE file for details.

Contributions

Contributions, pull requests and bug reports are welcome! Please don't be afraid to contact author using GitHub or by e-mail.

lifted's People

Contributors

trskop avatar

Stargazers

 avatar

Watchers

James Cloos avatar  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.