Skip to content

baijifeilong/IceSpringRealOptional

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

IceSpringRealOptional

Real Optional type in python, not @Nullable annotation.

Official sites

Features

  • All Java 8 style Optional API support
  • All Generic Type annotation support

Install

  • PyPI: pip install IceSpringRealOptional

Usage

from IceSpringRealOptional.maybe import Maybe

maybe = Maybe.ofNullable("CommonSense")
print("{}: isPresent={}".format(maybe, maybe.isPresent()))
print("{}: value={}".format(maybe, maybe.get()))
maybe.ifPresent(lambda x: print(f"{x} exist"))
print("{}'s length: {}".format(maybe, maybe.map(len)))

empty = Maybe.empty()
print(empty.orElse("{} is empty".format(empty)))
print(empty.orElseGet(lambda: "{} is empty again".format(empty)))

try:
    Maybe.empty().orElseThrow(lambda: RuntimeError("Unlucky"))
except RuntimeError as e:
    print("Runtime error caught: {}".format(e))

Example Output

<Maybe:CommonSense>: isPresent=True
<Maybe:CommonSense>: value=CommonSense
CommonSense exist
<Maybe:CommonSense>'s length: <Maybe:11>
<Maybe:None> is empty
<Maybe:None> is empty again
Runtime error caught: Unlucky

License

MIT

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages