[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 还有一些,忘记了 ...
随机推荐
- GeoServer 常见问题总结 (转)
geoserver在部署发布服务时,经常会遇到如下问题,现总结如下: 1.忘记了GeoServer Web Admin Page的登陆用户名和密码怎么办?存储位置:C:\Program Files\G ...
- 在一台机子上,安装,运行两mysql数据库实例
为了方便测试,想要用到两个mysql数据库实例.以windows系统为例 当然安装第一个mysql数据库,很简单没什么说的.但是再要安装,运行mysql安装程序时,会提示,修复,卸载,重新安装. 这时 ...
- Detecting a return-oriented programming exploit
A method and apparatus for detecting a Return-Oriented Programming exploitation. At a computer devic ...
- 玩转Bootstrap(基础) -- (6.导航条基础)
1.导航条样例 <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" ...
- 9.Spring Boot实战之配置使用Logback进行日志记录
转自:https://blog.csdn.net/meiliangdeng1990/article/details/54300227 Spring Boot实战之配置使用Logback进行日志记录 在 ...
- 利用formdata对象上传文件时,需要添加的参数
function doUpload() { var formData = new FormData($( "#uploadForm" )[0]); $.ajax({ url: 'h ...
- Maven搭建Spring Security3.2项目详解
本来是打算在上一篇SpringMVC+Hibernate上写的,结果发现上面那篇 一起整合的,结果发现上一篇内容实在是太长了,就另起一篇,这篇主要是采用 Maven搭建Spring+SpringMVC ...
- SQL Server 中计算农历
1.建一表,放初始化资料 因为农历的日期,是由天文学家推算出来的,到现在只有到2049年的,以后的有了还可以加入! CREATE TABLE SolarData ( yearId int no ...
- python整除
1.'/'除号与c不同,单个'/'是浮点除,两个除号'//'才是整除
- swift 数据存储
1.plist 存储 1.利用沙盒根目录拼接“Documents”字符串 //存储 func saveArray() { // 1.获得沙盒根路径,不管是真机还是模拟机,用它是最合适不过了 let h ...