optutils
optutils
Usage
What it does basically:
some_val := ?string("something here")
x := if some_val {
return x
} else {
return ""
}
it help you to test whether there is anything in a
option
Let's start with a basic example, unwrap a value in option.
unwrap_or
none
or
or
import optutils
fn main() {
some_val := ?string(none)
val2 := optutils.unwrap_or(some_val, "")
println(val2)
// ""
}
unwrap_or_other
none
or
import optutils
fn main() {
some_val2 := ?string(none)
// unwrap string option, if string option is none
// return int 1
option_2 := optutils.unwrap_or_other(some_val2, 1)
if option_2 is string {
val3 := string(option_2)
println("string: ${val3}")
} else if option_2 is int {
val4 := int(option_2)
println("int: ${val4}")
}
// int: 1
}
unwrap_or_default
import optutils
struct TestStruct {
a string
b int
c bool
d f64
e []string
}
fn main() {
some_val3 := ?TestStruct(none)
val5 := optutils.unwrap_or_default(some_val3)
println(val5)
// TestStruct{
// a: ''
// b: 0
// c: false
// d: 0.0
// e: []
// }
some_val4 := ?string(none)
val6 := optutils.unwrap_or_default(some_val4)
println(val6)
// NIL
println(val6 == "")
// true
}
unwrap_then
optutils.unwrap_or_other
optutils.unwrap_then(x, fn(y))
result
import optutils
fn main() {
some_val5 := ?int(0)
val7 := optutils.unwrap_then(some_val5, fn (x int) bool {
if x == 0 {
return true
} else {
return false
}
})!
println(val7)
// true
}
try_map
import optutils
fn main() {
some_arr := [0, 2, 4, 6, 8]
map_val := optutils.try_map(some_arr, fn (x int) int {
y := x/2
return y
})
println(map_val)
// Option[0, 1, 2, 3, 4]
}
unwrap_try_map
try_map
import optutils
fn main() {
some_arr := [0, 2, 4, 6, 8]
umap_val := optutils.unwrap_try_map(some_arr, fn (x int) int {
y := x/2
return y
})
println(umap_val)
// [0, 1, 2, 3, 4]
}
try_filter
bool
import optutils
fn main() {
some_arr := [0, 2, 4, 6, 8]
filter_val := optutils.try_filter(some_arr, fn (x int) bool {
return x/2 > 1
})
println(filter_val)
//Option[4, 6, 8]
}
unwrap_try_filter
try_filter
import optutils
fn main() {
some_arr := [0, 2, 4, 6, 8]
ufilter_val := optutils.unwrap_try_filter(some_arr, fn (x int) bool {
return x/2 > 1
})
println(ufilter_val)
// [4, 6, 8]
}
is_equal
option
true
false
import optutils
fn main() {
some_val := ?int(10)
some_val2 := ?int(11)
some_val3 := ?int(none)
some_val4 := ?int(10)
eq := optutils.is_equal(some_val, some_val2)
eq2 := optutils.is_equal(some_val, some_val4)
println("some_val equal some_val2? ${eq}")
// false
println("some_val equal some_val4? ${eq2}")
// true
}
is_some
option
true
false
import optutils
fn main() {
some_val := ?int(10)
some := optutils.is_some(some_val)
println("got something? ${some}")
// true, it is something there
}
is_none
option
true
false
import optutils
fn main() {
some_val := ?int(10)
nothing := optutils.is_none(some_val3)
println("got nothing? ${nothing}")
// true, it is nothing there
}
Deprecated
unwrap
option
or
fn main() {
some_val := ?string("hi, there!")
val := some_val?
val := optutils.unwrap(some_val)!
val := optutils.unwrap(some_val) or { "the other value" }
println(val)
// hi, there!
// however, we can do this directly.
val := some_val or {"the other value"}
}
try_unwrap
or {}
fn main() {
some_val := ?string(none)
val := some_val or {"alternative"}
val := optutils.try_unwrap(none_val) or {"alternative"}
println(val)
// alternative
}
due to the duplication, I recommend to use
?
or
Using it
Install via vpm
v install jaar23.optutils
Install via Github
v install --git https://github.com/jaar23/optutils
import in your code
import optutils