News for hackers. Stuff that matters.
Re:Using ‘switch’ to match arbitrary logic instead of string... by tumetab1 on Monday August 19, 2019 @07:04AM (#20736730)
In the end I think it's a option of style in which "if" and "switch" are neither the correct match.

The goal is to define a value (a variable or return value) in which a value is always established and it's established through several checks (exclusive or not).

In this case the goal is to the definitively define a config given a URL.

The "switch" and a default condition which I think express better that a value must be defined. With "ifs" we can add more complex logic and avoid the problematic "breaks" but we loose that in your face "default" condition.

In theory it would be better to have a different control structure like "pickJustOne":

  return pickJustOne(url) {
    githubUrl: config.github;
    twitterUrl: config.twitter;
    default: config.unknown;
  }
"pickJustOne" would have a companion named "pickOneByOrder" that would work like an "if" where the order of evaluation would matter.
Re:Using ‘switch’ to match arbitrary logic instead of string... by Razengan on Monday August 19, 2019 @07:51AM (#20736926)
EDIT: Oops, chalk this up as another case of reinventing the wheel. We already have all this in the form of the ?: ternary operator, as masklinn pointed out:

https://news.ycombinator.com/item?id=20736714

----

Inspired by this, I cooked up a crude imitation in (ugly/functional'ish?) Swift:

    let result = [(firstCheck,  1),
                  (secondCheck, 2),
                  (thirdCheck,  3)]
                  .first { $0.0() }?.1
That iterates over an array of (() - > Bool, Value) tuples and returns the second element of the first tuple whose first element is a function that returns true, otherwise nil. The types of all values must be the same. Could shorten .first to .firstTrue or something.

And a version that's closer to your idea:

    typealias BooleanFunction = () -> Bool
    typealias FunctionValuePair<ResultType> = (BooleanFunction, result: ResultType)

    func pickOne<ResultType> (_ pairs: FunctionValuePair<ResultType>...) -> ResultType? {
        pairs.first { function, result in
            function() == true
        }?.result
    }

    let result = pickOne(
        (firstCheck,  "one"),
        (secondCheck, "two"),
        (thirdCheck,  "three"))
        ?? "default"
You could make the default result an argument for pickOne so that it never returns nil, but I prefer to keep it an optional.

Though it's too early in my day to think of better names. :)

Re:Using ‘switch’ to match arbitrary logic instead of string... by cr0sh on Monday August 19, 2019 @03:11PM (#20740803)
Maybe something like a hash table/array could be used, predefined with the key being the URL and the value being the config, then the function could do something like return the value from the array given the key, with some kind of logic to return the unknown config by default if the key wasn't in the array (this would all depend on the language - it could be that such a method might be worse for readability; it would also depend on if the array could be defined elsewhere as an included file/module or something, etc).