[Compose] Isomorphisms and round trip data transformations

Posted Answer1215

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[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());

 

以上是关于[Compose] Isomorphisms and round trip data transformations的主要内容,如果未能解决你的问题,请参考以下文章

深入理解 Jetpack Compose 内核:SlotTable 系统

深入理解 Jetpack Compose 内核:SlotTable 系统

社区说 | Jetpack Compose 解析及实践

Compose 动画 : 高可定制性的动画 Animatable

RuntimeError: An attempt has been made to start a new process before the current process has...(代码片段

Docker--compose学习