Comments (6)
I've knocked up a PR that makes my change. It should actually be safe to apply, right? Unless it invalidates some laws. This is where things get woolly for me. 😊
from monocle-ts.
Hi,
AFAIK Optional
s are not "standard", they are a Monocle
thing. Based on the documentation I think that the current implementation is not lawful, I guess should be
/** generate an optional from a type and a prop which is a `Option` */
static fromProp<T extends { [K in P]: Option<any> }, P extends keyof T>(prop: P): Optional<T, T[P]['_A']> {
return new Optional<T, T[P]['_A']>(
s => s[prop],
- (a, s) => Object.assign({}, s, { [prop as any]: some(a) })
+ (a, s) => isSome(s[prop]) ? Object.assign({}, s, { [prop as any]: some(a) }) : s
)
}
i.e. you cannot insert or delete with an Optional
, only modify
from monocle-ts.
Ah. So if want to be able to set
something where there was None
before, I actually need to use Lens<X, Option<Y>>
instead of Optional<X, Y>
?
from monocle-ts.
I think so, however is pretty awkward
import { Lens, Optional, Prism } from '../src'
import { Option, some, none } from 'fp-ts/lib/Option'
interface Bar {
s: Option<string>
}
interface Foo {
bar: Option<Bar>
}
const foo: Foo = {
bar: some({
// s: some('blah')
s: none
})
}
const barLens = Lens.fromProp<Foo, 'bar'>('bar')
const sLens = Lens.fromProp<Bar, 's'>('s')
console.log(barLens.modify(obar => obar.map(bar => sLens.set(some('BLAH'), bar)), foo)) // { bar: Some({"s":{"value":"BLAH","_tag":"Some"}}) }
Based on this 3D https://groups.google.com/forum/#!topic/scala-monocle/i7Y4o0I7tIc we could define the some
prism
function getSomePrism<A>(): Prism<Option<A>, A> {
return new Prism<Option<A>, A>(
s => s,
a => some(a)
)
}
const sOptional = barLens.asOptional()
.compose(getSomePrism<Bar>().asOptional())
.compose(sLens.asOptional())
.compose(getSomePrism<string>().asOptional())
console.log(sOptional.set('WHOP', foo)) // { bar: Some({"s":{"value":"WHOP","_tag":"Some"}}) }
Also we could add a bunch of composeX
helpers so we can write
const sOptional = barLens
.composePrism(getSomePrism<Bar>())
.composeLens(sLens)
.composePrism(getSomePrism<string>())
as shown in the link
from monocle-ts.
Puts on reading glasses.
Thanks @gcanti !
from monocle-ts.
Trying to read through the comments and related ones in other issues, it does not appear Optional ever got the ability to set its target to none (i.e., clear the target). Was there a reason this wasn't implemented? I don't see discussion here or at #15 mentioning this, just a workaround involving a Lens<State, Option> rather than Optional<State, X>
from monocle-ts.
Related Issues (20)
- Is dissoc possible? HOT 2
- Are the experimental features safe to use in production HOT 1
- Lens props should not requires at least two argument HOT 1
- Lens set HOT 1
- iso.iso constructor is not equivalent to iso class constructor
- optional: prop for partial keys HOT 3
- Customize Monocle policy regarding missing interposed nodes?
- `Lens#composeAt` method?
- Tree structure HOT 1
- No fromProp method in docs. HOT 1
- fromTraversable(array) - array is deprecated
- How-to: select optional props from record. HOT 1
- learnings resources section
- insertAt & renameAt
- missing dependencies? HOT 1
- Lens.fromPath with array HOT 1
- Optic implemented using Kliesli Arrows HOT 1
- Question: lens to get the selected element form a dictionary HOT 1
- Question: Help with pipe syntax
- Is there any future for monocle-ts? HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from monocle-ts.