You can really unlock the power of ramda (and functional programming in general) when you combine functions. Counting words in a string may seem like a relatively difficult task, but ramda makes it easy by providing a countBy function. This lesson walks through using the countBy to count words in a string.

const text = `
'have no fear of this mess,'
said the cat in the hat.
'i always pick up all my playthings
and so...
i will show you another
good trick that i know!' then we saw him pick up all the things that were down.
he picked up the cake,
and the rake, and the gown,
and the milk, and the strings,
and the books, and the dish,
and the fan, and the cup,
and the ship, and the fish.
and he put them away.
then he said, 'that is that.'
and then he was gone
with a tip of his hat. then our mother came in
and she said to us two,
'did you have any fun?
tell me. what did you do?' and sally and i did not know
what to say.
should we tell her
the things that went on there that day? should we tell her about it?
now, what SHOULD we do?
well...
what would YOU do
if your mother asked YOU?` console.clear() const countWords = R.compose(
R.map(R.sortBy(R.identity)),
R.invert,
R.countBy(R.identity),
R.map(R.toLower),
R.match(/\w+/g)
);
const res = countWords(text);
console.log(res);
/*

{
1: ["a", "about", "always", "another", "any", "asked", "away", "books", "cake", "came", "cat", "cup", "day", "dish", "down", "fan", "fear", "fish", "fun", "gone", "good", "gown", "him", "his", "if", "is", "it", "me", "mess", "milk", "my", "no", "not", "now", "on", "our", "picked", "playthings", "put", "rake", "sally", "saw", "say", "she", "ship", "show", "so", "strings", "them", "there", "this", "tip", "trick", "two", "us", "was", "well", "went", "were", "will", "with", "would", "your"],
15: ["the"],
16: ["and"],
2: ["all", "hat", "have", "her", "in", "know", "mother", "of", "pick", "things", "to"],
3: ["did", "do", "said", "should", "tell", "up"],
4: ["he", "i", "then", "we", "what"],
5: ["you"],
6: ["that"]
}
*/

最新文章

  1. salesforce 零基础学习(六十二)获取sObject中类型为Picklist的field values(含record type)
  2. 使用Zabbix监控Oracle数据库
  3. [转]MySQL日志——Undo | Redo
  4. 工厂模式模拟Spring的bean加载过程
  5. "undefined method `root' for nil:NilClass" error when using "pod install" 解决办法
  6. Caliburn.Micro学习笔记目录——Zhouyongh
  7. Android之ScrollView嵌套ListView
  8. SPOJ #500. Turbo Sort
  9. Android使用绘图Path总结
  10. (转)用AGG实现高质量图形输出(二)
  11. Microsoft Word Regular Expression
  12. jsp分页的不同实现方法
  13. linux 在jetty中部署web工程
  14. Python学习笔记 set&&dict
  15. Nginx+Django-Python+BPMN-JS的整合工作流实战项目
  16. vue怎么引入外网json文件
  17. xmind 8 便携版:关联文件后,双击打开文件,在当前文件夹产生configuration子文件的问题解决办法
  18. 探索未知种族之osg类生物---渲染遍历之裁剪二
  19. JSP中request获取值
  20. linux中的signal机制(转)

热门文章

  1. git提交代码到本地仓库和远程仓库
  2. 洛谷 P2562 [AHOI2002]Kitty猫基因编码
  3. HDU 2489 Minimal Ratio Tree(prim+DFS)
  4. Zorka和zico实现不同主机之间的交互
  5. TabControl里面添加From
  6. Java Web学习总结(7)——HttpServletRequest对象
  7. JS实现时钟效果
  8. 快速搭建REST API——json server
  9. 【转】排列组合 "n个球放入m个盒子m"问题 总结
  10. 【Codeforces Round #299 (Div. 2) B】Tavas and SaDDas