[Functional Programming] Function modelling -- 7. contramap & Endo execrises
amp -- Function Programming functional
2023-09-14 08:59:14 时间
// Definition const Endo = run => ({ run, concat: other => Endo(x => other.run(run(x))) }); Endo.empty = () => Endo(x => x); // Ex1: // ========================= const classToClassName = html => html.replace(/class\=/gi, "className="); const updateStyleTag = html => html.replace(/style="(.*)"/gi, "style={{$1}}"); const htmlFor = html => html.replace(/for=/gi, "htmlFor="); //const ex1 = html => // htmlFor(updateStyleTag(classToClassName(html))) //rewrite using Endo // Can also use List foldMap // Solution1 // const ex1 = html => List.of(htmlFor, updateStyleTag, classToClassName).foldMap(Endo, Endo.empty()).run(html) // Solution 2 // const ex2 = html => [htmlFor, updateStyleTag, classToClassName].reduce((acc, curr) => acc.concat(Endo(curr)), Endo.empty()).run(html) // Solution 3 const ex1 = html => Endo(htmlFor) .concat(Endo(updateStyleTag)) .concat(Endo(classToClassName)) .run(html); QUnit.test("Ex1", assert => { const template = ` <div class="awesome" style="border: 1px solid red"> <label for="name">Enter your name: </label> <input type="text" id="name" /> </div> `; const expected = ` <div className="awesome" style={{border: 1px solid red}}> <label htmlFor="name">Enter your name: </label> <input type="text" id="name" /> </div> `; assert.deepEqual(expected, ex1(template)); }); // Ex2: model a predicate function :: a -> Bool and give it contramap() and concat(). i.e. make the test work // ========================= const Pred = run => ({ run, contramap(f) { return Pred(x => run(f(x))); }, concat(otherM) { return Pred(x => run(x) && otherM.run(x)); } }); // todo QUnit.test("Ex2: pred", assert => { const p = Pred(x => x > 4) .contramap(x => x.length) .concat(Pred(x => x.startsWith("s"))); const result = ["scary", "sally", "sipped", "the", "soup"].filter(p.run); assert.deepEqual(result, ["scary", "sally", "sipped"]); }); // Ex3: // ========================= const extension = file => file.name.split(".")[1]; const matchesAny = regex => str => str.match(new RegExp(regex, "ig")); const matchesAnyP = pattern => Pred(matchesAny(pattern)); // Pred(str => Bool) // TODO: rewrite using matchesAnyP. Take advantage of contramap and concat //const ex3 = file => // matchesAny('txt|md')(extension(file)) && matchesAny('functional')(file.contents) const ex3 = file => matchesAnyP("txt|md") .contramap(extension) .concat(matchesAnyP("functional").contramap(f => f.contents)) .run(file); QUnit.test("Ex3", assert => { const files = [ { name: "blah.dll", contents: "2|38lx8d7ap1,3rjasd8uwenDzvlxcvkc" }, { name: "intro.txt", contents: "Welcome to the functional programming class" }, { name: "lesson.md", contents: "We will learn about monoids!" }, { name: "outro.txt", contents: "Functional programming is a passing fad which you can safely ignore" } ]; assert.deepEqual([files[1], files[3]], files.filter(ex3)); });
相关文章
- Java -- JDBC 学习--调用函数&存储过程
- 前端学习 -- Html&Css -- 背景
- [AWS - DA] CLI & Policies
- [Java Spring] Controlling API Exposure & Override Endpoint Name
- [React] Broadcaster + Operator + Listener pattern -- 19. useBroadcaster & useListener
- [Angular & Unit Testing] Automatic change detection
- [ES6] 14. Generator -- 1. yield & next()
- 【IOS-COCOS2D-X 游戏开发之九】COCOS2DX利用CCSAXPARSER解析XML数据&CCMUTABLEDICTIONARY使用与注意!
- 【Teradata】配置PE和AMP(congfig和reconfig工具、vprocmanager)
- 前端学习 -- Html&Css -- 表格
- 前端学习 -- Html&Css -- 相对定位 绝对定位 固定定位
- [Angular] Reactive Form -- FormControl & formControlName, FormGroup, formGroup & formGroupName
- [Backbone]5. Model & View, toggle between Models and Views -- 2
- Veeam Backup & Replication 8 同步与恢复
- Shiro之UsernamePasswordToken&RememberMeAuthenticationToken&AuthenticationToken
- 华为OD机试 - 没有回文串(Java & JS & Python)
- BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第6章节--在SharePoint2013中开发、集成和构建应用程序 总结
- POJ 1459 && ZOJ 1734--Power Network【最大流dinic】
- Django 模型系统(model)&ORM--基础
- "围观"设计模式(7)--创建型之单例模式(Singleton Pattern)
- 树莓派玩耍笔记1 -- 开箱 & 安装系统以及简单配置
- (void)(&x==&y)
- 王立平--result += "{";
- 一 · 路由事件的三种方式/策略(冒泡 直接 隧道)
- 05.抽象工厂模式+反射--AbstractFactory&Reflect