Giter Site home page Giter Site logo

t-util's Introduction

t-util

Practical "monad" patterns for clojure development to ease error handling especially when dealing with I/O or other resources that usually throw exceptions to do the same.

Note that this library is also a test bed for ideas and that's why there are namespaces like interpret.interpreter etc.

Public namespaces are kept stable.

Research

Public namespaces

monads.api

Error or Success

A function call many times need to communicate error or success, this pattern is normally described using an interpreter as an example
but can also be used for:

  • File read write
  • Network I/O
  • JDBC calls
  • Lock acquire
  • Condition checking like validation

The standard way is by throwing an exception, but we can also have the function return a type that shows Success or Error.
For the latter this allows the calling function to react differently on error and success in a more direct way (my opinion)

(require '[monads.api :as mapi])

(defn read-io [x] (if x (mapi/successM :true) (mapi/errorM "This is an error")))

(read-io nil)
;;#monads.api.MError{:v "This is an error"}

(read-io 1)
;;#monads.api.Success{:v :true}

(mapi/error? (read-io nil))
;; true
(mapi/success? (read-io 1))
;;true
 
;; helper macros these make is easier to work with success error situations

;; on-success-else fn variable-binding commands-if-success commands-if-error
(mapi/on-success-else (read-io 1) x (prn "read value" x) (prn "failed to read due to " x)) 
;;:true
(mapi/on-success-else (read-io nil) x (prn "read value" x) (prn (str "failed " x))) 
;;"failed This is an error"

;;value of a function
(mapi/showM (read-io 1))
;; :true 

License

Copyright © 2015 FIXME

Distributed under the Eclipse Public License either version 1.0

t-util's People

Contributors

gerritjvv avatar

Watchers

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