[Transducer] Create a Sequence Helper to Transduce Without Changing Collection Types
to create sequence collection types Without Helper Changing
2023-09-14 08:59:17 时间
A frequent use case when transducing is to apply a transformation to items without changing the type of the collection.
In this lesson, we'll create a helper to do just that. seq
will be similar to into
, except the target type will be inferred from the source collection. For example, if transducing from an array, seq
will create an array as the output collection as well.
seq
is thus more restrictive and easier to consume than into
.
The whole for 'seq' is we don't need to worry about collection type will different from target type. In short, input and output are the same type.
import {isPlainObject} from 'lodash'; import {compose, map, arrayReducer, objectReducer, transduce} from '../utils.js'; const into = (to, xf, collection) => { if (Array.isArray(to)) return transduce(xf, arrayReducer, to, collection); else if (isPlainObject(to)) return transduce(xf, objectReducer, to, collection); throw new Error('into only supports arrays and objects as `to`'); }; const seq = (xf, collection) => { if (Array.isArray(collection)) return transduce(xf, arrayReducer, [], collection); else if (isPlainObject(collection)) return transduce(xf, objectReducer, {}, collection); throw new Error('unsupported collection type'); }; seq(map(x => x*2), [1,2,3]); const flip = compose( map(([k,v]) => ({[v*10]:k})), ); seq(flip, {one: 1, two: 2, three: 3});
相关文章
- 【mysql】Could not create connection to database server. Attempted reconnect 3 times. Giving up.
- ORA-19884: datafile string is unable to use Unused Block Optimization because tablespace has been dropped ORACLE 报错 故障修复 远程处理
- ORA-22165: given index [string] must be in the range of [string] to [string] ORACLE 报错 故障修复 远程处理
- ORA-24405: error occured while trying to create connections in the pool ORACLE 报错 故障修复 远程处理
- ORA-29391: string and string must be mandatory to create a mandatory plan directive ORACLE 报错 故障修复 远程处理
- ORA-29663: Unable to find a method that matches one or more of the functions ORACLE 报错 故障修复 远程处理
- ORA-31633: unable to create master table “string.string” ORACLE 报错 故障修复 远程处理
- ORA-38466: user does not have privileges to CREATE/MODIFY expressions ORACLE 报错 故障修复 远程处理
- ORA-39222: Unable to create global temporary master table string ORACLE 报错 故障修复 远程处理
- MySQL Error number: 4040; Symbol: ER_ANONYMOUS_TO_GTID_UUID_SAME_AS_VIEW_CHANGE_UUID; SQLSTATE: HY000 报错 故障修复 远程处理
- ORA-00838: Specified value of MEMORY_TARGET is too small, needs to be at least stringM ORACLE 报错 故障修复 远程处理
- MySQL Error number: MY-010983; Symbol: ER_RES_GRP_SET_THR_AFFINITY_TO_CPUS_FAILED; SQLSTATE: HY000 报错 故障修复 远程处理
- MySQL Error number: MY-011605; Symbol: ER_GRP_RPL_FAILED_TO_CREATE_TRANS_CONTEXT; SQLSTATE: HY000 报错 故障修复 远程处理
- MySQL Error number: MY-011740; Symbol: ER_LDAP_AUTH_FAILED_TO_CREATE_OR_GET_CONNECTION; SQLSTATE: HY000 报错 故障修复 远程处理
- MySQL Error number: MY-011744; Symbol: ER_LDAP_AUTH_FAILED_TO_CREATE_LDAP_OBJECT_CREATOR; SQLSTATE: HY000 报错 故障修复 远程处理
- ORA-13211: failed to tessellate the window object ORACLE 报错 故障修复 远程处理
- ORA-13233: failed to create sequence number [string] for R-tree ORACLE 报错 故障修复 远程处理
- ORA-13990: Invalid tag name given to common tag callout function. ORACLE 报错 故障修复 远程处理
- ORA-14100: partition extended table name cannot refer to a remote object ORACLE 报错 故障修复 远程处理
- ORA-14455: attempt to create referential integrity constraint on temporary table ORACLE 报错 故障修复 远程处理
- ORA-16419: Snapshot standby must be converted to a physical standby database. ORACLE 报错 故障修复 远程处理
- undefined reference to symbol’ pthread_create@@GLIBC_2.2.5’详解程序员
- Python3 pip命令报错:Fatal error in launcher: Unable to create process using ‘”‘详解编程语言
- How to Use MySQL SEQ to Create Custom Sequences for Your Database(mysqlseq)
- Learning Linux: How to Use Signal Commands for Checking System Status(linux查看信号命令)
- 如何删除 Linux 上的软连接?How to delete soft links in Linux(删除软连接linux)