json – Proper recursive rendering in React

I’m trying to render a recursive list from a json file. Could you please suggest a proper way to render children (and their children etc.)?
Those elements are stored in entry.children

By now I can just render parent elements like this:

import React, {useState, useEffect} from "react"
import {useSpring, animated, config} from 'react-spring'

function App() {

    const loadedData = require('json!./data.json');
    const (entries, setEntries) = useState(loadedData.projectTreeData.mainProjectTreeInfo.rootProjectchildrenildren)
    const (clicked, setClicked) = useState(-1)
    const (hovered, setHovered) = useState(-1)
    const props = useSpring({fontSize: 42, from: {fontSize: 24}})

    const handleMouseEnter = i => e => {
        setHovered(i)
    }

    function handleMouseLeave(){
        setHovered(-1)
    }

    return (

        <div>
        <br/>         

        {entries.map ((entry, i) => 

            <animated.div 
            style={useSpring({ config: { mass: 0.07 }, fontSize: i == hovered ? 50 : 30 , from: {fontSize: 24}   })}> 

            <a              
            key={i} 
            onMouseEnter={handleMouseEnter(i)}
            onMouseLeave={handleMouseLeave}
            > 

            {entry.name}    </a> <br/> 
            </animated.div>)} 

        </div>
    )
}


export default App

It is clear for me that there has to be a loop, but how to build it properly?
Many thanks!

command line – Bash Recursive Script Failure

I build the script that is mandatory for use in GUI interface (for a reason).
The script is designed to relaunch itself in gnome terminal in all conditions.

The basic stub of the script is looking like that:

if (( $1 = "-tl" ))
then
    read VARIABLE
    
    #PERFORM SOME AWESOME STUFF :)

else
    gnome-terminal -x bash -c "${PWD}/the-script -tl"
fi

The script by default might be launched by “Execute in Terminal” command of Nautilus etc. or by the command line (see shell).

Once the script is launched, it designed to relaunch itself back in Gnome Terminal in any conditions.

The problem goes while the script is mooved outside the user directory, it runs and stops immidiately, popping up the terminal window for a second (while correctly relaunched, it must at least stop by the read variable command).

Using string like that achieves same result:

gnome-terminal -x bash -c "./the-script -tl"

Making the bash directly treat the argument as bash script name instead of command, gives correct result outside user directory, but has broken functionality back in user dir:

gnome-terminal -x bash "${PWD}/the-script" -tl

It turns out that both solutions are not universal!

proof techniques – Recurrence relation for the number of “references” to two mutually recursive function

I was going through the Dynamic Programming section of Introduction to Algorithms(2nd Edition) by Cormen et. al. where I came across the following recurrence relations in the assembly line scheduling portion.


$(1),(2),(3)$ are three relations as shown.

$$f_{1}(j) = begin{cases}
e_1+a_{1,1} &quadtext{if } j=1\
min(f_1(j-1)+a_{1,j},f_2(j-1)+t_{2,j-1}+a_{1,j})&quadtext{if} jgeq2\
end{cases}tag 1$$

Symmetrically,

$$f_{2}(j) = begin{cases}
e_2+a_{2,1} &quadtext{if } j=1\
min(f_2(j-1)+a_{2,j},f_1(j-1)+t_{1,j-1}+a_{2,j})&quadtext{if} jgeq2\
end{cases}tag 2$$

(where $e_i,a_{i,j},t_{2,j-1}$ are constants for $i=1,2$ and $j=1,2,3,…,n$)

$$f^star=min(f_1(n)+x_1,f_2(n)+x_2)tag 3$$


The text tries to find the recurrence relation of the number of times $f_i(j)$ ($i=1,2$ and $j=1,2,3,…,n$) is referenced if we write a mutual recursive code for $f_1(j)$ and $f_2(j)$. Let $r_i(j)$ denote the number of times $f_i(j)$ is referenced.

They say that,

From $(3)$,

$$r_1(n)=r_2(n)=1.tag4$$

From $(1)$ and $(2)$,

$$r_1(j)=r_2(j)=r_1(j+1)+r_2(j+1)tag 5$$


I could not quite understand how the relations of $(4)$ and $(5)$ are obtained from the three corresponding relations.

