alexanderjarvis / maybe Goto Github PK
View Code? Open in Web Editor NEWMaybe is a type that wraps optional values
License: MIT License
Maybe is a type that wraps optional values
License: MIT License
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
?
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)
)
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?
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.
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.
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.
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?
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!
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.