[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"] } */
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2016-02-08 [AngularJS + RxJS] Search with RxJS
2016-02-08 [Redux] Passing the Store Down with <Provider> from React Redux