Simple exemple with Lense & Optional & Transversal ?

527 views
Skip to first unread message

Ronan Michaux

unread,
Jun 3, 2015, 5:22:14 PM6/3/15
to scala-...@googlegroups.com
Hello,

I'm trying to learn Monocle, so I modified the readme.md exemple :

 
  @Lenses case class Street(name: String, nr: Int)
 
@Lenses case class Address(street: Street, city: Option[String])
 
@Lenses case class Company(address: List[Address], name: String)
 
@Lenses case class Employee(company: Company, id: String)


But I don't know to use Transerval & Optional to manipulate this new version of Employee.

I'm looking for some code exemple.

Regards,
Ronan.

Julien Truffaut

unread,
Jun 4, 2015, 4:54:29 PM6/4/15
to scala-...@googlegroups.com, ronan....@gmail.com
Hi Ronan,

Typically you want to use `some: Prism[Option[A], A]` (defined in `monocle.std.option`) to zoom into an `Option`. 
For a `List`, it all depends which elements you want to update, if it is all elements, then you can use `each: Traversal[List[A], A]` (defined in `monocle.function`). Note: `each` is more generic than this, there is an instance for most scala std collection.

So for example if you want to modify all cities you could do:
`(Company.address composeTraversal each composeLens Address.city composePrism some).set("London")(company)`

Please tell me if you have any other questions, you can also come to the gitter channel.

Cheers,

Ronan Michaux

unread,
Jun 5, 2015, 4:56:14 PM6/5/15
to scala-...@googlegroups.com, ronan....@gmail.com
I've tried your exemple :

  @Lenses case class Street(name: String, nr: Int)
 
@Lenses case class Address(street: Street, city: Option[String])
 
@Lenses case class Company(address: List[Address], name: String)
 
@Lenses case class Employee(company: Company, id: String)


  val company1
= Company(List(Address(Street("street1", 1), Some("city1"))), "acme")

 
import monocle.std.option._
 
import monocle.function._
 
 
(Company.address composeTraversal each composeLens Address.city composePrism some).set("London")(company1)


But I've got this compile error :

ambiguous implicit values:
both method optEach
in trait OptionInstances of type [A]=> monocle.function.Each[Option[A],A]
and method someEach in trait OptionInstances of type [A]=> monocle.function.Each[Some[A],A]
match expected type monocle
.function.Each[S,A]


I don't understand my mistake ?

Regards.

Julien Truffaut

unread,
Jun 5, 2015, 5:25:22 PM6/5/15
to Ronan Michaux, scala-...@googlegroups.com
I can understand, the error message is very unclear. The problem is not that you an ambiguous implicit but that you don't have an instance of `Each[List[A], A]` in scope.

All List instances are in `monocle.std.list` so if you add the following import it should work: `import monocle.std.list._`

--
You received this message because you are subscribed to the Google Groups "Scala-Monocle" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-monocl...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Julien Truffaut
Tel :  +41 792317831

Ronan Michaux

unread,
Jun 5, 2015, 5:43:18 PM6/5/15
to scala-...@googlegroups.com, ronan....@gmail.com
Thank you !
Reply all
Reply to author
Forward
0 new messages