如何理解JavaScript中的原编程 和 函数式编程
来源:互联网 发布:网络推广数据指标体系 编辑:程序博客网 时间:2024/05/16 05:21
在 《Paradigms of Artificial Intelligence Programming》一书中有一个简单的随机句子生产器程序,原网址今天好像不能打开
不过在http://blog.csdn.net/zzljlu/article/details/7830259 中的末尾有实现 为了了解JS 的函数式编程和原编程威力
也用JS实现了一下, 没有注释 望谅解
---------------------------------------------------------------------------------------------------------
var grammer;var simpleGrammar = [['sentence', '_', ['nounPhrase', 'verbPhrase']], ['nounPhrase', '_', ['article', 'noun']], ['verbPhrase', '_', ['verb', 'nounPhrase']], ['article', '_', 'the', 'a'], ['noun', '_', 'man', 'woman', 'ball', 'table'], ['verb', '_', 'hit', 'took', 'saw', 'liked']];var biggerGrammar = [['sentence', '_', ['nounPhrase', 'verbPhrase']], ['nounPhrase', '_', ['article', 'adj1', 'noun', 'pp1'], ['name'], ['pronoun']], ['verbPhrase', '_', ['verb', 'nounPhrase', 'pp1']], ['pp1', '_', null, ['pp', 'pp1']], ['adj1', '_', null, ['adj', 'adj1']], ['pp', '_', ['prep', 'nounPhrase']], ['prep', '_', 'to', 'in', 'by', 'with', 'on'], ['adj', '_', 'big', 'little', 'blue', 'green', 'adiabatic'], ['article', '_', 'the', 'a'], ['name', '_', 'pat', 'kim', 'lee', 'terry', 'robin'], ['noun', '_', 'man', 'ball', 'woman', 'table'], ['verb', '_', 'hit', 'took', 'saw', 'liked'], ['pronoun', '_', 'the', 'she', 'it', 'these', 'those', 'that']];function ruleLeft(rule) { return (rule instanceof Array) && rule[0];};function ruleRight(rule) { return (rule instanceof Array) && rule.slice(2);};function rewrites(category) { return ruleRight(assoc(category, grammer));};function generate(phrase) { if ((phrase instanceof Array)) { return mappend(generate, phrase); } else if (rewrites(phrase)) { return generate(randomElt(rewrites(phrase))); } else { return [phrase]; };};function assoc(category, grammer) { if ((grammer instanceof Array)) { for (var len = grammer.length, i = 0; i < len; i = ++i) { var rule = grammer[i]; if (category === rule[0]) { return rule; }; }; };};function mappend(fn, array) { return array.map(fn).reduce(function (x, y) { return x.concat(y); });};function randomElt(array) { return array[Math.floor(Math.random() * array.length)];};function generateTree(phrase) { if ((phrase instanceof Array)) { return phrase.map(generateTree); } else if (rewrites(phrase)) { var temp = generateTree(randomElt(rewrites(phrase))); temp.unshift(phrase); return temp; } else { return [phrase]; };};function generateAll(phrase) { if ((phrase instanceof Array) && phrase.length === 0) { return [[]]; } else if ((phrase instanceof Array)) { return combineAll(generateAll(phrase[0]), generateAll(phrase.slice(1))); } else if (rewrites(phrase)) { return mappend(generateAll, rewrites(phrase)); } else { return [[phrase]]; };};function combineAll(xarray, yarray) { return mappend(function (y) { return xarray.map(function (x) { return x.concat(y); }); }, yarray);};grammer = simpleGrammar;console.log(generateAll('sentence').length);
0 0
- 如何理解JavaScript中的原编程 和 函数式编程
- 理解javascript函数式编程中的闭包
- 如何理解函数式编程?
- Ruby,Scala和JavaScript中的函数式编程(一)
- 深入理解Javascript函数编程
- 一步一步教你理解和实现iOS中的链式编程和函数式编程
- 深入理解Scala中的函数式编程
- 理解Scala中的函数式编程
- 深入理解Scala中的函数式编程
- JavaScript中的函数式编程实践
- JavaScript 中的函数式编程实践
- JavaScript 中的函数式编程实践
- JavaScript 函数式编程中的 curry 实现
- JavaScript 中的函数式编程实践
- 初探 JavaScript 中的函数式编程
- 理解函数式编程
- 理解函数式编程
- 理解函数式编程
- hdu 1232畅通工程
- android的窗口机制分析------UI管理系统
- php学习之旅:在页面显示报错信息
- Leetcode144: Construct Binary Tree from Preorder and Inorder Traversal
- 机器学习中的相似性度量
- 如何理解JavaScript中的原编程 和 函数式编程
- Android 依据变量来获得资源R中的id值
- 数学笔记(一)之列主序矩阵
- codeforces-437C-The Child and Toy
- iOS工程中编译器报黄警告
- Leetocde_290_Word Pattern
- 使用MPI并行求解前缀和(prefix sum)
- UVa 156 - Ananagrams【各种stl】
- html5新增DOM