[Functional Programming] Match Function Requirements with Lodash Partial and Flip
and with Function Programming match functional partial Flip
2023-09-14 09:00:47 时间
Functions returning functions returning functions can begin to look a bit unwieldy. The arrow function has helped the syntax a lot, but maybe using a curry utility function to combine all the arguments into a single function will help your syntax look a little cleaner. Currying isn't necessary for our pattern, but you'll definitely see it used in many patterns involving functions returning functions and maybe you'll grow to love it.
import { curry, partial, flip } from "lodash" let createTimeout = curry((time, listener) => { let id = setTimeout(listener, time) return () => { clearTimeout(id) } }) let addListener = curry((selector, eventType, listener) => { let element = document.querySelector(selector) element.addEventListener(eventType, listener) return () => { element.removeEventListener(eventType, listener) } }) let createInterval = curry((time, listener) => { let id = setInterval(listener, time) return () => { clearInterval(id) } }) //broadcaster = function that accepts a listener let merge = curry((broadcaster1, broadcaster2, listener) => { let cancel1 = broadcaster1(listener) let cancel2 = broadcaster2(listener) return () => { cancel1() cancel2() } }) let clickOrTick = merge( // addListener("#button", "click"), partial(window.addEventListener, "copy"), partial(flip(setInterval), 1000) //setInterval(()=> {}, 1000) ) let cancelClickOrTick = clickOrTick(() => { console.log("click or tick") }) cancelClickOrTick()
相关文章
- Git秘籍: Git and Git Flow Cheat Sheet
- ORA-24075: cannot specify agent with NULL address and non-NULL protocol ORACLE 报错 故障修复 远程处理
- MySQL Error number: MY-010465; Symbol: ER_RPL_BINLOG_MASTER_USES_CHECKSUM_AND_SLAVE_CANT; SQLSTATE: HY000 报错 故障修复 远程处理
- ORA-13062: topology IDs do not match in the feature table and the topology ORACLE 报错 故障修复 远程处理
- ORA-16112: log mining and apply stopping ORACLE 报错 故障修复 远程处理
- Unleashing the Power of MySQL and SQL AS(mysqlsqlas)
- Unleashing the Power of Oracle VIP: Enhancing Authentication and Security(oraclevip作用)
- Maximizing Performance with Effective MySQL Updates and Optimization(mysql更新优化)
- Mastering the Art of Process Management on Linux with C and Kill Commands(linuxckill)
- Exploring the Power of Linux with C Code: Tips Tricks and Best Practices(linuxc代码)
- Building Complex Database Applications Made Easy with Linux QT and SQL(linuxqtsql)
- Exploring the Latest Trends and Insights in Oracle ERP: Join the Forum Now!(oracleerp论坛)
- Oracle数据库中的AND查询(oracle and查询)
- and的区别Oracle数据库中的OR与AND的差异(oracle中or和)
- 语句使用Oracle两个WITH语句实现数据查询(oracle两个with)