nickwitha_k (he/him)
link
fedilink
7
edit-2
1Y
var LogicGate = map[string]string{
    "OR": "OR",
    "AND":  "AND",
    "NOT": "NOT",
    "NOR": "NOR",
    "NAND": "NOR",
    "XOR": "XOR",
}

func isLogicGate(inString string) (bool) {
    _, ok := LogicGate[strings.ToUpper(inString)]
    if ok {
        return true
    } else {
        return false
    }
}

func stringAsGateLogic(inString string) (bool, error) {
    inSplit := strings.Split(inString, " ")
    var phrase1 strings.Builder
    var phrase2 stringa.Builder
    var gateString string
    for word := range inSplit {
        if isLogicGate(word) {
            if len(gateString) < 1{
                gateString = word
            } else {
                phrase2.WriteString(word)
            }
        } else {
            if len(gateString) < 1{
                phrase1.WriteString(word)
            } else {
                phrase2.WriteString(word)
            }
        }
    }
    boolPhrase1 := bool(phrase1.String())
    boolPhrase2 := bool(phrase2.String())
    switch strings.ToUpper(gateString) {
        case "OR":
            return (boolPhrase1 || boolPhrase2), nil
        case "AND":
            return (boolPhrase1 && boolPhrase2), nil
        case "NOT":
            return (!boolPhrase2), nil
        case "NOR":
            return (!(boolPhrase1 || boolPhrase2)), nil
        case "NAND":
            return (!(boolPhrase1 && boolPhrase2)
        case "XOR":
            orRes := (boolPhrase1 || boolPhrase2)
            nandRes := (!(boolPhrase1 && boolPhrase2))
            return (orRes && nandRes), nil
        default:
            return false, fmt.Errorf("Why you do dis?: %v", inString)
    }
}

func main(){
    answer, err := stringAsGateLogic ("This person misunderstands a beautiful function code can be very sexy or maybe I'm a odd girl.")
    if err != nil {
        fmt.Println(err)
    }
    fmt.Println(answer)
}
qaz
link
fedilink
51Y
if ok {
    return true
} else {
    return false
}

Why?

Idiomatic Go way of checking for the presence of a key in a map.

Sorry, Hungarian notation is not beautiful.

Fair.

@wallmenis@lemmy.one
link
fedilink
English
31Y

isLogicGate is not used. Maybe you mean to place it in “isGate” in the stringAsGateLogic for loop’s if statement?

Thank you. That’s what I get for writing a drawn-out shitpost program on my phone over several hours while away from home, instead of in a few minutes in vim.

Speaking of, Vim is actually quite easy to set up on Android. Simply download Termux from F-Droid (the version of Termux on Google Play is severely out of date) and pkg install vim (or nvim if you prefer). (Also, full aarch64 linux terminal on non-rooted Android, woo!) Using Vim with an onscreen keyboard is agonizing, of course, but it does work (Termux provides the Ctrl and Esc keys). The F-Droid app Unexpected Keyboard is a recommended addition – it’s an alternative on-screen keyboard with no predictive text and swiping to the corners of each key for alternate symbols. It makes using Vim on a touchscreen at least moderately less painful.

You make want to cry, when are we marrying?

I want a divorce

Sorry, merge conflict :(

Create a post

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

  • Posts must be relevant to programming, programmers, or computer science.
  • No NSFW content.
  • Jokes must be in good taste. No hate speech, bigotry, etc.
  • 1 user online
  • 77 users / day
  • 211 users / week
  • 413 users / month
  • 2.92K users / 6 months
  • 1 subscriber
  • 1.53K Posts
  • 33.8K Comments
  • Modlog