Thought I could make out intuitively that as there is only one place where $f_1(n)$ and $f_2(n)$ are called, which is in $f^star$, so probably in $(4)$ we get the required relation.

But as I had not encountered such concept before I do not quite know how to proceed. I would be grateful if someone guides me with the mathematical prove of the derivation as well as the intuition, however I would prefer an alternative to mathematical induction as it is a mechanical cookbook method without giving much insight into the problem though (but if in case there is no other way out, then I shall appreciate mathematical induction as well provided the intuition is explained to me properly).

linux – create recursive snapshots with BTRFS

I’m working on a shell script (just with btrfs default tools), which should do recursive snapshots of all subvolumes. My BTRFS looks like that and each directory is a subvolume. In the .snapshots directory are the snaps stored from the parent directory.

── .snapshots
├── sub1
│   ├── .snapshot
│   └── subsub1
│       └── .snapshot
├── sub2
│   ├── .snapshot
│   └── subsub2
│       └── .snapshot
└── sub3
    ├── .snapshot
    └── subsub3
        └── .snapshot

My problem

Imagine, there was a file added in subsub3 and you want to create a new snapshot from the whole BTRFS root and save it in .snapshots, then I see empty files and only from the direct childs. That means there is no directory sub3/subsub3 in the .snapshots directory.

with sudo -n btrfs subvolume list /path I can see all subvolumes and it’s possible to filter with awk '{print $2}' all IDs, but what’s the best way for recursion?

Can you give me a hint?

sql – Working around lack of recursive CTE in Presto

I’m specifically stuck with a task, where you can think of a spreadsheet with several columns and several rows. A cell does a calculation which includes using the value of the cell to the left of it, i.e. a column step to the left, on the same row. A very simple thing to accomplish in spreadsheets, and it’s easy to do in most common programming languages, but it seems like it’s a hard (?) thing for SQL to do, unless there is support for recursive CTE. I need to do this in Presto.

Presto does not (so far) support recursive CTE:s. Specifically, in a WITH I need to reference that same WITH from itself (I think from a JOIN).

Are there any common workarounds for this problem? Any other nifty tools for the job?

Some reference links

recursion – How to deail with a hypothetical situation in which a pub/sub cycle gets into an unending recursive loop?

Let me explain what I mean. Imagine A subs to event b. In such case A pubs event a. B subs to event a. In this case B subs b. This is a full-blown circle. How does a pub/sub cycle deal with such fringe case? I haven’t tested it because I’m yet to write my own pub/sub engine in Java or JavaScript. In fact I’m not sure if I understand it correctly that’s why nothing in Google came up after I searched fr these keywords “pubsub” + “loop” + “recursion”.

Let me draw an ASCII diagram.

A <-----------> b
|               |
|               |
|               |
a <---------->  B

Recursive Matrix Multiplication Algorithm

Can I improve on this any further. Is there a prettier way of passing the array sizes?

#include <stdio.h>

void matrix_addition(size_t n, int A(n)(n), int B(n)(n), int C(2*n)(2*n), size_t c_start, size_t c_end) {
    for(size_t i = 0; i < n; ++i) {
        for(size_t j = 0; j < n; ++j) {
            C(i+c_start)(j+c_end) = A(i)(j) + B(i)(j);
        }
    }
}

