Skip to content
This repository has been archived by the owner on Jul 25, 2018. It is now read-only.

Store map doesn't hold value for get. #1

Closed
SimonRichardson opened this issue Aug 23, 2013 · 3 comments
Closed

Store map doesn't hold value for get. #1

SimonRichardson opened this issue Aug 23, 2013 · 3 comments

Comments

@SimonRichardson
Copy link
Member

I'm not sure if this is correct or not, considering I've never used a store before so could be completely wrong!

But if you try:

var Store = require('fantasy-stores'),
    target,
    store = Store(
      function(x) { return target = x; },
      function() { return target; }
    ),
    newStore;

store.set(1);

console.log(store.get());

newStore = store.map(function(a) { return a + 1; });

console.log(newStore.set(1)); // outputs 2.
console.log(newStore.get()); // outputs 1.

Try here on requirebin.

I would expect the third console.log to output 2 as well or am I completely missing the point?

Cheers
Simon.

@puffnfresh
Copy link
Member

Not missing the point - but get doesn't do what you want in this case. What we need is extract:

4 == (extract . fmap (*2) $ store (+1) 1)

Comonads! Awesome, right? Also means we should have extend.

@puffnfresh
Copy link
Member

Oh, looks like we already have that:

> store(function(x) { return x + 1; }, function() { return 1; }).map(function(x) { return x * 2; }).extract()
4

@SimonRichardson
Copy link
Member Author

Ah, that does make sense now.
Cheers

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants