These are chat archives for ramda/ramda

9th
Oct 2015
Hardy Jones
@joneshf
Oct 09 2015 01:47
@jethrolarson sorry, I don't.
We can throw something together real quick to see how to turns out.
probably trying to do an infinite stream in js would be too painful for something quick hacked together
First you need the Cofree f a
function Cofree(x, xs) {
  return Object.create(null, {
    map: {
      value: (f) => Cofree(f(x), xs.map(f)),
    },
    extend: {
      value: (f) => Cofree(f(this), xs.map(co => co.extend(f))),
    },
    extract: {
      value: () => x,
    },
    toString: {
      value: () => `Cofree(${x}, ${xs.toString()})`
    },
  });
}
Then, lets say you want to use Maybe a since it's easier to work with in a strict language:
const Nothing = Object.create(null, {
  map: {
    value: (f) => Nothing,
  },
  toString: {
    value: () => `Nothing`,
  },
});

function Just(x) {
  return Object.create(null, {
    map: {
      value: (f) => Just(f(x)),
    },
    toString: {
      value: () => `Just(${x.toString()})`,
    },
  });
}
Hardy Jones
@joneshf
Oct 09 2015 01:53
Now a way to construct a Cofree f a that doesn't involve eye gouging:
const coiter = (f, init) =>
  Cofree(init, f(init).map(co => coiter(f, co)))

Finally, you just give it a function f that tells it how to react at each step:

console.log('%s', coiter(n => n < 5 ? Just(n + 1) : Nothing, 0))
//=> Cofree(0, Just(Cofree(1, Just(Cofree(2, Just(Cofree(3, Just(Cofree(4, Just(Cofree(5, Nothing)))))))))))

Then Bob's your uncle and Fanny's your aunt.

You could use Identity a instead of Maybe a, but then you'd blow the stack since js is strict.
Hardy Jones
@joneshf
Oct 09 2015 02:03

The reuse comes from changing the underlying Functor.

Identity gives you an infinite stream.
Maybe gives you a non-empty finite stream.
Either a gives you a non-empty finite stream with a final value.
[] gives you a rose tree.

etc.

Scott Sauyet
@CrossEye
Oct 09 2015 02:07
Love to be able to answer this StackOverflow question, if others find it worth casting "Reopen" votes: http://stackoverflow.com/questions/33027305
Hardy Jones
@joneshf
Oct 09 2015 02:17
That kind of stuff is why I rarely ask questions on so.
Scott Sauyet
@CrossEye
Oct 09 2015 02:27
Well I already edited the Q a bit. It was a bit more open-ended than SO tends to prefer.
I changed the very generic "Why are they different?" into "Can these very different libraries both be called functional, and if so, what makes each one a functional library?", which might be more acceptable there.
Niloy Mondal
@niloy
Oct 09 2015 06:38
Are there any plans to integrate generators in Ramda for infinite list goodness?
Raine Virta
@raine
Oct 09 2015 06:54
what would the API look like?
Niloy Mondal
@niloy
Oct 09 2015 06:55
API remains same, but any API that accepts array can accept an ES6 iterator
Raine Virta
@raine
Oct 09 2015 06:55
and they would have to return an iterator as well
Niloy Mondal
@niloy
Oct 09 2015 06:56
Yup
It wont break existing code because existing code dont pass Iterators
Jethro Larson
@jethrolarson
Oct 09 2015 07:55
@joneshf that's really interesting, I'll probably have to study that a bit more before it clicks though.
Niloy Mondal
@niloy
Oct 09 2015 10:27
Meh, unable to build Ramda with es6 code because UglifyJS doesnt support it :(
Raine Virta
@raine
Oct 09 2015 10:27
huh?
Niloy Mondal
@niloy
Oct 09 2015 10:28
I was trying to modify Ramda code to make it work with generators, unfortunately uglifyjs doesnt support es6 syntax
Frederik Krautwald
@Frikki
Oct 09 2015 13:02
Just babel it first, @niloy
Niloy Mondal
@niloy
Oct 09 2015 13:51
@Frikki Oh yeh, thanks
Jethro Larson
@jethrolarson
Oct 09 2015 16:09
Gulp was made for this kind of thing :)
Jethro Larson
@jethrolarson
Oct 09 2015 16:15
@joneshf can you show how you'd use cofree to create an event stream of say, clicks?