Giter VIP home page Giter VIP logo

Comments (6)

leemhenson avatar leemhenson commented on June 8, 2024

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.

gcanti avatar gcanti commented on June 8, 2024

Hi,

AFAIK Optionals 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.

leemhenson avatar leemhenson commented on June 8, 2024

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.

gcanti avatar gcanti commented on June 8, 2024

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.

leemhenson avatar leemhenson commented on June 8, 2024

Puts on reading glasses.

Thanks @gcanti !

from monocle-ts.

kylegoetz avatar kylegoetz commented on June 8, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.