[Ramda] Count Words in a String with Ramda's countBy and invert
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"]
}
*/
[Ramda] Count Words in a String with Ramda's countBy and invert的更多相关文章
- [Ramda] Get Deeply Nested Properties Safely with Ramda's path and pathOr Functions
In this lesson we'll see how Ramda's path and pathOr functions can be used to safely access a deeply ...
- [Ramda] Refactor to Point Free Functions with Ramda using compose and converge
In this lesson we'll take some existing code and refactor it using some functions from the Ramda lib ...
- [Ramda] Eliminate Function Arguments (Point-Free Style) with Ramda's Converge
When doing comparisons inside of functions, you end of relying heavily on the argument passed into t ...
- 深入理解Java中的String
一.String类 想要了解一个类,最好的办法就是看这个类的实现源代码,来看一下String类的源码: public final class String implements java.io.Ser ...
- string相关总结
一 <string> 1 string类常见成员函数 (1)属性相关 s.empty() s为空时返回true,否则返回false s.size() 返回s中字符的个数,不包 ...
- java内存分配和String类型的深度解析
[尊重原创文章出自:http://my.oschina.net/xiaohui249/blog/170013] 摘要 从整体上介绍java内存的概念.构成以及分配机制,在此基础上深度解析java中的S ...
- 探秘Java中的String、StringBuilder以及StringBuffer
探秘Java中String.StringBuilder以及StringBuffer 相信String这个类是Java中使用得最频繁的类之一,并且又是各大公司面试喜欢问 到的地方,今天就来和大家一起学习 ...
- 读书笔记-String
[String]就是对char[]数组进行封装的对象,由三部分组成: 1, char数组:它是String对象所表示的字符串的超集: 2, 3, offset和count,表示了String对象表示的 ...
- C string.h 常用函数
参考:http://womendu.iteye.com/blog/1218155 http://blog.csdn.net/zccst/article/details/4294565 还有一些,忘记了 ...
随机推荐
- APM2.8 Rover 自己主动巡航车设计(固件安装和设置)
1.2 APM2.8软件安装与固件下载 下载Mission Planner这个地面基站软件,这里介绍的是windoews平台下的,在MAC或者linux下能够使用QGroundCont基于QT编写的地 ...
- IOS-Run loop学习总结
不知道大家有没有想过这个问题,一个应用開始执行以后放在那里,假设不正确它进行不论什么操作.这个应用就像精巧了一样,不会自发的有不论什么动作发生.可是假设我们点击界面上的一个button.这个时候就会有 ...
- loadrunner监控linux之linux下安装rpc
安装和配置rpc服务 说明:rpc服务需rsh的支持,一般情况下rsh已安装.通过rpm -qa rsh命令查看. 下载rpc.rstatd-4.0.1.tar.gz,可先下载到window下,通过f ...
- Mongodb总结3-稍微封装一下
上次发表的2,纯粹是Demo,演示API的用法. 今天,稍微封装了下,看得更清楚. 考虑到不容易做得很有通用性,所以封装的一般,换种场景需要直接修改代码,但是有一部分是可以复用的. 最近项目,很可能只 ...
- COGS——C66. [HAOI2004模拟] 数列问题
http://www.cogs.pro/cogs/problem/problem.php?pid=66 ★☆ 输入文件:dfs3.in 输出文件:dfs3.out 简单对比 时间限制:1 ...
- 【软件project】 文档 - 银行业务管理 - 需求分析
---------------------------------------------------------------------------------------------------- ...
- 哈夫曼树的介绍 ---java实现
一. 什么是哈夫曼树 是一种带权路径长度最短的二叉树,也称最优二叉树 带权路径长度:WPL=(W1*L1+W2*L2+W3*L3+...+ Wn*Ln) N个权值Wi(i=1,2,...n)构 ...
- 2013腾讯编程马拉松初赛第〇场(HDU 4504)威威猫系列故事——篮球梦
http://acm.hdu.edu.cn/showproblem.php?pid=4504 题目大意: 篮球赛假如我们现在已经知道当前比分 A:B,A代表我方的比分,B代表对方的比分,现在比赛还剩下 ...
- Leetcode之Best Time to Buy and Sell Stock
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- 每日技术总结:promise,express route,评分,local storage商品浏览历史,
最近正在用Vue做一个电商项目.利用工作前后空隙时间. 1.promise的使用 点这里 如何在实际项目中使用Promise 2. Express Route 前后端传参的两种方法 (1)req.pa ...