void recursive_matrix_multiply(
        size_t l, size_t n, int A(l)(l), int B(l)(l), int C(n)(n),
        size_t a_r_start, size_t a_r_end, size_t a_c_start, size_t a_c_end, 
        size_t b_r_start, size_t b_r_end, size_t b_c_start, size_t b_c_end
    ) {
    if(n == 1) {
        C(0)(0) = A(a_r_start)(a_c_start)*B(b_r_start)(b_c_start);
    } else {
        int m = n/2;
        int C1(m)(m);
        int C2(m)(m);

        recursive_matrix_multiply(l, m, A, B, C1, a_r_start, a_r_end-m, a_c_start, a_c_end-m, b_r_start, b_r_end-m, b_c_start, b_c_end-m);
        recursive_matrix_multiply(l, m, A, B, C2, a_r_start, a_r_end-m, a_c_start+m, a_c_end, b_r_start+m, b_r_end, b_c_start, b_c_end-m);
        
        matrix_addition(m, C1, C2, C, 0, 0);
        
        recursive_matrix_multiply(l, m, A, B, C1, a_r_start, a_r_end-m, a_c_start, a_c_end-m, b_r_start, b_r_end-m, b_c_start+m, b_c_end);
        recursive_matrix_multiply(l, m, A, B, C2, a_r_start, a_r_end-m, a_c_start+m, a_c_end, b_r_start+m, b_r_end, b_c_start+m, b_c_end);

        matrix_addition(m, C1, C2, C, 0, m);

        recursive_matrix_multiply(l, m, A, B, C1, a_r_start+m, a_r_end, a_c_start, a_c_end-m, b_r_start, b_r_end-m, b_c_start, b_c_end-m);
        recursive_matrix_multiply(l, m, A, B, C2, a_r_start+m, a_r_end, a_c_start+m, a_c_end, b_r_start+m, b_r_end, b_c_start, b_c_end-m);

        matrix_addition(m, C1, C2, C, m, 0);

        recursive_matrix_multiply(l, m, A, B, C1, a_r_start+m, a_r_end, a_c_start, a_c_end-m, b_r_start, b_r_end-m, b_c_start+m, b_c_end);
        recursive_matrix_multiply(l, m, A, B, C2, a_r_start+m, a_r_end, a_c_start+m, a_c_end, b_r_start+m, b_r_end, b_c_start+m, b_c_end);
    
        matrix_addition(m, C1, C2, C, m, m);
    }
}


int main()
{
    int A(4)(4) = {{1, 2, 3, 4}, {4, 3, 2, 1}, { 0, 0, 1, 1 }, {1, 1, 0, 0}};
    int B(4)(4) = {{2, 2, 4, 4}, {4, 1, 1, 4}, {1, 0, 1, 0}, {1, 0, 1, 0}};
    int C(4)(4);
    
    recursive_matrix_multiply(4, 4, A, B, C, 0, 3, 0, 3, 0, 3, 0, 3);

    for(size_t i = 0; i < 4; ++i) {
        for(size_t j = 0; j < 4; ++j) {
            printf("C(%i)(%i) = %i, ", i, j, C(i)(j));
        }
        printf("n");
    }
    
    return 0;
}

It currently works as intended but I’m looking to beautify this a bit. Even an improvement on variable naming would be appreciated. I’m trying to build up a portfolio so if you saw this from a professional standpoint what would you think?

google sheets – Creating a Recursive Formula in a single cell?

I have searched Google and stack exchange extensively and cannot find a solution that fits my needs.

I have a recursive formula in a google spreadsheet that can be represented as

F(0) = 0

F(1) = 5000

F(n) = F(n-1) + ((n-1) * 5000)

I can effectively calculate this with a long two column table with n values in column A and the recursive formula in column B which references the previous cell when needing to access F(n-1).

This approach is clunky, takes up space, and requires a separate table for every different calculation. In my sheet I need to calculate this formula for different values of F(0) and it requires a separate table for each one.

What I want is a simple single cell with a formula that references another cell for the n value and calculates the result recursively all in the background, instead of taking up a whole set of space in a table somewhere in my sheet for it to do the recursion through multiple cells.

My spreadsheet to make it clearer:

https://docs.google.com/spreadsheets/d/16kWw9iCvxExseNLdHQBWVGWybvDJgl8EHI9oDibbDSw/edit?usp=drivesdk

Is this possible?

programming languages – Question regarding definition of recursive function

According to Wikipedia, and also a very common definition of a recursive function found in several books, “functions that call themselves from within their own code”. I agree that this solves the problem for most of what I think recursive functions are, but, suppose you have the following “iterative” code written in C:

int foo(){
    return 2;
}

The unique pourpose of this function is to compute the value “2”. Can we say this is also a “recursive” function? Saying it only relies in a base case. I can see the function isn’t calling itself in their body, even though, I can’t find a way to compute it in another way, using the self-calling part.

Besides, I’m aware of the fact that every iterative function has a recursive implementation, so, in this case this function does have the same implementation for the iterative and for the recursive view?