These are chat archives for ramda/ramda

16th
Dec 2014
David Chambers
@davidchambers
Dec 16 2014 00:02
Something I whipped together myself. It's trivial, though:
'use strict';

var util = require('util');

var R = require('ramda');


/* istanbul ignore next */
function Maybe() {}


var Nothing = exports.Nothing = function Nothing() {
  if (!(this instanceof Nothing)) {
    return new Nothing();
  }
};

util.inherits(Nothing, Maybe);

Nothing.prototype.equals = R.is(Nothing);

Nothing.prototype.map = function(f) {
  void f;
  return this;
};


var Just = exports.Just = function Just(value) {
  if (!(this instanceof Just)) {
    return new Just(value);
  } else {
    this.value = value;
  }
};

util.inherits(Just, Maybe);

Just.prototype.equals = function(x) {
  return R.is(Just, x) && x.value === this.value;
};

Just.prototype.map = function(f) {
  return new Just(f(this.value));
};


// fromMaybe :: a -> Maybe a -> a
exports.fromMaybe = R.curry(function(default_value, maybe) {
  return R.cond(
    [R.is(Just), R.prop('value')],
    [R.is(Nothing), R.always(default_value)]
  )(maybe);
});
Michael Hurley
@buzzdecafe
Dec 16 2014 00:03
lgtm
just a functor so far. planning to add ap, of, chain, etc.?
David Chambers
@davidchambers
Dec 16 2014 00:05
At the moment this is an internal module in one of my projects. If I need more features I'll look at switching to ramda/ramda-fantasy or another open-source maybe implementation.
Michael Hurley
@buzzdecafe
Dec 16 2014 00:09
eventually i'll turn my attention back to ramda-fantasy.
for now, getting closer with transducers. you, scott and i will have to discuss the API
Michael Hurley
@buzzdecafe
Dec 16 2014 00:18
btw, you don't need an else in your Just constructor :stuck_out_tongue:
David Chambers
@davidchambers
Dec 16 2014 01:19
I don't like to use return for control flow.
David Chambers
@davidchambers
Dec 16 2014 01:58
How does one handle mapping over a maybe when the result of the applying the function to the unwrapped value is itself a maybe? In my code this is producing a double-wrapped value (a maybe inside a maybe).
David Chambers
@davidchambers
Dec 16 2014 02:32
>>= (bind) is the function I sought!
Michael Hurley
@buzzdecafe
Dec 16 2014 11:39
congratulations it's a monad
Jethro Larson
@jethrolarson
Dec 16 2014 22:21
Realized that yeah, I'm just reimplementing react.js with methods for querying the tree