These are chat archives for pozadi/kefir

25th
Apr 2015
Artem Kozlov
@aindlq
Apr 25 2015 10:25
I wonder does it make any sense to use Promises together with Kefir, in theory Kefir already provides everything and even more in comparison to ES6 promises.
Promise is basically kind of a property which doesn't change value as soon as there is one.
I wonder what other people think about this.
Sergey Lapin
@lapanoid
Apr 25 2015 10:37
@artemkozlov You can easily wrap property into promise, to catch some value for example. But property and promise is really different things.
Artem Kozlov
@aindlq
Apr 25 2015 10:42
@lapanoid I'm not so sure, what is the difference?
Sergey Lapin
@lapanoid
Apr 25 2015 10:46
Something like this
toCatchPromise = (streamOrProp)->
  d = Q.defer()
  procOnValue = (v)->
    streamOrProp.offValue(procOnValue)
    d.resolve(v)
  streamOrProp.onValue(procOnValue)
  d.promise
Mainly same difference like with streams and promises. Stream is a continuation of values, promise is one value
Roman Pominov
@rpominov
Apr 25 2015 11:39
@artemkozlov I think properties can replace promises. As you said they provide all features of promises + something more.
Properties also provide cancelation, and by a coincedence I am working on an example to demonstrate this right now :smile:
wip:
function ajax(options) {
  return Kefir.stream(function(emitter) {
    // make request, and subscribe to the result
    return function() {
      // if request still in progress, cancel it
    }
  }).take(1).endOnError().toProperty();
}
Roman Pominov
@rpominov
Apr 25 2015 11:46
The ajax function returns a property. When the property gets a subscriber the request is send, and if all subscribers will unsubscribe before it done, request will be canceled.
Btw, I work on examples now. Here what I have so far:
http://jsfiddle.net/r8znhpka/1/ — Click to alert
http://jsfiddle.net/3cqorb76/ — Clock
http://jsfiddle.net/3cqorb76/1/ — Clock controlled by scrolling
http://jsfiddle.net/3cqorb76/4/ — Clock controlled by both time and scrolling
http://jsfiddle.net/o1spr8hx/2/ — Multiple clicks
http://jsfiddle.net/mw18nbrr/2/ — Drag a div
http://jsfiddle.net/jL1nm3c3/2/ — Tree
"Clock controlled by both time and scrolling" is my favorite at the moment :)
Sergey Lapin
@lapanoid
Apr 25 2015 11:52
Looks funny and pretty cool)
Sergey Lapin
@lapanoid
Apr 25 2015 12:00
I think promise won’t be completlly replaced ever cause it is just a simplier abstraction. So conversion between stream-prop and promise and relationship between them is some open field.
Roman Pominov
@rpominov
Apr 25 2015 12:03
agree, all I saying — whenever you use a Promise, you can replace it with a Property.
But not vice versa.
Sergey Lapin
@lapanoid
Apr 25 2015 12:07
You can catch some first value for example.. Or catch last value before streams ends.. There could be other applications of promises
Sergey Lapin
@lapanoid
Apr 25 2015 12:27
After all in es6 promises will became standart so I guess it will be like second birsday for them in js community)
Raymond Tijhaar
@tijhaart
Apr 25 2015 14:54
This message was deleted
Artem Kozlov
@aindlq
Apr 25 2015 15:08
I was just thinking that it is much more convenient to use one set of APIs.
Right now I'm using Kefir, Promise, than some libraries to work with collections like lodash and immutable.js which all have they own API.
so I'm trying to minimize API diversity. Kefir for streams and promises. + transducers for actions on streams and collections.
@pozadi I like your examples, thanks for sharing
Artem Kozlov
@aindlq
Apr 25 2015 15:19
@pozadi have you seen https://github.com/fantasyland/fantasy-land, what do you think is it possible to think about Streams and Properties in terms of Monads etc.
Roman Pominov
@rpominov
Apr 25 2015 16:12
@artemkozlov Yes, this is interesting stuff, but seems to not match very well with features of Kefir.
Didn't dig too much, though.
There is another FRP lib https://github.com/cujojs/most it's trying to implement fantasy-land spec.
Also it is interesting related to Promises convo, as I understand, it uses Promises as part of API.
Artem Kozlov
@aindlq
Apr 25 2015 18:48
@pozadi could you please explain why do you think that it doesn't match very well with Kefir. My intuition suggests me that Stream or Property is a Monad :smile:
Artem Kozlov
@aindlq
Apr 25 2015 19:30
I've just found very entertaining promise/monads discussions - promises-aplus/promises-spec#94, lots of trolling but also lots of sensible arguments
Roman Pominov
@rpominov
Apr 25 2015 19:30
@artemkozlov I didn't dig to much, as I mentioned earlier, but if I understand right, Stream/Property do not implement Applicative
Also we don't have equivalent of Apply's .ap, but it looks possible to add.
Roman Pominov
@rpominov
Apr 25 2015 19:36
And we have all other parts: .map = .map, .chain = .flatMap
But not sure about strict equivalence of .chain and .flatMap
Roman Pominov
@rpominov
Apr 25 2015 19:43
And have to say I'm not very fluent in all this heavy FP stuff :)
Roman Pominov
@rpominov
Apr 25 2015 19:51
Perhaps the purpose of fantasy-land project is to troll lib authors)
Artem Kozlov
@aindlq
Apr 25 2015 19:52
not really :smile: , I hope you don't reject all this FP/Category Theory related stuff just because it is too "theoretic"
because most people tend to :(
People come to it in math because they have so many different things and had a feeling that it would be great somehow to unify them
To easily apply knowledge that you have in one domain to another one, etc
And I have a feeling that this is something that would be great to apply in JS world as well. Where you have so many different APIs, but want to compose them well and reason about result of the composition
Roman Pominov
@rpominov
Apr 25 2015 19:56
I don't reject it, I just not very familiar with it at the moment, but going to learn)
Artem Kozlov
@aindlq
Apr 25 2015 19:56
@pozadi you need to try Haskell :)
but this is one way road, there is no way back ....
Roman Pominov
@rpominov
Apr 25 2015 20:00
I'am mostly a front-end developer and want to learn something I could use at work. I learned pure-script a bit, and this was fun, but didn't gone too far.
Perhaps we need types in JS to adapt all this cool FP things.
Artem Kozlov
@aindlq
Apr 25 2015 20:06
Typescript
Sergey Lapin
@lapanoid
Apr 25 2015 22:33
@aindlq My eyes was on fire while I was reading this promises-aplus/promises-spec#94 ))