本文介绍了Haskell的Lens库中`ix`和`at`有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



上下文:
我有一个数据结构 $ b $

您可以始终从 at :< ix / p>

  ixAt = at。遍历

定义,除非它使用(< ;. )用于合成以保持索引不变。 ( at 和 ix 都是索引镜头/遍历)。



焦点话题:镜头中的大多数运算符也都有中缀名称,您可以在


All I know is that one works and the other doesn't.

Context:I have one data structure F which contains a Data.Map.Map k S to another data structure S. My goal was to build a Lens that given an F and k would describe a field in S.

The difficulty is that the key k may not be present in the map. That's fine the function can wrap its return in Maybe. However I could not propagate a lens through a Maybe using at. After reading a lot of Stack Overflow answers, I came across this one.

It turns out that replacing at with ix solved my type problems if I also replaced (^.) with (^?).

Question:It seems like at and ix do the same thing, at least with regard to Map. Both take a key and give a 'Lens' to the value at that key. However, ix seems to play nice with the function composition operator (.). What is the difference between the two?


Off Topic Rant:

I like infix operators as much as the next guy but the Control.Lens package seems to have gone a little overboard. For a new user having some English names and a key somewhere would lower the learning curve. Due to the huge number of wrapper classes used in the Lens library it is particularly difficult to dig through the type signatures if you don't already know what is going on. My code is starting to look like Perl for heaven sake.

解决方案

That at and ix are different is already noticable if you look at the available instances for the classes containing these functions:

  • instances of At: Map, IntMap, HashMap
  • instances of Ixed: [a], Map, ByteString, Text, and a lot more

All instances if At are also an instance of Ix, but not all instances of Ix are also an instance of At.

So what's the difference between them? At is for containers that allow inserting keys that are not present in the container. This is obviously possible for a Map, but not e.g. for a list. To still be able to index into a list and change items that are there, Ix doesn't allow creating new items, but just "does nothing" when you try to write to a key that is not there.

>>> Data.Map.fromList [('a', 1)] & at 'b' .~ Just 4 
fromList [('a',1),('b',4)] -- Inserts value 
>>> Data.Map.fromList [('a', 1)] & ix 'b' .~ 4  
fromList [('a',1)]          -- Does nothing because key is not present

(There is also a shortcut for a .~ Just b, a ?~ b)

Technically, this difference comes from the fact that ix is a Traversal whereas at is a Lens. And because at is a Lens that "returns" a Maybe Something, you cannot compose it with a lens that takes just a plain "Something". ix is a Traversal with 0 or 1 values, so you can compose ix just like any other traversal (just like you can write traverse . traverse). (^?) just takes the first value (head) of that traversal.

You can always derive ix from at:

ixAt = at . traverse

The same definition is already in lens, except it's using (<.) for composition to keep the index from at. (at and ix are both indexed lenses / traversals).

Off-topic: Most operators from lens also have infix names, you can find a (incomplete) table at: https://github.com/ekmett/lens/wiki/Operators

这篇关于Haskell的Lens库中`ix`和`at`有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 07:25