• 0 Posts
  • 15 Comments
Joined 1 year ago
cake
Cake day: July 10th, 2023

help-circle
  • When the enum reaches your JSON, it will have to be a string (as JSON does not have a dedicated “enum” type). But it at least ensures that languages parsing your JSON will should have a consistent set of strings to read.

    Consider this small bit of Elm code (which you may not be an Elm dev, and thats okay, but it’s the concept that you should look to get):

    -- A Directions "enum" type with four options:
    -- North, East, South, West
    type Directions
        = North
        | East
        | South
        | West
    
    -- How to turn each Directions into a String
    -- Which can then be encoded in JSON
    directionsToString : Directions -> String
    directionsToString direction =
        case direction of
            North -> "north"
            East  -> "east"
            South -> "south"
            West  -> "west"
    
    -- "Maybe Directions" since not all strings can be parsed as a Directions.
    -- The return will be "Just <something>" or "Nothing"
    directionsFromString : String -> Maybe Directions
    directionsFromString dirString =
        case dirString of
            "north" -> Just North
            "east"  -> Just East
            "south" -> Just South
            "west"  -> Just West
            _       -> Nothing
    

    The two functions (directionsFromString and directionsToString) are ready to be used as part of JSON handling, to read a String from a key and turn it into a Directions enum member, or to turn a Directions to a String and insert the string to a key’s value

    But all that aside, for your restructuring, and keeping with the license plate example, both type and license number could be contained in a small object. For example:

    {
        ...
        "licensePlate": {
            "type": "government"    <- an enum in the language parsing this
                                       but a string in JSON
            "plateNumber": "ABC123"
            ...
        }
        ...
    }
    

  • If its something that represents mutually exclusive states, like the license plates examples (Gov’t, Embassy, Learner), an enum like 4wd mentioned is a better idea than many boolean keys. This would also be the switch/case question you posed. For a “regular case”, I would include that in the enum, but if you create an enum that only contains “special cases”, you can always set it to null.

    On the case of booleans, I would suggest avoiding them unless it is necessary, and truly a binary (as in, two-option, not binary numbers), self-contained-in-one-key thing (obligatory anti-boolean video). If the use case is to say what a different key’s object represents, you don’t need it (see: enums. You’ll thank yourself later if you add a third option). If the use case for using it is saying another key contains value(s), you don’t need it. Many languages can handle the idea of “data is present, or not present” (either with “truthy/falsey” behavior interpreting “data-or-null”, or “Maybe/Option” types), so often “data-or-null” can suffice instead of booleans.

    I would suggest trying to always include all keys of a present object, even if it’s value is null or not applicable. It will prevent headaches later when code might try to access that key, but it isn’t present. This approach might also help you decide to reduce the quantity of keys, if they could be consolidated (as in taking booleans and converting to a state-like enum, as mentioned above), or removed (if unused and/or deprecated).




  • Elm

    In short, it’s ruined my expectations of languages. It’s a functional language, like the style of Haskell, and transpiles to html or js (its meant for web). There’s very little that it allows for going wrong, and for things that could fail, it either tells you to port that out to JS and bring it back when you’re done, or you have to handle a Result type or Maybe type.

    It sounds strict, yes, but not having to deal with issues later is so nice.





  • Why does the for loop repeat after it exits to print a new line? If it exits the loop, shouldn’t it be done with it?

    There’s the new line after the for loop to make sure that the next recursion starts on a fresh line. Otherwise the next recursion would print on the same line, right where it left off, and you’d just have a line of “#”'s. The for loop itself is just for printing “#”'s.

    Why is n incremented and not i as stated with i++?

    I think this is a confusion with the recursion. Look at the line with draw(n - 1); this happens before any printing of hashes happens, and only continues after its done. And it calls itself as long as it’s not less than or equal to 0. To psuedo-code the order of operations here:

    draw 3 {
        draw 2 {
            draw 1 {
                draw 0 {};
                print "#" * 1;
            };
            print "#" * 2;
        };
        print "#" *3;
    };
    

    so n is never incremented as you think, it just calls decremented versions of the draw function before the current one finishes. The i’s are purely involved in the for loop, which only prints hashes. Does that make sense?


  • Although, i would agree with it not necessarily being “friendly”, since its a drastically different syntax than many beginners would be used to, the brackets and parenthesis here are not what you think they are.

    Unison is a language in the style of Haskell, F#, Purescript, Elm, etc. So that first line is actually type annotations.

    In Haskell, this would just be helloWorld :: IO () , meaning a function named “helloWorld” with no arguments and produces what is essentally a potentially-unsafe IO action with a Void return (the empty parenthesis () ).

    Here in Unison they call the bracket part “abilities” or something. Its saying the same thing as Haskell, but being more explicit in saying it can raise an exception.



  • Haskell for sure has a very sloped learning curve. The functional style, different syntax, a myriad of symbols and pragmas, as well as the tooling around it.

    The only reason I was able to pick it up as quick as I did was because I was used to Elm due to my job. Then it was just learning about the IO type (and how to use it), cabal, stack, built-in symbols, and the most common pragmas.

    But the symbols part is especially harsh, since symbols only hold meaning if they’re universally understood. Sure, the base- language ones are kinda adopted at this point, so they’ll stay, but the fact that external modules can also make symbols (sometimes without actually-named counterparts) adds some confusion. Like, would you just know what .: is supposed to mean off the top of your head?




  • Essentially, there is a massive pixel-divided canvas. It starts out pure-white.

    You are allowed to place one single pixel of a certain subset of colors every x minutes (I don’t remember how often, somewhere between 5 minutes to an hour).

    Your pixel(s) can be overridden by any other user who places their pixel on your space after you place yours.

    Some people come together to form collaborative art, messages, etc. (As seen here).

    In the past, there have been countries’ flags, a giant spreading black void, hidden Among Us beans, and many fandom-specific and subreddit-specific images.