## functional programming – Split string at given positions

Given a vector `<Vec<usize>>` of indices indicating where to split a string as follows:

``````println!("{:?}", idxs);
(2, 1, 6, 2, 5, 2, 2, 1)
``````

And the following string (The Series column in the Bureau of Labor Statistics’ data):

`let mut sstr = "JTS000000000000000JOR";`

I wrote a recursive function to split the string as follows:

``````fn split_str(cur: &str, rest: &str, idxs: Vec<usize>, mut res: Vec<String>) -> Vec<String> {
if idxs.len() > 1 {
//println!("cur: {} idx: {} rest: {}", cur, *idxs.first().unwrap(), rest);
res.push(cur.to_owned());
let (cur, rest) = rest.split_at(*idxs.first().unwrap());
split_str(cur, rest, idxs(1..).to_vec(), res)
}
else {
res.push(cur.to_string());
//println!("{}", rest);
res(1..).to_vec()
}
}
``````

The method is run as follows:

``````let r = split_str(&sstr(0..idxs(0)), &sstr((idxs(1)+1)..), idxs(1..).to_vec(), vec!("".to_string()));
``````

which correctly returns

``````("JT", "S", "000000", "00", "00000", "00", "JO", "R")
``````

How can I optimize this code? I’m used to Scala and it feels like I’m forcing Rust into a pattern it wasn’t designed for, but I don’t want to revert to traditional loops.

## functional analysis – Determining all real functions \$g=frac{y}{x}\$, where \$y\$ is an involution

Suppose that we have the following functional equation
$$y(x)=x ,g(x)$$
where both $$y$$ and $$g$$ are functions over the reals. In addition, we want $$y(x)$$ to be an
involution, i.e. $$y(y(x))=x$$, for some interval of the real line $$mathbb R$$. Is there a straightforward way to determine all possible functions $$g(x)$$ for which $$y(x)$$ is an involution? (At least given some specific interval in the reals).

I am not sure whether this question is trivial or extremely simple, but I was not able to work out
an answer or find one in any reference.

## functional analysis – Inequality involving bounded mean oscillation(BMO) function

I want to prove

$$||f|-|f|_B|leq|f-f_B|+||f||_{BMO}$$

where $$f_B=frac 1{m(B)}int_B f(x)dx$$, and $$f_{BMO}=supfrac1{m(B)}int_B |f(x)-f_B|dx where $$sup$$ is taken over all balls in $$mathbb{R^d}$$.
I naively attempted triangle inequality $$||f|-|f|_B|leq |f|+|f|_Bleq |f-f_B|+2|f|$$ or $$|f-f_B|+2|f|_B$$ but this doesn’t seem to like a good estimate. Can anyone give me any insight on this? Thanks!

## object oriented – Is there any way to make my JavaScript code more logical and efficient? How to convert functional to OOP in JS?

I created some tooltips using `javascript` but after writing the code I found it a lot more complicated and hard to read/understand is there any way to shortened my code and make it more logical and efficient.

