scalaz中的商店是什么 [英] What is Store in scalaz

查看:106
本文介绍了scalaz中的商店是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图理解scalaz中的Lens(令人惊讶的是在cats-core中没有找到类似的东西),并且遇到了所谓的Store,它是类型别名:

I'm trying to understand Lenses in scalaz (surprisingly didn't find something similar in cats-core) and I came across the so called Store which is a type alias:

type StoreT[F[_], A, B] = IndexedStoreT[F, A, A, B]
type IndexedStore[I, A, B] = IndexedStoreT[Id, I, A, B]
type Store[A, B] = StoreT[Id, A, B]

哪里

final case class IndexedStoreT[F[_], +I, A, B](run: (F[A => B], I))

问题是如何治疗这种类型?该文档仅引用Lens es.有人可以用几个词解释一下吗?

The question is how to treat this type? The documentation just referes to Lenses. Can someone give an explanation in a few words?

对我来说,它看起来类似于State monad,其中状态转换"使用函数F[A => B]

To me it looks similar to State monad where the "state transition" is storing with function F[A => B]

推荐答案

一个Store[S,A]是一个充满A s的结构,由S索引,其中一个独特的S作为光标"进入结构.

A Store[S,A] is a structure full of As, indexed by S, with a distinguished S as a kind of "cursor" into the structure.

要回答这是什么?"这个问题,看看它支持什么操作是最有启发性的.

To answer the question "what is it?", it's most instructive to look at what operations it supports.

您可以要求光标的位置:

You can ask for the position of the cursor:

_.pos : Store[S,A] => S

您可以浏览"光标下方的值:

You can "peek" at the value under the cursor:

_.peek : Store[S,A] => A

您可以搜索"移动光标:

And you can "seek" to move the cursor:

_ seek _ : (Store[S,A], S) => Store[S,A]

将其视为维度为S的数组,在该数组中有一个索引s:S,可以移动索引.

Think of it as an array of dimensions S, where you have an index s:S into the array, and you can move the index.

例如,Store[(Int,Int), Byte]是二维256色位图.您可以peek使用光标下方像素的颜色(以字节表示),然后可以使用seek将光标移动到其他像素.

For example, Store[(Int,Int), Byte] is a two-dimensional 256-colour bitmap. You can peek at the colour (represented by a byte) of the pixel under the cursor, and you can move the cursor to a different pixel using seek.

Store[S,_]也是逗号.这意味着它支持以下操作:

Store[S,_] is also a comonad. This means it supports the following operations:

map : (A => B) => (Store[S,A] => Store[S,B])
extend : (Store[S,A] => B) => (Store[S,A] => Store[S,B])
duplicate : Store[S,A] => Store[S, Store[S, A]]

map表示您可以使用函数来更改存储中的所有值.

map means you can change all the values in the store using a function.

s.extend(f)采用本地"计算f,该计算在S的特定位置在商店上进行操作,并将其扩展为在每个位置在商店上进行操作的全局"计算.在位图示例中,如果您具有函数mean(store),该函数取store中紧接光标周围的像素的平均值,则store.extend(mean)将对整个图像执行高斯滤波.新图像中的每个像素将是原始图像中该位置紧邻像素周围像素的平均值.

s.extend(f) takes a "local" computation f, that operates on the store at a particular location of S, and extends it to a "global" computation that operates on the store at every location. In the bitmap example, if you have a function mean(store) that takes the average of the pixels immediately surrounding the cursor in the store, then store.extend(mean) will perform a Gaussian filter on the whole image. Every pixel in the new image will be an average of the pixels immediately surrounding the pixels at that location in the original image.

s.duplicate为您提供了一个充满Store[S,A]Store[S,Store[S,A]],在每个位置S都有原始Store[S,A]的副本,其光标设置在该位置S.

s.duplicate gives you a Store[S,Store[S,A]] full of Store[S,A]s, that at every location S has a copy of the original Store[S,A] with its cursor set to that location S.

StoreState dual .在引擎盖下,State[S,A]实际上是S => (A, S),而Store[S,A]实际上是(S => A, S):

Store is the dual of State. Under the hood, State[S,A] is really S => (A, S), and Store[S,A] is really (S => A, S):

State[S,A] ~= S => (A, S)
Store[S,A] ~= (S => A, S)

两者均由两个函子S => _(_, S)组成.如果以一种方式编写它们,则会得到State[S,_].如果以其他方式编写它们,则会得到Store[S,_].

Both are composed of the two functors S => _ and (_, S). If you compose them one way, you get State[S,_]. If you compose them the other way, you get Store[S,_].

我曾对此进行过几次讨论:

I gave a talk about this a couple of times:

https://www.youtube.com/watch?v=FuOZjYVb7g0

可以利用这种二元性的一种很酷的方法是,如果您拥有商店和状态机,它们将相互消灭.商店驱动状态机,然后状态机从商店中选择一个值.

A cool way you can exploit this duality is that if you have a store and a state machine, they annihilate each other. The store drives the state machine, and in turn the state machine picks a value from the store.

def zap[S,A,B](state: State[S,A], store: Store[S,B]): (A,B) = {
  val (a, s) = state.run(store.pos)
  (a, store.seek(s).peek)
}

这篇关于scalaz中的商店是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