[Javascript] Monads

Source: Internet
Author: User
Tags cloudflare

Monads to nest computations. They is a pointed functor that adds mjoin and chain functions to combine other functors. Brian shows a number of code examples of different monads in action.

Functions: "Mjoin", "Chain"

Mjoin:

 var  setsearchinput = function (x) {return  (  #input    var  getsearchterm = function () { return  GetParam ( term   " , Location.search)}.toio ()  var  initsearchform = compose (Mjoin, map (setsearchinput), getsearchterm) Initsearchform ()  // => IO (Dom)  runio (Initsearchform ())  

"Getsearchterm" return a IO, so in "setsearchinput" we need to use map (), but itself would return another IO, so the Resul T is io (IO ()) and then we apply mjoin, the result would be "IO ()".

Chain

var chain =return  compose (mjoin, map (f))}

 var  setsearchinput = function (x) {return  (  #input    var  getsearchterm = function () { return  GetParam ( term   " , Location.search)}.toio ()  var  initsearchform = compose (chain  ( Setsearchinput), Getsearchterm) initsearchform ()  // => IO (Dom)  runio (Initsearchform ())  
var sendtoserver = HttpGet ('/upload')var uploadfromfile =  Compose (Chain (Sendtoserver), Chain (ReadFile), Askuser) uploadfromfile ('whatfile? '). Fork (LogErr, alertsuccess)

Requirejs.config ({shim: {}, Paths: {domready:'Https://cdnjs.cloudflare.com/ajax/libs/require-domReady/2.0.1/domReady.min', Ramda:'//cdnjs.cloudflare.com/ajax/libs/ramda/0.8.0/ramda.min', maybe:'Http://looprecur.com/hostedjs/v2/maybe', io:'Http://looprecur.com/hostedjs/v2/io', Future:'HTTP://LOOPRECUR.COM/HOSTEDJS/V2/DATA.FUTURE.UMD', Hcjs:'Http://looprecur.com/hostedjs/v2/hcjs'}}); Require (['Ramda',    'maybe',    'io',    ' Future',    'Hcjs',    'domready!'], function (_, maybe, io, future) {console.clear (); varRunio =Io.runio; //Exercise 1// ==========    //Use Safeget and mjoin or chain to safetly get the street nameConsole.log ("--------Start Exercise 1--------"); varSafeget =_.curry (function (x, O) {returnmaybe (o[x]);    }); varuser ={ID:2, Name:"Albert", Address: {street: {number: A, Name:'Walnut St'        }      }    };      function log (x) {Console.log (x.tostring ()); returnx; }    varEx1 = Compose (Mjoin, map (Safeget ('name'), Mjoin, Map (Safeget ('Street')), Safeget ('Address')); varEx1 = Compose (Chain (Safeget ('name')), Chain (Safeget ('Street')), Safeget ('Address')); Assertequal (Maybe ('Walnut St'), Ex1 (user)); Console.log ("Exercise 1...ok!"); //Exercise 2// ==========    //Use monads to get the href and then purely log it.Console.log ("--------Start Exercise 2--------"); varGethref =function () {returnLocation.href;    }.toio (); varPurelog =function (x) {console.log (x); returnx;    }.toio (); varEX2 =Compose (Chain (Purelog), gethref); Assertequal ("Http://run.jsbin.com/runner", Runio (EX2 (NULL))); Console.log ("Exercise 2...ok!"); //Exercise 3// ==========    //Use monads to first get the Post with Getpost () and then pass it's ID in to Getcomments ().Console.log ("--------Start Exercise 3--------"); varEx3 = Compose (Chain (Compose (getcomments, _.Get('ID')) , getpost); varEx3 = Compose (Mjoin, map (Compose (getcomments, _.Get('ID')) , Getpost) ex3 ( -). Fork (log, function (res) {assertequal (2, res.length); Console.log ("Exercise 3...ok!");    }); //HELPERS// =====================function Getpost (i) {return NewFuture (function (Rej, res) {setTimeout (function () {res () {id:i, title:'Love them futures'          }); },  -);    }); } function Getcomments (i) {return NewFuture (function (Rej, res) {setTimeout (function () {res) (["This class should is illegal","monads is like space burritos"]); },  -);    }); }  });

[Javascript] Monads

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.