zoukankan      html  css  js  c++  java
  • [JS Compose] 0. Understand 'Box' or 'Container', they are just like Array!

    We'll examine how to unnest function calls, capture assignment, and create a linear data flow with a type we call Box. This is our introduction to working with the various container-style types.

    At first, might not be comforable with 'Box' or 'Container' idea. It bit similar to 'Array'.
    Lets see an example first:
    const nextCharForNumberString = str =>
      [str]  // [' 64 ']
      .map(s => s.trim()) // ['64']
      .map(r => parseInt(r)) // [64]
      .map(i => i + 1) // [65]
      .map(i => String.fromCharCode(i)) // ["A"]
      .map(c => c.toLowerCase()) // ["a"]
    
    const result = nextCharForNumberString('  64 ')
    
    console.log(result) // ["a"]

    Notice that, the function take one param 'str', inside the function body, the first thing we do is put 'str' into Array. Well, we know [] this is Array because we know Javascript, we know programming. Lets assume we don't know programming, '[]' this is just something looks like a 'BOX'!

    So what we do is, put 'str' into 'BOX', and every single step, we use 'map' to do transform.  Wait a second here... Because we know programming, we know what 'map' acutally does. So we think ok, this is how it should be. 

    Assume again, we don't know programming... what will you describe what happen then? 

    Well, I saw ' 64 ' inside a 'BOX'; then '64' inside the BOX; then 64 inside the BOX; then 65....

    One thing we have to notice here now is that, 'BOX' is always there! So what actually 'map' does? Try to describe it again...

    Well.. Uhm... what map actually does is that it goes into a BOX, according to the logic passed to the map and its value, doing transform to the value, then update value and set to a new BOX. So next opreation get this new BOX and new value.

    So now you can see, Array just like a Box, so we can replace to:

    const nextCharForNumberString = str =>
      Box(str)  // Box(' 64 ')
      .map(s => s.trim()) // Box('64')
      .map(r => parseInt(r)) // Box(64)
      .map(i => i + 1) // Box(65)
      .map(i => String.fromCharCode(i)) // Box("A")
      .map(c => c.toLowerCase()) // Box("a")
    
    const result = nextCharForNumberString('  64 ')
    
    console.log(result) // Box("a")

    Of course, this code won't work in Javascript. 

    So we need to define 'Box': it should be a function return an object, which has 'map' function, 'map' function accpect an function as param and take the value pass from the Box. The return value from 'map' should still put back into the Box, so that, the next map function can use it.

    const Box = (x) => ({
       map: f => Box(f(x)) 
    })

    So far is good, but we don't want the result as 'Box("a")', we just want the value "a", so we can add another function into map, which call 'fold', the function will just return the updated value.

    const Box = (x) => ({
      map: f => Box(f(x)),
      fold: f => f(x)
    })
    
    const nextCharForNumberString = str =>
      Box(str)  // Box(' 64 ')
      .map(s => s.trim()) // Box('64')
      .map(r => parseInt(r)) // Box(64)
      .map(i => i + 1) // Box(65)
      .map(i => String.fromCharCode(i)) // Box("A")
      .fold(c => c.toLowerCase()) // "a"
    
    const result = nextCharForNumberString('  64 ')
    
    console.log(result) // "a"

    Now we got "a". 

    The important thing to take away from here is: 'Box' and 'Container' are nothing but like 'Array' which we already know. 

    'map' function is not just loop over Array, it is actually goes into the Box and update the value, and return a new Box with the updated value.

  • 相关阅读:
    Python的条件判断和循环
    Python分支和循环结构的练习
    Python的变量和运算符
    Python简介
    Git的使用及网络编程多线程多进程
    函数面向对象编程及文件的读取
    函数模块字符串列表
    python变量运算符分支结构循环结构及例题
    集合
    三级菜单
  • 原文地址:https://www.cnblogs.com/Answer1215/p/6158995.html
Copyright © 2011-2022 走看看