[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 还有一些,忘记了 ...
随机推荐
- Spark MLlib LDA 源代码解析
1.Spark MLlib LDA源代码解析 http://blog.csdn.net/sunbow0 Spark MLlib LDA 应该算是比較难理解的,当中涉及到大量的概率与统计的相关知识,并且 ...
- Android滚轮选择器实现
思路: 1.布局,整个控件的布局,事实上就是用代码取带xml来实现当前布局 2,能够滑动的(即滚轮).事实上是一个ScrollView 3.推断滑动状态的,有protected void onScro ...
- 题目1205:N阶楼梯上楼问题(2008年华中科技大学计算机保研机试真题:递推求解)
题目1205:N阶楼梯上楼问题 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:2447 解决:927 题目描写叙述: N阶楼梯上楼问题:一次能够走两阶或一阶,问有多少种上楼方式. (要求 ...
- ontouch-控件添加ontouch监听事件
1,代码public class CalculatorViewPager extends ViewPager {}中 package com.android.calculator2; import a ...
- 3.阿里巴巴dubbo分布式rpc框架详细使用教程
dubbo是阿里巴巴开源的分布式服务框架,致力于提供高性能和透明化的rpc远程服务调用方案,以及soa服务治理方案,如果没有分布式需求,是不需要dubbo的,分布式环境dubbo的使用架构官方给出了一 ...
- Android .getRGB得到是负数,解决方案
情景:ava.awt.color 下面的getRGB怎么得出的是负数???本来想通过getRGB得到一个整数,在另外的一个部分在根据这个整数构件一个color,因为参数规定只能能传整数!!!color ...
- 深度解析VC中的消息
消息是指什么? 消息系统对于一个win32程序来说十分重要,它是一个程序运行的动力源泉.一个消息,是系统定义的一个32位的值,他唯一的定义了一个事件,向Windows发出一个通知,告诉应用程序某个事情 ...
- 限制tomcat仅响应本机请求(转)
http://blog.bbzhh.com/index.php/archives/135.html 在VPS上搭建了nginx和tomcat应用,想通过nginx来反向代理127.0.0.1:8080 ...
- 洛谷—— P1017 进制转换
https://www.luogu.org/problem/show?pid=1017#sub 题目描述 我们可以用这样的方式来表示一个十进制数: 将每个阿拉伯数字乘以一个以该数字所处位置的(值减1) ...
- Seal Report开放数据库报表工具(.Net)
Seal Report_20160923 概述:开放数据库报表工具(.Net) 简介:Seal-Report提供了一个完整的框架,用于从任何数据库生成日常报告和仪表板.Seal-Report是Micr ...