Is there an isomorphism between (subset of) category theory and relational algebra?

It comes from big data perspective. Basically, many frameworks (like Apache Spark) “compensate” lack of relational operations by providing Functor/Monad-like interfaces and there is a similar movement towards cats-to-SQL conversions (Slick in Scala). For instance, we need natural join (assuming no repetitions on indexes) for element-wise multiplication of vectors from SQL-perspective, which could be considered as zip + map(multiply) (Spark’s MLib, however, already has ElementwiseProduct) in Category Theory’s applications.

Simply saying (following examples are in Scala):

  • the referenced subcase of join can be thought as applicative functor (over sorted collection), which in its turn gives us zip: List(1,2,3).ap(List(2,4,8).map(a => (b: Int) => a * b)) –> (List(1,2,3) zip List(2,4,8)).map(x => x._1 * x._2). Moreover, we can induce it to some other joins, assuming some preprocessing (groupBy operator or just surjection, or generally – an epimorphism).

  • other joins and selection can be thought as monad. For instance, WHERE is just: List(1,2,2,4).flatMap(x => if (x < 3) List(x) else List.empty) –> List(1,2,2,4).filter(_ < 3)

  • data itself is just ADT (GADT too?), which in its turn looks like a simple Set-category (or more generally speaking – Cartesian-closed), so it should (I suppose) cover Set-based operations (due to Curry-Howard-Lambek itself) and also operations like RENAME (at least in practice).

  • aggregation corresponds to fold/reduce (catamorphism)

So, what I’m asking is can we build an isomorphism between (maybe subset of) category theory and (the whole) relational algebra or is there something uncovered? If it works, what exact “subset” of categories is isomorphic to relalgebra?

You can see that my own assumptions are quite broad while formal solutions like Curry-Howard-Lambek correspondence for logic-cats-lambda are more precise – so actually, I’m asking for a reference to an accomplished study (that shows a direct relationship) with more examples in Scala/Haskell.

Edit: the accepted answer made me think that I went too far representing joins and conditions as a monad (especially using an empty value that effectively instantiates FALSE), I think pullbacks should suffice at least for relalgebra subset of SQL. Monads are better for higher order (nesting) stuff like GROUP BY, which isn’t part of relalgebra.

Getting 2 divs aligned besides each other with margin between them in responsive only

I have some divs that I want aligned below each other in small resolutions (mobile) and 2 by 2 besides each other in bigger resolutions (tablet and desktop).

They are generated like this:

<div id="container">  <div class="card"></div> <div class="card"></div> <div class="card"></div> <div class="card"></div> <div class="card"></div>  </div> 

And the CSS is:

#container{   margin: 0 20px;   background-color:#ececec; } .card{   display: block;   position: relative;   margin-bottom: 15px;   background-color:#ffffff;   height:100px;   border:1px solid #c4c4c4; }  @media screen and (min-width: 767px) {      .card{     display: inline-block;     position: relative;     margin-bottom: 15px;     background-color:#ffffff;     height:100px;     width: calc(50% - 20px);     margin:5px;     border:1px solid #c4c4c4;   }  }  } 

Here’s a fiddle of how close I got: https://jsfiddle.net/dncjhzwg/

In mobile view, everything works fine. But as soon as I increase the window above 767 pixels, they are almost correctly displayed. The problem is I can’t get the margins to work.

I need there to be NO margins on the left and right of each of them and 10px margin in the middle. How do I achieve that?

What is the best way to handle glue between different classicly OOP objects at the higher levels of abstraction?

In object oriented programming, it is commonly said that each class/object/instance should have a well-defined purpose. For example, there may be a Window class in a windowing toolkit, a ChunkLoader class in a Minecraft-like game, or a Matrix<> class (template) in a linear algebra library. In each of these cases, the class has a specific job. In these examples the jobs, respectively, are to handle the underlying windowing api and expose useful functions, load and unload chunks, and represent a matrix with useful operations.

While this guideline (single purpose for each class) is good advice in many cases, it seems to fall flat for the glue parts of a program. That is, something needs to call the chunkloader, matrices, or window classes as needed. This can be shuttling data between them, setting them up with the appropriate polymorphic interfaces between them, or a host of other things that combine the aforementioned parts (ChunkLoader, etc.) into a whole. For example, in a Minecraft-like game something needs to handle the main loop and call the other classes with more defined purposes as needed.

While I am working on my programs, my first (and, so far, only) idea is to create classes that abstract larger areas of the program, and that deal with the less abstract classes inside them. This, however, tends to take the form of manager classes, in name or otherwise. For example, there may be a RenderingManager class in a video game that has a data member for each general part of the world (ie. a member to render the world itself, another for other players, and another for GUI). Then, within it, the GuiManager class that has its own members for various parts of the GUI (eg. inventory, health, pause screen).