``````var tooltip = (function () {
'use strict';
const PREFIX = 'data-tooltip';
const SUFFIX = ('pointer-direction', 'position', 'content');
const REQUIRED_ATTRIBUTES = ();

SUFFIX.forEach((item) => {
REQUIRED_ATTRIBUTES.push(PREFIX + '-' + item);
});

const POSITIONS = (
'top-left-corner',
'top-center',
'top-right-corner',
'right-center',
'bottom-right-corner',
'bottom-center',
'bottom-left-corner',
'left-center',
'middle',
);

const DIRECTIONS = (
'top-start',
'top',
'top-end',
'right-start',
'right',
'right-end',
'bottom-start',
'bottom',
'bottom-end',
'left-start',
'left',
'left-end',
);

DIRECTIONS.forEach((direction, position) => {
if (direction === 'top-start') {
if (POSITIONS(position) === 'top-left-corner') {
} else if (POSITIONS(position) === 'top-center') {
} else if (POSITIONS(position) === 'top-right-corner') {
} else if (POSITIONS(position) === 'right-center') {
} else if (POSITIONS(position) === 'bottom-right-corner') {
} else if (POSITIONS(position) === 'bottom-center') {
} else if (POSITIONS(position) === 'bottom-left-corner') {
} else if (POSITIONS(position) === 'left-center') {
} else if (POSITIONS(position) === 'middle') {
}
} else if (direction === 'top') {
if (POSITIONS(position) === 'top-left-corner') {
} else if (POSITIONS(position) === 'top-center') {
} else if (POSITIONS(position) === 'top-right-corner') {
} else if (POSITIONS(position) === 'right-center') {
} else if (POSITIONS(position) === 'bottom-right-corner') {
} else if (POSITIONS(position) === 'bottom-center') {
} else if (POSITIONS(position) === 'bottom-left-corner') {
} else if (POSITIONS(position) === 'left-center') {
} else if (POSITIONS(position) === 'middle') {
}
} else if (direction === 'top-end') {
if (POSITIONS(position) === 'top-left-corner') {
} else if (POSITIONS(position) === 'top-center') {
} else if (POSITIONS(position) === 'top-right-corner') {
} else if (POSITIONS(position) === 'right-center') {
} else if (POSITIONS(position) === 'bottom-right-corner') {
} else if (POSITIONS(position) === 'bottom-center') {
} else if (POSITIONS(position) === 'bottom-left-corner') {
} else if (POSITIONS(position) === 'left-center') {
} else if (POSITIONS(position) === 'middle') {
}
}

if (direction === 'right-start') {
if (POSITIONS(position) === 'top-left-corner') {
} else if (POSITIONS(position) === 'top-center') {
} else if (POSITIONS(position) === 'top-right-corner') {
} else if (POSITIONS(position) === 'right-center') {
} else if (POSITIONS(position) === 'bottom-right-corner') {
} else if (POSITIONS(position) === 'bottom-center') {
} else if (POSITIONS(position) === 'bottom-left-corner') {
} else if (POSITIONS(position) === 'left-center') {
} else if (POSITIONS(position) === 'middle') {
}
} else if (direction === 'right') {
if (POSITIONS(position) === 'top-left-corner') {
} else if (POSITIONS(position) === 'top-center') {
} else if (POSITIONS(position) === 'top-right-corner') {
} else if (POSITIONS(position) === 'right-center') {
} else if (POSITIONS(position) === 'bottom-right-corner') {
} else if (POSITIONS(position) === 'bottom-center') {
} else if (POSITIONS(position) === 'bottom-left-corner') {
} else if (POSITIONS(position) === 'left-center') {
} else if (POSITIONS(position) === 'middle') {
}
} else if (direction === 'right-end') {
if (POSITIONS(position) === 'top-left-corner') {
} else if (POSITIONS(position) === 'top-center') {
} else if (POSITIONS(position) === 'top-right-corner') {
} else if (POSITIONS(position) === 'right-center') {
} else if (POSITIONS(position) === 'bottom-right-corner') {
} else if (POSITIONS(position) === 'bottom-center') {
} else if (POSITIONS(position) === 'bottom-left-corner') {
} else if (POSITIONS(position) === 'left-center') {
} else if (POSITIONS(position) === 'middle') {
}
}

if (direction === 'bottom-start') {
if (POSITIONS(position) === 'top-left-corner') {
} else if (POSITIONS(position) === 'top-center') {
} else if (POSITIONS(position) === 'top-right-corner') {
} else if (POSITIONS(position) === 'right-center') {
} else if (POSITIONS(position) === 'bottom-right-corner') {
} else if (POSITIONS(position) === 'bottom-center') {
} else if (POSITIONS(position) === 'bottom-left-corner') {
} else if (POSITIONS(position) === 'left-center') {
} else if (POSITIONS(position) === 'middle') {
}
} else if (direction === 'bottom') {
if (POSITIONS(position) === 'top-left-corner') {
} else if (POSITIONS(position) === 'top-center') {
} else if (POSITIONS(position) === 'top-right-corner') {
} else if (POSITIONS(position) === 'right-center') {
} else if (POSITIONS(position) === 'bottom-right-corner') {
} else if (POSITIONS(position) === 'bottom-center') {
} else if (POSITIONS(position) === 'bottom-left-corner') {
} else if (POSITIONS(position) === 'left-center') {
} else if (POSITIONS(position) === 'middle') {
}
} else if (direction === 'bottom-end') {
if (POSITIONS(position) === 'top-left-corner') {
} else if (POSITIONS(position) === 'top-center') {
} else if (POSITIONS(position) === 'top-right-corner') {
} else if (POSITIONS(position) === 'right-center') {
} else if (POSITIONS(position) === 'bottom-right-corner') {
} else if (POSITIONS(position) === 'bottom-center') {
} else if (POSITIONS(position) === 'bottom-left-corner') {
} else if (POSITIONS(position) === 'left-center') {
} else if (POSITIONS(position) === 'middle') {
}
}

if (direction === 'left-start') {
if (POSITIONS(position) === 'top-left-corner') {
} else if (POSITIONS(position) === 'top-center') {
} else if (POSITIONS(position) === 'top-right-corner') {
} else if (POSITIONS(position) === 'right-center') {
} else if (POSITIONS(position) === 'bottom-right-corner') {
} else if (POSITIONS(position) === 'bottom-center') {
} else if (POSITIONS(position) === 'bottom-left-corner') {
} else if (POSITIONS(position) === 'left-center') {
} else if (POSITIONS(position) === 'middle') {
}
} else if (direction === 'left') {
if (POSITIONS(position) === 'top-left-corner') {
} else if (POSITIONS(position) === 'top-center') {
} else if (POSITIONS(position) === 'top-right-corner') {
} else if (POSITIONS(position) === 'right-center') {
} else if (POSITIONS(position) === 'bottom-right-corner') {
} else if (POSITIONS(position) === 'bottom-center') {
} else if (POSITIONS(position) === 'bottom-left-corner') {
} else if (POSITIONS(position) === 'left-center') {
} else if (POSITIONS(position) === 'middle') {
}
} else if (direction === 'left-end') {
if (POSITIONS(position) === 'top-left-corner') {
} else if (POSITIONS(position) === 'top-center') {
} else if (POSITIONS(position) === 'top-right-corner') {
} else if (POSITIONS(position) === 'right-center') {
} else if (POSITIONS(position) === 'bottom-right-corner') {
} else if (POSITIONS(position) === 'bottom-center') {
} else if (POSITIONS(position) === 'bottom-left-corner') {
} else if (POSITIONS(position) === 'left-center') {
} else if (POSITIONS(position) === 'middle') {
}
}
});
})();
``````

