zoukankan      html  css  js  c++  java
  • [Javascript] Broadcaster + Operator + Listener pattern -- 11. Customize the done logic

    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)
    }))
  • 相关阅读:
    PHP和Ajax设置页面请求超时
    Flex 布局教程
    数据库访问优化法则
    phpcms网站搬家至服务器
    phpcms网页替换验证码及搜索功能
    php判断手机段登录
    php环境搭建
    ThinkPHP框架
    JQuery事件
    JQuery
  • 原文地址:https://www.cnblogs.com/Answer1215/p/13875577.html
Copyright © 2011-2022 走看看