GithubHelp home page GithubHelp logo

maybe's People

Contributors

benadamstyles avatar maartenschumacher avatar mendaomn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

maybe's Issues

forEach

Firstly, thank you! This has woken me up to a problem I knew I was encountering but couldn't define, and I'm excited to start using it.

Secondly โ€“ and this is purely stylistic, and a question rather than an issue โ€“ why did you go with foreach rather than forEach?

Flowtype and .filter()

I have been looking at the maybe source code and trying to work out how to improve the typing of the .filter() method but I haven't worked anything out. The problem is below. Is it possible to fix this within maybe or is flow not able to handle this anyway yet?

maybe(getUserType()) // => 'admin' | 'customer'
  .filter(userType => userType !== 'admin')
  .forEach((userType: 'customer') => // => flow complains that userType is 'admin' | 'customer'
    doSomethingOnlyCustomerCanDo(userType)
  )

Explicitly returning null in .map()

I am using maybe in React Native, in the props of a custom text component. The custom component should set the underlying Text component prop numberOfLines to 1 by default, or null if the user passes false into the custom component's truncate prop:

<BasicText
  numberOfLines={maybe(props.truncate)
    .filter(t => !t)
    .map(() => null)
    .orJust(1)}
/>

However, flow understandably complains about returning null in .map():

null (This type is incompatible with union: number | boolean | string | object type | array type
See also: call of method `map`

Is there an "idiomatic" way to handle this, or is it simply a case of putting an // $ExpectError comment and forgetting about it?

Update `maybe` to take a predicate and a value

Would it make sense to update maybe to take a predicate function instead of just checking for Nil? This would allow for more flexibility.

something like(in JS):

const maybe = (pred, value) =>
  pred(value) ? just(value) : nothing()

That way you let the user decide what Nothing is.

Consider explaining term "Just" in readme

Hi, I've never used a language with Maybe/Monads before and don't understand why a value is "Just" if it's defined. Nothing is intuitive but Just doesn't really make sense to me. I tried googling to find the historical precedent. I can see that it's the same terminology as Haskell but couldn't find an explanation of the etymology.

Could you add something to the README to explain why it's called just? Would help me wrap my head around it.

Only evaluating `.orJust()` return value when necessary

Sorry, me again!

I quite often find myself in something like the following situation:

return maybe(props.boolean)
  .filter(bool => bool)
  .map(() =>
    <ComponentIfTrue />
  )
  .orJust(
    <ComponentIfFalse />
  )

The trouble is, as I understand it, <ComponentIfFalse /> will always be evaluated, regardless of the value of props.boolean. Would there be the possibility of adding a new method called something like .orJustCall() to which you must pass a function, which is only called when the conditions lead to orJust? If so I'm happy to PR.

Unclear purpose

I got here from your medium article. I'm a long time javascript ninja and am a fan of cleanliness and efficiency. I've built a lot of applications, apis, and libraries. But I still don't really see the point.

This isn't a criticism of "maybe" itself (yet). On the contrary, I'm intrigued... (enough to write an issue instead of hitting the back button). I just feel like your examples in the readme are a bit basic and on the surface the only thing I see is that they result in more characters, and inevitably slower execution time than the "problem code".

And with any optional parameter, at some point you're going to want it to interact with the rest of your logic in a way that is dependant on what the parameter is. So at the end of the day, you're going to write if ( val.isJust() ){ ... } instead of if ( val != null || val !== undefined ){ ... } or even if ( !_.isEmpty( val ) ){ ... }

So... I'm not understanding something. Can you provide more concrete use-cases to demonstrate what problem this solves?

Wrapping multiple optional values

What would the maybe version of the following be?

let valueX = null
let valueY = null

if (valueX && valueY) {
  // do something with valueX and valueY
}

Would it be this?

let valueX = null
let valueY = null

maybe(valueX)
.map(() => valueY)
.foreach(() => {
  // do something with valueX and valueY
})

This doesn't seem very functional. Maybe I'm missing something super obvious. Thanks!

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.