This recursive manager structure, however, has several problems:

  • Managers (whether or not they are called managers) are bad practice in and of themself, according to the research that I have done before asking.
  • The manager classes are necessarily a particularly complex part of the program, as they touch many (or all) parts of the program, and thus need to keep in mind the idiosyncrasies of those parts. This can be partially mitigated by having clean interfaces to each layer of abstraction, but not all problems are always amicable to clean interfaces.
  • The manager classes are by their very nature hard to unit test, as, at the upper levels, they rely on huge swaths of the rest of the code. This requires essentially a complete program to run unit tests. Using polymorphism as a mitigation is undesirable as the interface to each manager class should completely hide the types with a lower level of abstraction, thus making passing pointers to other types impractical. Additionally, in languages such as C++, polymorphism isn’t available with basic members, meaning that the already-complex manager classes get more complex in order to enable unit tests.
  • This hierarchy of manager classes can be represented by a tree where each node is an instance, with a main manager as the root and the sub-managers as its children, and so on down to the most basic classes. Sometimes, it is necessary for a node to communicate with its cousin (eg. for the renderer to query the physics engine in a video game). There is no clear way to handle this in this design.

Is there some better technique for the large-scale organization of a program and/or to glue together the more basic OOP parts? If so, what is it? If not, how can I mitigate the aforementioned problems?

Can I draw a weapon between attacks?

Suppose a 5th level fighter with Extra Attack, armed with a greatsword and a javelin.

Extra Attack:

Beginning at 5th level, you can attack twice, instead of once, whenever you take the Attack action on your turn.

Suppose the following sequence of events:

  1. The fighter starts their turn already wielding their greatsword.
  2. The fighter uses their full movement speed to get to melee range of an enemy.
  3. The fighter uses their attack action to strike an enemy: The enemy is killed on the first strike.
  4. The nearest remaining enemy is 15 feet away.

So at this point, can the fighter draw their javelin as their free object interaction for the turn, then throw it at the nearby enemy as their extra attack?

I think this is okay: You can move between attacks, and moving feels like it should be a bigger deal mechanics-wise than a free object interaction.

Moving Between Attacks:

If you take an action that includes more than one weapon attack, you can break up your movement even further by moving between those attacks. For example, a fighter who can make two attacks with the Extra Attack feature and who has a speed of 25 feet could move 10 feet, make an attack, move 15 feet, and then attack again.

The dodgy bit might be letting go of the greatsword with the main hand to draw and throw the javelin. But I think Jeremy Crawford has that covered:

Jeremy Crawford:

A two-handed weapon needs two hands to be used, but not necessarily two to be carried.

Related comment for reaction spells sort of applies here too:

Simon Orion:

Would this cover Reaction Spells

Jeremy Crawford:

Yes

So it sounds like going from holding a weapon with two hands to holding it with one hand is a non-action.

So I think this checks out.

Just setting up a character that I want to focus on two handed weapons. I would like the ability to attack at range in the same round if I can get it.

The number having odd divisors between 2 numbers

I am trying to solve a problem from Hackerearth. I’m able to solve sample testcases but somehow when I am submitting the solution it gives time limit exceeded error or wrong output message.

Problem

Little Dipu is a small kid and like all the other kids, he likes to play, but he plays with numbers (He is extraordinary you know). Now-a-days Dipu has some extra interest in odd numbers. So, he says that a number N is interesting if it has odd number of divisors. Now Dipu turns to you and asks you to tell him how many interesting numbers are there between two given numbers, L and R (both inclusive).

Input

First line of input contains the number of test cases T. Each of the next T lines contains two space separated integers L and R.

Output

For each test case, output an integer, denoting the count of interesting numbers between L and R (both inclusive).

Constraints 1 <= T <= 100000  1 <= L <= R <= 1018  SAMPLE INPUT  2 1 3 6 10 SAMPLE OUTPUT  1 1 

Explanation

In 1st test case, 1 has 1 divisor, 2 has 2 divisors and 3 also has 2 divisors. So only 1 has odd number of divisors. Hence count of interesting numbers is 1.

In 2nd test case, 9 is the only number to have odd number of divisors. So answer is again 1.

My Code:

n = int(input())  for _ in range(n):     i,j = [int(k) for k in input().split(" ")]      s = 0     for l1 in range(i, j):         count = len([l1 for l2 in range(l1) if l1%(l2+1)==0])          if count%2 !=0:             s += 1      print(s) 

How can I improve above code?

Switching between DHCP and static IP from terminal (ubuntu 16.04)

I need to change regularly between DHCP and static IP. I usually do so by editing the file /etc/network/interfaces. Editing this is tiresome, and I want to automate so that I can do it command line.

One solution that would work for me is to have two different interfaces files, (interfaces-A) and (interfaces-B), which I then load into the network manager. The problem is I do not how to do so.

So the question is, what is the simplest to change IP settings between static and DHCP through terminal? (possibly through loading two different network-interface files)

Where is the difference between Acting and Role-Playing described?

I was sure I had read somewhere in the DMG that Role-Playing was something like

Following the personality, desires and fears of the character being played, even if they are not (logically) the best action.

For example, a priest in search of redemption might try to save people from a burning building, while an evil pyromaniac will laugh at the situation.

On the other hand, acting is role-playing taken a step further. This is described in the DMG, p6:

Players who enjoy acting like getting into character and speaking in their characters’ voices. Roleplayers at heart, they enjoy social interactions with NPCs, monsters, and their fellow party members.

That being said, I wanted to show my new group an official source that emphasized this difference. Is there any? Or did I just make this up in my head?