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.

  1. const text = `
  2. 'have no fear of this mess,'
  3. said the cat in the hat.
  4. 'i always pick up all my playthings
  5. and so...
  6. i will show you another
  7. good trick that i know!'
  8.  
  9. then we saw him pick up all the things that were down.
  10. he picked up the cake,
  11. and the rake, and the gown,
  12. and the milk, and the strings,
  13. and the books, and the dish,
  14. and the fan, and the cup,
  15. and the ship, and the fish.
  16. and he put them away.
  17. then he said, 'that is that.'
  18. and then he was gone
  19. with a tip of his hat.
  20.  
  21. then our mother came in
  22. and she said to us two,
  23. 'did you have any fun?
  24. tell me. what did you do?'
  25.  
  26. and sally and i did not know
  27. what to say.
  28. should we tell her
  29. the things that went on there that day?
  30.  
  31. should we tell her about it?
  32. now, what SHOULD we do?
  33. well...
  34. what would YOU do
  35. if your mother asked YOU?`
  36.  
  37. console.clear()
  38.  
  39. const countWords = R.compose(
  40. R.map(R.sortBy(R.identity)),
  41. R.invert,
  42. R.countBy(R.identity),
  43. R.map(R.toLower),
  44. R.match(/\w+/g)
  45. );
  46. const res = countWords(text);
  47. console.log(res);
  1. /*
  2.  
  3. {
  4. 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"],
  5. 15: ["the"],
  6. 16: ["and"],
  7. 2: ["all", "hat", "have", "her", "in", "know", "mother", "of", "pick", "things", "to"],
  8. 3: ["did", "do", "said", "should", "tell", "up"],
  9. 4: ["he", "i", "then", "we", "what"],
  10. 5: ["you"],
  11. 6: ["that"]
  12. }
  13. */

[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. GeoServer 常见问题总结 (转)

    geoserver在部署发布服务时,经常会遇到如下问题,现总结如下: 1.忘记了GeoServer Web Admin Page的登陆用户名和密码怎么办?存储位置:C:\Program Files\G ...

  2. 在一台机子上,安装,运行两mysql数据库实例

    为了方便测试,想要用到两个mysql数据库实例.以windows系统为例 当然安装第一个mysql数据库,很简单没什么说的.但是再要安装,运行mysql安装程序时,会提示,修复,卸载,重新安装. 这时 ...

  3. Detecting a return-oriented programming exploit

    A method and apparatus for detecting a Return-Oriented Programming exploitation. At a computer devic ...

  4. 玩转Bootstrap(基础) -- (6.导航条基础)

    1.导航条样例 <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" ...

  5. 9.Spring Boot实战之配置使用Logback进行日志记录

    转自:https://blog.csdn.net/meiliangdeng1990/article/details/54300227 Spring Boot实战之配置使用Logback进行日志记录 在 ...

  6. 利用formdata对象上传文件时,需要添加的参数

    function doUpload() { var formData = new FormData($( "#uploadForm" )[0]); $.ajax({ url: 'h ...

  7. Maven搭建Spring Security3.2项目详解

    本来是打算在上一篇SpringMVC+Hibernate上写的,结果发现上面那篇 一起整合的,结果发现上一篇内容实在是太长了,就另起一篇,这篇主要是采用 Maven搭建Spring+SpringMVC ...

  8. SQL Server 中计算农历

    1.建一表,放初始化资料   因为农历的日期,是由天文学家推算出来的,到现在只有到2049年的,以后的有了还可以加入!   CREATE TABLE SolarData ( yearId int no ...

  9. python整除

    1.'/'除号与c不同,单个'/'是浮点除,两个除号'//'才是整除

  10. swift 数据存储

    1.plist 存储 1.利用沙盒根目录拼接“Documents”字符串 //存储 func saveArray() { // 1.获得沙盒根路径,不管是真机还是模拟机,用它是最合适不过了 let h ...