Coder Social home page Coder Social logo

supervis.es's Introduction

supervis.es

This is some example code that demonstrates extending the sequence method that is common to functional programming to absorb some monadic abstractions.

Please use Github Issues or comment on Commits to provide feedback. All thoughts are welcome.

yak-shaving

The canonical source code is coffee-src/supervis.es.coffee. The code is tested using npm test, and a pretest script compiles the CoffeeScript to JavaScript in lib/supervis.es.js. jasmine-node then interprets the specs in the spec folder (they're also written in CoffeeScript).

To try it, you'll need node, jasmine-node, CoffeeScript, and their respective dependencies.

code organization

The key function is sequence. In naïve form, sequence appears to create a pipeline through a series of unary functions, much like functional composition in reverse:

plusOne = (n) -> n + 1
double = (n) -> n * 2

sequence(plusOne, double, double)(1)
  #=> 8

If the first argument to sequence is an instance of Supervisor, then sequence will use the Supervisor to wrap and unwrap values along the way. Here is the above example using the Writer monad:

plusOne = (n) -> [n + 1, "(#{n}) -> #{n + 1}; "]
double = (n) -> [n * 2, "(#{n}) -> #{n * 2}; "]

sequence(Supervisor.Writer, plusOne, double, double)(1)
  #=> [ 8, '(1) -> 2; (2) -> 4; (4) -> 8; ' ]

If the first argument isn't a Supervisor instance, sequence actually uses the Identity monad behind the scenes.

At this time, I've coded up:

  1. Identity
  2. Maybe
  3. Writer
  4. List
  5. Promise
  6. Callback

The Promise monad wraps a ridiculously simple Promise implementation. Errors fall through much like the Maybe monad. Callback is an implementation of Haskell's infamous "Continuation" monad: You can sequence asynchronous callback functions without ugly nesting.

feedback wanted

Yes please!

Thanks!

supervis.es's People

Contributors

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