Giter Club home page Giter Club logo

stdx.nu's People

Contributors

1kinoti avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

amtoine

stdx.nu's Issues

sharing thoughts about the package

i've had a look at this package and it looks really cool, from a ton of aspects ๐Ÿ‘ ๐Ÿ‘

however, it's quite slow to parse ๐Ÿค”
i know it's the parser's fault but in an interactive use right now, it's not super nice...

in this context, i've written the super straightforward following modules ๐Ÿ˜‹

Result

# nothing -> record<type: string, error: string>
export def Err [error: any] {{type: Err, error: $error}}
# nothing -> record<type: string, val: any>
export def Ok [val?: any] {{type: Ok, val: $val}}

export def unwrap-result [] {
    let input = $in
    match $input {
        {type: Ok, val: $val} => $val,
        {type: Err, error: $error} => {error make --unspanned {msg: $error}},
        _ => {
            error make --unspanned {
                msg: $"Input of unwrap is not an Option: '($input)' of type ($input | describe)"
            }
        },
    }
}

export def is-ok [] {
    let input = $in
    match $input {
        {type: Ok, val: $_} => true,
        {type: Err, error: $_} => false,
        _ => {
            error make --unspanned {
                msg: $"Input of is-ok is not a Result: '($input)' of type ($input | describe)"
            }
        },
    }
}

export def is-err [] {
    let input = $in
    match $input {
        {type: Ok, val: $_} => false,
        {type: Err, error: $_} => true,
        _ => {
            error make --unspanned {
                msg: $"Input of is-err is not a Result: '($input)' of type ($input | describe)"
            }
        },
    }
}

Option

# nothing -> record<type: string>
export def None [] {{type: None}}
# nothing -> record<type: string, val: any>
export def Some [val?: any] {{type: Some, val: $val}}

export def unwrap-option [] {
    let input = $in
    match $input {
        {type: Some, val: $val} => $val,
        {type: None} => {error make --unspanned {msg: "Unwrap on None variant of Option"}},
        _ => {
            error make --unspanned {
                msg: $"Input of unwrap is not an Option: '($input)' of type ($input | describe)"
            }
        },
    }
}

export def is-none [] {
    let input = $in
    match $input {
        {type: Some} => false,
        {type: None} => true,
        _ => {
            error make --unspanned {
                msg: $"Input of is-none is not an Option: '($input)' of type ($input | describe)"
            }
        },
    }
}

export def is-some [] {
    let input = $in
    match $input {
        {type: Some} => true,
        {type: None} => false,
        _ => {
            error make --unspanned {
                msg: $"Input of is-none is not an Option: '($input)' of type ($input | describe)"
            }
        },
    }
}

and some simple testing

use std assert
use Option [None, Some, unwrap-option, is-none, is-some]

assert equal (None) (None)
assert not equal (None) (Some 123)
assert equal (Some 123) (Some 123)

assert (None | is-none)
assert not (None | is-some)

assert (Some 123 | is-some)
assert not (Some 123 | is-none)

assert equal (Some 123 | unwrap-option) 123
assert error {None | unwrap-option}

assert error {123 | is-some}
assert error {123 | is-none}
assert error {123 | unwrap-option}
use Result [Ok, Err, unwrap-result, is-err, is-ok]

assert equal (Err "my error") (Err "my error")
assert not equal (Err "my error") (Ok 123)
assert equal (Ok 123) (Ok 123)

assert (Err "my error" | is-err)
assert not (Err "my error" | is-ok)

assert (Ok 123 | is-ok)
assert not (Ok 123 | is-err)

assert equal (Ok 123 | unwrap-result) 123
assert error {Err "my error" | unwrap-result}

assert error {123 | is-ok}
assert error {123 | is-err}
assert error {123 | unwrap-result}

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.