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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. [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 ...

  4. 深入理解Java中的String

    一.String类 想要了解一个类,最好的办法就是看这个类的实现源代码,来看一下String类的源码: public final class String implements java.io.Ser ...

  5. string相关总结

    一 <string> 1 string类常见成员函数 (1)属性相关 s.empty()   s为空时返回true,否则返回false s.size()      返回s中字符的个数,不包 ...

  6. java内存分配和String类型的深度解析

    [尊重原创文章出自:http://my.oschina.net/xiaohui249/blog/170013] 摘要 从整体上介绍java内存的概念.构成以及分配机制,在此基础上深度解析java中的S ...

  7. 探秘Java中的String、StringBuilder以及StringBuffer

    探秘Java中String.StringBuilder以及StringBuffer 相信String这个类是Java中使用得最频繁的类之一,并且又是各大公司面试喜欢问 到的地方,今天就来和大家一起学习 ...

  8. 读书笔记-String

    [String]就是对char[]数组进行封装的对象,由三部分组成: 1, char数组:它是String对象所表示的字符串的超集: 2, 3, offset和count,表示了String对象表示的 ...

  9. C string.h 常用函数

    参考:http://womendu.iteye.com/blog/1218155 http://blog.csdn.net/zccst/article/details/4294565 还有一些,忘记了 ...

随机推荐

  1. jsp 用shiro 的判断 是否有菜单查看的权限

    实例:spring-shiro.xml 123 /admin/repairType/index = roles["ROLE_ADMIN"]/admin/user=roles[&qu ...

  2. geotools修改shapefile 属性名乱码问题

    在GeoServer中文社区的讨论地址为:http://opengeo.cn/bbs/read.php?tid=1701&page=e&#a 使用geotools修改shapefile ...

  3. 洛谷 P2347 砝码称重

    P2347 砝码称重 题目描述 设有1g.2g.3g.5g.10g.20g的砝码各若干枚(其总重<=1000), 输入输出格式 输入格式: 输入方式:a1 a2 a3 a4 a5 a6 (表示1 ...

  4. 如何去掉bootstrap table中表格样式中横线竖线

    修改之前,表格看上去比较拥挤,因为bootstrap table插件中自带斑马线表格样式,有横线和竖线分栏,现在我们不需要这些. 应UI设计的要求,要去掉中间的横线和竖线,使用了修改需求中一种简单粗暴 ...

  5. 分治法(divide & conquer)与动态规划(dynamic programming)应用举例

    动态规划三大重要概念:最优子结构,边界,状态转移公式(问题规模降低,如问题由 n 的规模降低为 n−1 或 n−2 及二者之间的关系): 0. 爬台阶 F(n)⇒F(n−1)+F(n−2) F(n−1 ...

  6. Windows Forms 对话框篇

    1,标准对话框 Windows内置的对话框,又叫公用对话框,它们作为组件提供的,并且存在于System.Windows.Forms命名空间中. 手工方式: private void button1_C ...

  7. Fragment Summary 1/2

    转自:http://blog.csdn.net/lmj623565791/article/details/37970961 自从Fragment出现,曾经有段时间,感觉大家谈什么都能跟Fragment ...

  8. 常用的Windows命令

    常用的Windows命令 explorer-------打开资源管理器 logoff---------注销命令 shutdown-------关机命令 lusrmgr.msc----本机用户和组 se ...

  9. LA 3026 - Period KMP

    看题传送门:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show ...

  10. MAVEN Error: Using platform encoding (GBK actually) to copy filtered resources.....

    环境:Maven3.2.5+MyEclipse 2015CI 现象:在Maven编译过程中出现错误信息:Using platform encoding (GBK actually) to copy f ...