I know the code is quite huge but it is what it is.

Apart that, How can I check which elements inside body have same attribute presented in `REQUIRED_ATTRIBUTES` array.

P.S I don’t want to use `jQuery`.

Below is the example of

``````<button data-tooltip="true" data-tooltip-pointer-direction="bottom" data-tooltip-content="Copy to Clipboard" data-tooltip-position="top-center">
Copy
</button>
``````

OUTPUT

## unit testing – Should “functional core, imperative shell” approach canonically break “don’t test private methods” rule?

In short, “functional core, imperative shell” can be summarized as:

• functional core implements logic; you unit test it. Your tests call real functions just like in production real functions are called; ie compared to mocking / faking, you don’t run a risk that mocks / fakes mildly mismatch behavior of the real dependencies

• imperative shell basically just handles dependencies. It extracts values from them / returns results to them, but has no logic — everything is delegated by calling functional core. You don’t unit test it, because there is no logic to test

So translating this to java (for pseudo-code purposes only, my question isn’t java specific), my classes have been looking something like

``````public class Foo {

...

public int doBar() {
return doBarImpl(fetchValueFromDB());
}

static int doBarImpl(int value) {
return value + 1;
}
}
``````

The referentialy transparent / functional core methods are package private and inaccessibly from public interface, but I can still test them by declaring my unit test classes to have same package. This does run against the whole “don’t test your private methods” idea. Declaring them public, either in same class or new one, would misrepresent it, because it is ultimately an implementation detail, and I don’t want other classes to rely on its existence (naturally this is when functionality isn’t generic enough; if it’s generic and reusable, then there is no issues / questions to make it public). Keeping it private but testing through public methods throws away the whole benefit I initially mentioned, in that you’re not testing method calls directly anymore but start using mocks and fakes and again step into the risk of them not matching the real thing in production. Is my understanding correct, that it is explicitly expected to test private methods in this “functional core, imperative shell” design and ignore the rule of thumb about not testing private methods

## functional analysis – A theorem on C* algebra of compact operators

I am having problem understanding the following lemma from “C*-Algebra by Examples”, the book by Kenneth R. Davidson

I am reading this on my own. I am stuck here for a long time. Here are my confusion:

