[Compose] Isomorphisms and round trip data transformations
2017-01-23 15:15
295 查看
What is Isomorphisms?
We have a value x, then apply function 'to' and 'from' to value 'x', the result we should still get 'x'.
So Isomorphisms is kind of opreation able to tranform a value back and forward without lose anything.
Example1:
Example2:
Example3:
We have a value x, then apply function 'to' and 'from' to value 'x', the result we should still get 'x'.
// from(to(x)) == x // to(from(y)) == y
So Isomorphisms is kind of opreation able to tranform a value back and forward without lose anything.
Example1:
const Iso = (to, from) => ({ to, from }) // String <-> [Chat] const StoC = Iso( (str) => str.split(''), (chat) => chat.join('') ); const res = StoC.from(StoC.to('How'));
Example2:
// String <-> [Chat] const StoC = Iso( (str) => str.split(''), (chat) => chat.join('') ); const truncate = (str, num) => StoC.from(StoC.to(str).slice(0,num)).concat('...'); let res = truncate("Hello World!", 7); console.log(res); // "Hello W..."
Example3:
const Iso = (to, from) => ({ to, from }) // [a] <-> Either/null/a const singleton = Iso( (either) => either.fold(() => [], x => [x]), ([x]) => x? Right(x): Left() ) const filterEither = (e, pred) => singleton.from(singleton.to(e).filter(pred)); const res = filterEither(Right('hello'), (x) => x.match(/h/ig)) .map(x => x.toUpperCase());
相关文章推荐
- codeforces round 396 div2 E Mahmoud and a xor trip 按位操作+dp子树内外
- Data obtained from ping: is it round trip or one way?
- Codeforces Round #365 (Div. 2) 703B Mishka and trip 水题
- Codeforces Round #416 (Div. 2) C - Vladik and Memorable Trip
- 【Codeforces Round 365 (Div 2)B】【容斥】Mishka and trip 环加完全点图的边权乘积和
- Codeforces Round #396 (Div. 2) E. Mahmoud and a xor trip
- Codeforces Round #365 (Div. 2) B. Mishka and trip
- Codeforces Round #416 (Div. 2) C Vladik and Memorable Trip
- Codeforces Round #396 (Div. 2) E. Mahmoud and a xor trip dfs 按位考虑
- 支付宝 口碑ISV开发:sign check fail: check Sign and Data Fail!JSON also!
- QuickFlow中控制DataField and TaskField(更简单的控制列表项和任务字段值)
- printf - format and print data
- Can not write to cache files, please check directory ./data/ and ./data/cache/ . 的解决方法
- MSL(Max Segment Lifetime) TTL(Time to Life) RTT(Round-Trip Time)
- Data Structures and Algorithm Analysis in C 学习之List
- CF Round#324 (Div2) E. Anton and Ira
- CodeForces Round #179 (295A) - Greg and Array 一个线段树做两次用
- Distributed transactions with multiple databases, Spring Boot, Spring Data JPA and Atomikos
- Codeforces Round #324 (Div. 2)——D. Dima and Lisa
- Basics of Cube Aggregates and Data Rollup