首页 > 编程语言 > 详细

[Javascript] Broadcaster + Operator + Listener pattern -- 11. Customize the done logic

时间:2020-10-25 22:49:58      阅读:25      评论:0      收藏:0      [点我收藏+]

Buffers give you chance to gather values together until your ready to work with them. This pattern can be used for calculations, string manipulations, and many other scenarios.

Consider a solution where splitter argument is a function instead of a value. How could you capture the condition in that function rather than the way it was implemented in this lesson

 

Sometime if "createOpertor"‘s done logic is not actully what you want, when you can customize you own done logic:

const split = splitter => curry((broadcaster, listener) => {
  let buffer = []
  return broadcaster((value) => {
    if (value === done) {
      // emit the rest of buffer on done
      listener(buffer)
      listener(done)
      buffer = []
    }
    if (value === splitter) {
      listener(buffer)
      buffer = []
    } else {
      buffer.push(value)
    }
  })
})

 

Usage:

const transform =  pipe(
    map((x) => x[1]),
    filter((x) => x !== ‘,‘),
    map(toUpper),
    split(" ")
  );
let typeGreeting = transform(
  createZipOf(createInterval(100), createForOf(‘My Zipo‘))
);
const cancelGreating = typeGreeting((value => {
  if(value === done) {
    _log("Shut down")
    return
  }
  _log(value)
}))

 

[Javascript] Broadcaster + Operator + Listener pattern -- 11. Customize the done logic

原文:https://www.cnblogs.com/Answer1215/p/13875577.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!