About
RSS

Bit Focus


记一些 (没) 有意义的 reduce 用法

Posted at 2013-09-30 03:26:41 | Updated at 2024-03-19 09:15:51

    在 Python 或 Javascript 等许多语言中都有 reduce 函数. 其中 Python 中 reduce 作为全局函数出现, 而 Javascript 中则是 Array 的成员函数. 大量的用 reduce 来做累加累乘之类的例子就不说了, 这里探讨一个特殊的用例.
    前端经常会需要将页面中用户填写的一些内容打包成 JSON 字典, 比如一个注册页面片段
<div>
    <input id='email' placeholder='Email'>
    <input id='password' placeholder='Password'>
    <input id='conform_password' placeholder='Confirm Password'>
    <input id='address' placeholder='Address'>
    <input id='phonenum' placeholder='Phone Number'>
    <button id='subm'>Submit</button>
</div>

<script>
document.getElementById('subm').onclick = function() {
    var inputValues = {
        email: document.getElementById('email').value,
        password: document.getElementById('password').value,
        address: document.getElementById('address').value,
        phonenum: document.getElementById('phonenum').value
    };
    /* process inputValues */
};
</script>
    以后每次这个表单多一项时, 构造 inputValues 时就会多一项, 代码维护会很烦.
    如果能这样写的话可能会好一些
var inputValues = {k: document.getElementById(k).value
                   for k in ['email', 'password', 'address', 'phonenum']};
    可惜 Javascript 里面没有温暖人心的 dict comprehension... 于是, 就有了下面这种 reduce 替代品 (终于正题了)
var inputValues = ['email', 'password', 'address', 'phonenum'].reduce(
    function(obj, item) {
        obj[item] = document.getElementById(item).value;
        return obj;
    }, {}));
    看来 Python 在语法灵活度上的优势已经虐掉 filter, map 函数两条街, 而且 dict comprehension 还能顺带打压一下 reduce, 也难怪 Guido 会挑明了说他不喜欢这些函数. 不过他不喜欢 reduce 主要是这东西太灵活, 一眼看不明白 (能一眼看明白的都直接用 sum 去了). 比如上面这个用例如果没有铺垫, 要理解还得废点草稿纸 (用来提升 bignity 倒是不错). 而下面这个例子也是 (Python)
some_dict = {
    'x': {
        'y': {
            'a': 10
        },
        'z': {
            'b': 20
        },
    },
}
reduce(lambda obj, key: obj.get(key, dict()), ['x', 'y', 'a'], some_dict)
    嘛, 是否允许在项目里面使用类似的机巧代码, 也许是比「哪个语言好」更值得每月探 (zheng) 讨 (chao) 的问题; 或是指定一些固定用法作为范式, 像背乘法口诀表一样背下来也许也不错的说.

Post tags:   Javascript  函数式程序设计  Python

Leave a comment:




Creative Commons License Your comment will be licensed under
CC-NC-ND 3.0


. Back to Bit Focus
NijiPress - Copyright (C) Neuron Teckid @ Bit Focus
About this site