Merge branch 'devel' of github.com:nim-lang/Nim into devel

This commit is contained in:
Andreas Rumpf 2017-09-28 10:35:59 +02:00
commit 05c6cb5f9f
2 changed files with 60 additions and 23 deletions

View file

@ -67,10 +67,8 @@
## assert(false) # This will not be reached ## assert(false) # This will not be reached
## except UnpackError: # Because an exception is raised ## except UnpackError: # Because an exception is raised
## discard ## discard
import typetraits import typetraits
type type
Option*[T] = object Option*[T] = object
## An optional type that stores its value and state separately in a boolean. ## An optional type that stores its value and state separately in a boolean.
@ -78,7 +76,6 @@ type
has: bool has: bool
UnpackError* = ref object of ValueError UnpackError* = ref object of ValueError
proc some*[T](val: T): Option[T] = proc some*[T](val: T): Option[T] =
## Returns a ``Option`` that has this value. ## Returns a ``Option`` that has this value.
result.has = true result.has = true
@ -88,14 +85,12 @@ proc none*(T: typedesc): Option[T] =
## Returns a ``Option`` for this type that has no value. ## Returns a ``Option`` for this type that has no value.
result.has = false result.has = false
proc isSome*[T](self: Option[T]): bool = proc isSome*[T](self: Option[T]): bool =
self.has self.has
proc isNone*[T](self: Option[T]): bool = proc isNone*[T](self: Option[T]): bool =
not self.has not self.has
proc unsafeGet*[T](self: Option[T]): T = proc unsafeGet*[T](self: Option[T]): T =
## Returns the value of a ``some``. Behavior is undefined for ``none``. ## Returns the value of a ``some``. Behavior is undefined for ``none``.
assert self.isSome assert self.isSome
@ -110,12 +105,11 @@ proc get*[T](self: Option[T]): T =
proc get*[T](self: Option[T], otherwise: T): T = proc get*[T](self: Option[T], otherwise: T): T =
## Returns the contents of this option or `otherwise` if the option is none. ## Returns the contents of this option or `otherwise` if the option is none.
if self.isSome: if self.has:
self.val self.val
else: else:
otherwise otherwise
proc map*[T](self: Option[T], callback: proc (input: T)) = proc map*[T](self: Option[T], callback: proc (input: T)) =
## Applies a callback to the value in this Option ## Applies a callback to the value in this Option
if self.has: if self.has:
@ -123,12 +117,27 @@ proc map*[T](self: Option[T], callback: proc (input: T)) =
proc map*[T, R](self: Option[T], callback: proc (input: T): R): Option[R] = proc map*[T, R](self: Option[T], callback: proc (input: T): R): Option[R] =
## Applies a callback to the value in this Option and returns an option ## Applies a callback to the value in this Option and returns an option
## containing the new value. If this option is None, None will be returned ## containing the new value. If this option is None, None will be returned.
if self.has: if self.has:
some[R](callback(self.val)) some[R](callback(self.val))
else: else:
none(R) none(R)
proc flatten*[A](self: Option[Option[A]]): Option[A] =
## Remove one level of structure in a nested Option.
if self.has:
self.val
else:
none(A)
proc flatMap*[A, B](self: Option[A], callback: proc (input: A): Option[B]): Option[B] =
## Applies a callback to the value in this Option and returns an
## option containing the new value. If this option is None, None will be
## returned. Similar to ``map``, with the difference that the callback
## returns an Option, not a raw value. This allows multiple procs with a
## signature of ``A -> Option[B]`` (including A = B) to be chained together.
map(self, callback).flatten()
proc filter*[T](self: Option[T], callback: proc (input: T): bool): Option[T] = proc filter*[T](self: Option[T], callback: proc (input: T): bool): Option[T] =
## Applies a callback to the value in this Option. If the callback returns ## Applies a callback to the value in this Option. If the callback returns
## `true`, the option is returned as a Some. If it returns false, it is ## `true`, the option is returned as a Some. If it returns false, it is
@ -138,13 +147,11 @@ proc filter*[T](self: Option[T], callback: proc (input: T): bool): Option[T] =
else: else:
self self
proc `==`*(a, b: Option): bool = proc `==`*(a, b: Option): bool =
## Returns ``true`` if both ``Option``s are ``none``, ## Returns ``true`` if both ``Option``s are ``none``,
## or if they have equal values ## or if they have equal values
(a.has and b.has and a.val == b.val) or (not a.has and not b.has) (a.has and b.has and a.val == b.val) or (not a.has and not b.has)
proc `$`*[T](self: Option[T]): string = proc `$`*[T](self: Option[T]): string =
## Returns the contents of this option or `otherwise` if the option is none. ## Returns the contents of this option or `otherwise` if the option is none.
if self.has: if self.has:
@ -152,7 +159,6 @@ proc `$`*[T]( self: Option[T] ): string =
else: else:
"None[" & T.name & "]" "None[" & T.name & "]"
when isMainModule: when isMainModule:
import unittest, sequtils import unittest, sequtils
@ -220,3 +226,30 @@ when isMainModule:
check(some(456).filter(proc (v: int): bool = v == 123).isNone) check(some(456).filter(proc (v: int): bool = v == 123).isNone)
check(intNone.filter(proc (v: int): bool = check false).isNone) check(intNone.filter(proc (v: int): bool = check false).isNone)
test "flatMap":
proc addOneIfNotZero(v: int): Option[int] =
if v != 0:
result = some(v + 1)
else:
result = none(int)
check(some(1).flatMap(addOneIfNotZero) == some(2))
check(some(0).flatMap(addOneIfNotZero) == none(int))
check(some(1).flatMap(addOneIfNotZero).flatMap(addOneIfNotZero) == some(3))
proc maybeToString(v: int): Option[string] =
if v != 0:
result = some($v)
else:
result = none(string)
check(some(1).flatMap(maybeToString) == some("1"))
proc maybeExclaim(v: string): Option[string] =
if v != "":
result = some v & "!"
else:
result = none(string)
check(some(1).flatMap(maybeToString).flatMap(maybeExclaim) == some("1!"))
check(some(0).flatMap(maybeToString).flatMap(maybeExclaim) == none(string))

View file

@ -1443,6 +1443,10 @@ elif defined(genode):
importcpp: "genodeEnv->parent().exit(@); Genode::sleep_forever()", importcpp: "genodeEnv->parent().exit(@); Genode::sleep_forever()",
header: "<base/sleep.h>".} header: "<base/sleep.h>".}
elif defined(nodejs):
proc quit*(errorcode: int = QuitSuccess) {.magic: "Exit",
importc: "process.exit", noreturn.}
else: else:
proc quit*(errorcode: int = QuitSuccess) {. proc quit*(errorcode: int = QuitSuccess) {.
magic: "Exit", importc: "exit", header: "<stdlib.h>", noreturn.} magic: "Exit", importc: "exit", header: "<stdlib.h>", noreturn.}