1. What is a irreducible C*- subalgebra? (So far in the book “irreducible” has been used in the context of representations)
2. In the proof, what does it mean by “appropriate dimension” of $$mathcal H$$ ? Isn’t it already determined by the given statement.
3. In the proof, unit vector “$$e$$” is chosen which I assume it is an element of $$mathfrak A$$. I am not sure if that is okay, as $$Ae=xin mathcal H$$ and
$$Be=xin mathcal H$$ , I couldn’t understand how that is possible.
4. We choose $$x,yin mathcal H$$ but what is the meaning of $$y^*$$ then?

I am not sure if there is any typo in the proof. Please help me understand this. If you can suggest some reference where this topic is discussed that would be beneficial as well.

Thank you for you time.

## functional analysis – Locally convex topology vs compact open topology

Consider the set of seminorms $$p_n$$ on $$X = C(mathbb R)$$,
$$p_n(f) = sup_{(-n, n)} |f(x)|.$$

Consider the topology on $$C(mathbb R)$$, for which sets of the form
$$V_{f_0, n, varepsilon} = {x in X mid p_n(f-f_0) < varepsilon},$$
where $$f_0 in C(mathbb R)$$, $$n in mathbb N$$, $$varepsilon > 0$$, are sub-base.

Show that this topology equals to the compact-open topology on $$C(mathbb R)$$, that is, the topology with sub-base consisting of sets
$$W_{K, U} = {f in C(mathbb R) mid f(K) subset U},$$
where $$K subset mathbb R$$ is compact and $$U subset mathbb R$$ is open.

I honestly cannot even think of an approach for neither of the two directions. My lecturer just showed this as an example for some other concept and did not prove nor even explained the intuition.

## functional programming – Is there one (or a few) canonical/ specific use cases for “functions returning functions” (beyond “decorators”)?

A common reason to return a function is when programming with iterators. Specifically, when you have a data structure (such as a list or set) and you want to return an iterator over it.

In a pure functional style, an iterator is an initial state `init: S` together with a function

``````next: S -> Option<(A, S)>
``````

where S is the state of the iterator and A is the output type of the iterator.
It takes in a current state and returns either None or a value and a new state.

So, if you want to define an iterator over a list, you need to write a function

``````iterate_over_list: List A -> (S, S -> Option<(A, S)>)
``````

where `S` is defined appropriately — depending on your implementation it could either be `(List A, int)` (the list together with an index into it) or just `List A` if you consume the list while iterating over it.

There’s no way to avoid returning a function here because an iterator is something that you call repeatedly (every time you want a new element, you ask the iterator to generate a new element).

To give an even simpler example along the same lines, sometimes you want an iterator over all integers starting at a given integer. Then you would write a function

``````iterate_starting_from: int -> (S, S -> Option<(int, S)>)
``````

where `S` in this case would just be `int` (the state of the iterator is the current integer to be returned). The implementation of this is something like (in pseudocode):

``````iterate_starting_from(i):
let init = i
let next = lambda x: Some((x, x+1))
return (init, next)
``````

## fa.functional analysis – Find the function which satisfies the following functional equation

This is the equation

$$e^{ia}fleft(int_{0}^a e^{ix}f(x)dxright)=int_{0}^a e^{ix}f(x)dx$$

where $$’a’$$ is a variable

I tried but wasn’t able to figure out.

Any suggestion towards how should I approach it are appreciated.

My attempt :

I tried to differentiate with respect to $$a$$.

I got this :

$$f(x) = e^{ia}fâ€™left(int_{0}^a e^{ix}f(x)dxright)f(a) + ifleft(int_{0}^a e^{ix}f(x)dxright)$$

then I tried to equate real and imaginary part but nothing happened.

I also tried other things but didn’t reached anywhere.

## functional analysis – Where can I find examples of cartesian coordinates mapped to a torus?

When I ask this I am imagining a torus with the axis going from negative infinity to positive infinity on both the x and y. It does this by having (0,0) on the “front” of a torus, (0,1) being at the top or a quarter of the way around, (0,2) being an 8th of the way past that (0,3) being a 16th and so on to (0,infinity). Same for the x axis.

With this, a Tangent graph would “appear” to go around and around the torus. An x=y line would make one loop around before getting back to the origin.

End game, I just want to see what different functions look like mapped to a coordinate system like this.