5 Replies - 972 Views - Last Post: 12 February 2013 - 10:54 AM

#1 StumpyMge  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 17-May 04

Am I wrong in preferring the error monad over machina.js?

Posted 11 February 2013 - 11:08 AM

So I wrote the error monad up the other day as well as either's monad plus instance using success/failure instead of right/left.

Ok so if you don't know any haskell you have absolutely no idea what I'm talking about, but bear with me. I wrote up a javascript library for fun and then a week later a colleague showed me machina.js which infuriated me due to the approach it suggests, but maybe it's just me. Maybe I'm nuts.

So looking for opinions from other experienced javascript folks if my approach is truly bonkers.

My library provides "then", "or", and "until" functions which chain as well as "success" and "failure" functions which are to be used for the return value in the functions you chain.

Go take a look at machina.js's first example over here (scroll down a touch you'll see it):
https://github.com/i...else/machina.js

Now look at how you would implement the identical behaviour using my library's functions which I mention above, and tell me which is clearer and if I'm nuts in my approach. Is my library actually useful considering machina.js is apparently seen as such (I did mine for fun presuming it useless to the community).

var isOnline = function(a) {
    var online = true;
    // checks window.navigator.online and more, sets the online value
    return online ? success(a) : failure(a);
};

var setOfflineTime = function(a) {
    a.offlineMarkerTime = new Date();
    return success(a);
};

var saveBatchToRemote = function(a) {
    return storage.saveBatchToRemote(a) : success(a) ? failure(a);
};

var saveToRemote = function(a) {
    return storage.saveToRemote(a) : success(a) ? failure(a);
};

var saveToLocal = function(a) {
    return storage.saveToLocal(a) : success(a) : failure(a);
};

var getFromLocal = function(a) {
    return success(storage.getFromLocal(a.offlineMarkerTime));
    // should have validation and failure but example didn't
};

var goOnline = getFromLocal.then(isOnline).then(saveBatchToRemote);
var goOffline = setOfflineTime.then(goOnline.or(saveToLocal));    
var machine = goOnline.or(goOffline);

var currentCustomer = machine(someCustomer).a; // how it might be run



Note, the last 3/4 lines are the key part where I'm actually composing the machine itself from it's component parts which is what I believe is preferable to the approach machina.js gives which isn't compositional.

Is This A Good Question/Topic? 0
  • +

Replies To: Am I wrong in preferring the error monad over machina.js?

#2 StumpyMge  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 17-May 04

Re: Am I wrong in preferring the error monad over machina.js?

Posted 11 February 2013 - 11:20 AM

Oh and if you *do* know haskell, then you can understand: my "or" is either's mplus, "then" is kleisli composition of the error monad, and I guess "until" would be like msum I guess? Not super familiar with mplus so not certain of that.
Was This Post Helpful? 0
  • +
  • -

#3 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3550
  • View blog
  • Posts: 10,319
  • Joined: 08-June 10

Re: Am I wrong in preferring the error monad over machina.js?

Posted 11 February 2013 - 11:36 AM

View PostStumpyMge, on 11 February 2013 - 07:08 PM, said:

So looking for opinions from other experienced javascript folks if my approach is truly bonkers.

donít worry about it. many people donít even consider JS object oriented. but to use Functional Programming on JS, you must be nuts. *gg*
Was This Post Helpful? 0
  • +
  • -

#4 StumpyMge  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 17-May 04

Re: Am I wrong in preferring the error monad over machina.js?

Posted 11 February 2013 - 11:49 AM

View PostDormilich, on 11 February 2013 - 11:36 AM, said:

donít worry about it. many people donít even consider JS object oriented. but to use Functional Programming on JS, you must be nuts. *gg*


I don't understand what you're saying here? To be clear, javascript is a functional language (meant to be a LISP with java syntax), not meant to be OO, per Brendan Eich (from: http://brendaneich.com/tag/history/ ):

Quote

As Iíve often said, and as others at Netscape can confirm, I was recruited to Netscape with the promise of ďdoing SchemeĒ in the browser.

Was This Post Helpful? 0
  • +
  • -

#5 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3550
  • View blog
  • Posts: 10,319
  • Joined: 08-June 10

Re: Am I wrong in preferring the error monad over machina.js?

Posted 11 February 2013 - 11:55 AM

View PostStumpyMge, on 11 February 2013 - 07:49 PM, said:

I don't understand what you're saying here?

sarcasm (denoted by the *gg*)
Was This Post Helpful? 0
  • +
  • -

#6 StumpyMge  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 17-May 04

Re: Am I wrong in preferring the error monad over machina.js?

Posted 12 February 2013 - 10:54 AM

I actually dreamed in code last night because of this stupid Machina.js code I was trying to refactor.. that lib encourages such bad habits, the resultant code is next to impossible to figure out, so figuring out what it's supposed to do so I can structure it into a reasonable composition is killing me.

People need to stop worrying and love the monad. I'm just saying.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1