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. 一次Linux磁盘损坏导致系统不可用恢复实例

    Linux操作系统的server重新启动后.系统启动报错,系统无法正常使用. 1.报错信息 1.1.报错屏幕信息 1.2.报错信息提取关键信息 (1)/dev/sda3:File -(inode #1 ...

  2. amazeui学习笔记二(进阶开发3)--HTML/CSS规范Rules

    amazeui学习笔记二(进阶开发3)--HTML/CSS规范Rules 一.总结 1.am:以 am 为命名空间 2.模块状态: {命名空间}-{模块名}-{状态描述} 3.子模块: {命名空间}- ...

  3. Codeforces Round #100 E. New Year Garland (第二类斯特林数+dp)

    题目链接: http://codeforces.com/problemset/problem/140/E 题意: 圣诞树上挂彩球,要求从上到下挂\(n\)层彩球.已知有\(m\)种颜色的球,球的数量不 ...

  4. amazeui页面分析3

    amazeui页面分析3 一.总结 1. 本质是list列表,是ul套li的形式,只不过li里面是图片 <li class="am-g am-list-item-desced am-l ...

  5. Intel X86 CPU 系列的寻址方式

    Intel X86 CPU 系列的寻址方式 数据总线和地址总线要尽量相同,这个是一个地址就是一个指针.

  6. 在云服务器上(CentOS)上安装Node

    今天手抖,买了台云服务器,本人对服务器啥的基本不懂,linux命令基本靠度娘,所以连装个node环境都历经坎坷,搞了一下午终于搞好了,记录一下: 第一步:当然是先登录服务器了,打开命令行窗口,输入: ...

  7. 【z02】选择客栈

    (hotel.cpp/c/pas) [问题描述] 丽江河边有n 家很有特色的客栈,客栈按照其位置顺序从 1 到n 编号.每家客栈都按照 某一种色调进行装饰(总共 k 种,用整数 0 ~ k-1 表示) ...

  8. 8、hzk16的介绍以及简单的使用方法

    HZK16 字库是符合GB2312标准的16×16点阵字库,HZK16的GB2312-80支持的汉字有6763个,符号682个.其中一级汉字有3755个,按 声序排列,二级汉字有3008个,按偏旁部首 ...

  9. 19、opencv和v4l2的关系

    分析如下: v4L2是针对uvc免驱usb设备的编程框架,而opencv是一种跨平台计算机视觉库,opencv不仅支持v4l2框架,还支持windows.os等操作系统上的摄像头框架 cvCreate ...

  10. angular2的编译模式之AOT和JIT

    原文 https://www.jianshu.com/p/c959d90e91ce 大纲 1.angular应用为什么需要编译 2.angular的编译模式类型 3.JIT(Just-In-Time) ...