Function clipboard: copy

Bash, 43 bytes

sed s/+/rdnFPrp+/g\;s/U/p2^/g|dc|tac|sed 4q

Try it online!

This prints the clipboard in the following format, note the usage of \x0F as the separator.

item_1\x0Fitem_2
item_3
.
.
item_m\x0Fitem_n

The key idea is to pass this into dc, a stack-based language, such that the required stack items will be printed.

The input is piped to sed where every + is replaced with rdnFPrp+, which in dc prints the second number on the stack followed by \x0F and then the top number before performing addition. sed also replaces every U with p2^, print the top stack element and square it.

The first substitution command s replaces all, as is denoted by the global flag g, +s with rdnFPrp+. In dc, r swaps the top two stack items, d duplicates the top item, n prints it without a newline, F pushes 15 onto the stack and P prints it as a character (which is the delimiter), r swaps again, p prints the top stack item and then + performs addition on the top two stack items.

We have another command, and in sed, commands are separated by either semicolons or newlines, of which the first option is chosen. Simply having ; will make bash interpret that as the end of the sed command, so it is escaped with a \.

In the last substitution command, U is replaced globally with p2^. In dc, p prints, and 2^ raises it to the second power.

The result of sed is eval'ed as dc code, printing the entire clipboard.

The pipe to dc makes dc interpret that as dc code. Now, the most recent calls are at the bottom and the older ones at the top.

Since the lines are in reverse order, tac (reverse cat) is used to fix that.

And finally, sed picks the first 4 lines from tac.

This is a shorter way of doing head -4. sed performs the commands to every line of the input one at a time. If there are no commands, nothing is done to the input, and it is returned as it is. 4q tells sed to perform the command q on line 4. When sed is processing line 4 of the input, the first three inputs have already been printed. The command q quits the program, so it prints the fourth line and quits, thus performing the equivalent of head -4.


Python 2, 126 bytes

s=[0];b=[]
for c in input().split():k='U+'.find(c)+1;b=[s[k-1::-1]][:k]+b;s=[int([c,s[0]**2,sum(s[:2])][k])]+s[k:]
print b[:4]

Try it online!


Haskell, 113 109 bytes

take 4.([]#).words
x:y:s#"+":r=(x+y:s#r)++[[y,x]]
x:s#"U":r=(x*x:s#r)++[[x]]
s#n:r=read n:s#r
_#_=[]
infix 4#

The first line defines an anonymous function which takes a string, e.g. "3 2 + 6 + 12 4 U + +", and returns a list of lists of ints: [[11,28],[12,16],[4],[5,6]]. Try it online!