zipper

Zipper Comonads, Generically

自闭症网瘾萝莉.ら 提交于 2019-11-26 07:55:08
问题 Given any container type we can form the (element-focused) Zipper and know that this structure is a Comonad. This was recently explored in wonderful detail in another Stack Overflow question for the following type: data Bin a = Branch (Bin a) a (Bin a) | Leaf a deriving Functor with the following zipper data Dir = L | R data Step a = Step a Dir (Bin a) deriving Functor data Zip a = Zip [Step a] (Bin a) deriving Functor instance Comonad Zip where ... It is the case that Zip is a Comonad though

Cleaner way to update nested structures

我怕爱的太早我们不能终老 提交于 2019-11-26 01:48:23
问题 Say I have got following two case class es: case class Address(street: String, city: String, state: String, zipCode: Int) case class Person(firstName: String, lastName: String, address: Address) and the following instance of Person class: val raj = Person(\"Raj\", \"Shekhar\", Address(\"M Gandhi Marg\", \"Mumbai\", \"Maharashtra\", 411342)) Now if I want to update zipCode of raj then I will have to do: val updatedRaj = raj.copy(address = raj.address.copy(zipCode = raj.address.zipCode + 1))