[Compose] Isomorphisms and round trip data transformations

What is Isomorphisms?
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());
时间: 2024-12-28 14:34:28

[Compose] Isomorphisms and round trip data transformations的相关文章

POJ 1041 John&#39;s trip 无向图的【欧拉回路】路径输出

欧拉回路第一题TVT 本题的一个小技巧在于: [建立一个存放点与边关系的邻接矩阵] 1.先判断是否存在欧拉路径 无向图: 欧拉回路:连通 + 所有定点的度为偶数 欧拉路径:连通 + 除源点和终点外都为偶数 有向图: 欧拉回路:连通 + 所有点的入度 == 出度 欧拉路径:连通 + 源点 出度-入度=1 && 终点 入度 - 出度 = 1 && 其余点 入度 == 出度: 2.求欧拉路径 : step 1:选取起点(如果是点的度数全为偶数任意点为S如果有两个点的度数位奇数取一

POJ 1041 John&#39;s trip (无向图欧拉回路)

John's trip Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7433   Accepted: 2465   Special Judge Description Little Johnny has got a new car. He decided to drive around the town to visit his friends. Johnny wanted to visit all his frien

poj1041 John&#39;s trip,无向图求欧拉回路路径

点击打开链接 无向图求欧拉回路: 1.图连通 2.所有顶点的度数位偶数 #include <cstdio> #include <cstring> #include <stack> #include <queue> #include <algorithm> using namespace std; const int mt = 2000; const int ms = 50; bool vis[mt+5]; int g[ms][mt+5]; int

Open Data for Deep Learning

Open Data for Deep Learning Here you'll find an organized list of interesting, high-quality datasets for machine learning research. We welcome your contributions for curating this list! You can find other lists of such datasets on Wikipedia, for exam

John&#39;s trip

[题目描述] John想要拜访每位朋友,且每条道路他都只走一次. John希望从家里出发,拜访完所有朋友后回到自己的家,且总的路程最短.John意识到如果可以然后返回起点应该是最短的路径.写一个程序帮助John找到这样的路径.给出的每条街连接两个路口,最多有1995条街,最多44个路口.街编号由1到n, 路口分别编号1到m. 输入:每个用例一个数据块:每行表示一条街,由三个整数组成:x,y,z. z为这条街的编号,x和y表示这条街连接的两个路口的编号.(实际数据中可能是自环).John住在一个输

poj 1041 John&#39;s trip 欧拉回路

题目链接 求给出的图是否存在欧拉回路并输出路径, 从1这个点开始, 输出时按边的升序输出. 将每个点的边排序一下就可以. 1 #include <iostream> 2 #include <vector> 3 #include <cstdio> 4 #include <cstring> 5 #include <algorithm> 6 #include <cmath> 7 #include <map> 8 #include

Magical Data Modelling Framework for JSON

https://github.com/icanzilb/JSONModel New: In version 0.12.0 I added experimental support for exporting JSON models to CoreData. 最新消息:在0.12.0版本中,我试验性的支持将 JSON models 转化成 CoreData . Give it a try and let me know, post an issue or just get in touch. Tr

POJ 1041 John&#39;s trip Euler欧拉回路判定和求回路

就是欧拉判定,判定之后就可以使用DFS求欧拉回路了.图论内容. 这里使用邻接矩阵会快很多速度. 这类题目都是十分困难的,光是定义的记录的数组变量就会是一大堆. #include <cstdio> #include <cstring> #include <stack> #include <vector> using namespace std; struct Edge { int ed, des; Edge(int e = 0, int d = 0) : ed

poj1041 John&#39;s trip (无向图求欧拉回路方案)

John's trip Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5950   Accepted: 1946   Special Judge Description Little Johnny has got a new car. He decided to drive around the town to visit his friends. Johnny wanted to visit all his frien