optics-dev / monocle Goto Github PK
View Code? Open in Web Editor NEWOptics library for Scala
Home Page: https://www.optics.dev/Monocle/
License: MIT License
Optics library for Scala
Home Page: https://www.optics.dev/Monocle/
License: MIT License
We should create a mailing list. Maybe a google group?
case class Person(name: String, age: Int)
object Person {
import lens.macro._
val NameLens = generateLensPerson, Int
}
haskell tree http://hackage.haskell.org/package/containers-0.5.0.0/docs/Data-Tree.html#t:Tree
Create root[A] : SimpleLens[Tree[A], A] and branches: SimpleLens[Tree[A], [A]] as in http://hackage.haskell.org/package/lens-4.1.2/docs/Data-Tree-Lens.html
I am not fan of oo as synonym for compose and I don't like that we need to define a new operator to create an AppliedLens (>-), AppliedTraversal (>--) and so on.
Same than tail except that it a Traversal toward all elements except the last one
I tried to add dependency to JodaTime but I am getting compile errors
e.g. int2Byte, int2Char, long2Int, ...
Generalize String prisms for Show/Read typeclasses,
and allow creation with manual functions.
"One can get the next/previous character with the safeCast[Char,Int] prism" in {
safeCast[Int,Char].getOption(safeCast[Int,Char].reverseGet('a') + 1) shouldEqual Some('b')
safeCast[Int,Char].reverseModify('a', _ + 1) shouldEqual Some('b')
safeCast[Int,Char].reverseModify('z', _ - 1) shouldEqual Some('y')
}
We could also define Char <-> Short Iso.
SimplePrism[A,B] represents a one-on-one function A => B and it's partial inverse function B => A.
We might get a couple of interesting things from limiting to SimplePrism.
We should probably define a stackoverflow tag so that users can post questions and we can easily find them. It looks like 'monocle' has already been defined, so maybe 'scala-monocle'.
Once a decision has been made, we should add it to the readme.
We should decide where to put our braces, what our indent size would be, how we would name classes, variables, and functions, and so forth. We should encoded these rules into the code formatter of our IDE's and stick with them.
Add Head, Tail, Each, Index, etc instances
Create Each, FilterIndex, Index, etc instances for Vector
This is going to be very similar to List instances
When we compose a Lens with a Prism we obtain a Traversal, so we lose the knowledge that we are zooming to 0-1 object instead of a 0-n objects (a Traversal).
trait Traversal[S, T, A, B]
trait NewConcept[S, T, A, B] extends Traversal[S, T, A, B] {
def getOption(from: S): A
}
trait Lens[S, T, A, B] extends NewConcept[S, T, A, B]
trait Prism[S, T, A, B] extends NewConcept[S, T, A, B]{
def re: Getter[B, T]
def reverseGet(from: B): T
}
This NewConcept should respect Traversal laws and getAll.size <= 1
What does everyone think about using https://huboard.com for Kanban?
The following example results in an NPE.
Hi Julien,
I see you are making it really nice and I decided to help out a bit.
I try to follow the README using the REPL and I find some minor issues.
case class Person(name: String, age: Int, address: Address)
case class Address(city: String, postcode: String, location: Location)
case class Location(latitude: Long, longitude: Long)
The order would be inverse in the REPL
case class Location(latitude: Long, longitude: Long)
case class Address(city: String, postcode: String, location: Location)
case class Person(name: String, age: Int, address: Address)
In the first example the "GET" I believe you are using a previous syntax
Person2CityLens.get(person, "Paris")
Probably should be
(AddressLens >- CityLens).get(person)
We can use head and tail on String or other ordered data structures
Add Iso for Int and List of digits.
@laytona We need a logo with a guy wearing a monocle and perhaps and moustache and a top hat.
I think we should put a lens example in the readme to give the guys a better idea of what the project all about.
Need to remove down casting for char bits i.e. (a1 | a2).asInstanceOf[Char]
Add Head, Tail, Each, Index, etc instances
Create a law sub module where we will define Lens, Travesal, etc laws with scalacheck.
Create a test sub module where for all Spec tests using law
e.g. _1.set((1, 3), 2) ==> (2,3)
_2.modify((2, "London"), reverse) ==> (2, "nodnoL")
Bonus task: think about easier syntax
@acachada mentioned that eclipse raised several compiler issues. At least, one is valid about the use of Macro inside of core should be forbidden.
stringToInt: SimplePrism[String, Int]
stringToDouble: SimplePrism[String, Double]
etc ...
Not a code issue, but it might be a good idea to do a lighting talk at an LSUG event. I'm sure if we speak to Andy, he would be up for it. The question is, what to include? Maybe just some features, examples and the diagram @julien-truffaut has done.
We should move to SBT 0.13.2, and enable name hashing, resulting in faster compilation.
I think Monocle's documentation may benefit from mentioning that strings for field names are checked for correctness at compile-time; for instance "_health"
matches the field name in Character
, from the Readme example val health = Macro.mkLens[Character, Int]("_health")
It may help to attract the "Strongly-typed not Stringly-typed" crowd :)
Each function should be in it's own package with a package object for importing,
to avoid mess created by many class declarations for implicits.
There should be a unified functions._ import.
Head for example takes O(n) time, instead O(1) for most cases.
One of the big causes for that is headOption in Fold.
I'll search for more of these soon.
The Lens
companion object has:
def apply[S, T, A, B](_get: S => A, _set: (S, B) => T): Lens[S, T, A, B]
so it would seem that it could be useful for Traversal
to have something like:
def apply[S, T, A, B](_getAll: S => List[A], _setAll: (S, List[B] => T): Traversal[S, T, A, B]
Actually, since this is something of a convenience method, it might be better to generalize it by allowing anything that can be traversed instead of just List
:
def apply[R[_]: Traverse, S, T, A, B](_getAll: S => R[A], _setAll: (S, R[B]) => T): Traversal[S, T, A, B]
Anyway, I'll have a pull request along shortly and you can decide whether this makes any sense :)
I think we should add to the read me the problem we are trying to solve i.e.
Before:
barbarian.set(address).set(country).set(leader).set(name).set("Fafnir")
After:
name.set(barbarian, "Fafnir")
What does everyone think?
e.g. http://www.haskellforall.com/2013/05/program-imperatively-using-haskell.html
This ticket will probably require to implement several features
I have cloned the project.
run sbt
$ project core
$ console
> import lens.Lens
> case class Location(latitude: Long, longitude: Long)
> case class Address(city: String, postcode: String, location: Location)
> case class Person(name: String, age: Int, address: Address)
> object AddressLens extends Lens[Person, Address]
<console>:14: error: object creation impossible, since:
it has 2 unimplemented members.
/** As seen from object AddressLens, the missing signatures are as follows.
* For convenience, these are usable as stub implementations.
*/
def get(from: Person): Address = ???
def lift[F[_]](from: Person,f: Address => F[Address])(implicit evidence$1: scalaz.Functor[F]): F[Person] = ???
object AddressLens extends Lens[Person, Address]
^
What am I doing wrong?
If I can run it myself I can fork and make a pull request with all the issues I find in the README.
head[A] : SimpleLens[List[A], Option[A]]
head.get(List(1,2,3)) == Some(1)
head.set(List(1,2,3), Some(0)) == List(0,2,3)
head.set(List(1,2,3), None) == List(2,3) // delete
head.set(Nil, Some(2)) == Nil
I defined a case class like this,
case class User(name: String, pass: String)
and I wanted to uppercase the name of an Option[User]. When I tried something like this:
Option(User("", "")) |->> each |->> _name ...
It worked without problems but then I tried:
Some(User("", "")) |->> each |->> _name ...
and I got the following compile error:
[error] both method mapEach in trait EachInstances of type [K, V]=> monocle.function.Each[Map[K,V],V]
[error] and method optEach in trait EachInstances of type [A]=> monocle.function.Each[Option[A],A]
[error] match expected type monocle.function.Each[S,A]
[error] println(user |->> each |->> _name modify (_.toUpperCase))
Check this repository for further information:
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.