1. 6
  1.  

  2. 5

    This is perhaps too basic for lobste.rs?

    Also, it brings to mind the set type in Nim, which is very nice syntactic sugar around this. (And one sign of Nim’s Pascal heritage.) But it does cause confusion for newbies who mistake it for a general purpose set type and then ask why set[string] is a syntax error…

    1. 1

      If one were to put this into a database (RDBMS), how would query complexity get affected? Could one query for Tuesdays without having to do a full table scan? Not sure if indexes would help given one must use a bitwise function in the WHERE.

      1. 4

        This can be done efficiently if your database allows indexes on expressions, as SQLite does. Something like create index Tuesday on Foo ((days & 4) != 0).

        I believe you can also use a WHERE clause to cut down the size of the index, but I’m not sure how exactly to write that.

        1. 3

          That’s only an answer to half of the question, it misses the ‘without having to do a full table scan’ part. You can express it in SQL, but if you want to do queries over the day of the week then you typically want the database to provide an index that allows it to avoid reading any of the entries that weren’t a particular value. If you have a single value then an SQL ENUM lets you express this and tell the database to build an index so that you can cheaply query for a specific day. If you want to support any day, then you need to use a richer type. Most databases appear to provide a bitfield type, so you could use that, but I don’t know what the indexing functionality is.

          It’s been ages since I did any database work, but my guess is that the right way of expressing this is actually to have a normalised database that provides one table for each day of the week with a foreign key referencing the thing. You could then do those queries with a JOIN. You might also want tables of the negation ({thing} is not on {day}) so that you can do queries over events that are only on Monday, for example. It’s then up to the database to decide how to efficiently encode this.

          1. 1

            The index does avoid a table scan, at least in SQLite. A query in which the WHERE clause is a conjunction involving ((days & 4) != 0) can use that index to guide it, so the engine will scan only the nonzero section of the index.

            Of course you do need seven indexes to support every day of the week.