Files
Thun/docs/2._Library_Examples.ipynb
T
Simon Forman 81919c4bbe Make cannot deal with spaces in filenames.
Is make wrong, or is it your filename?
2018-06-06 12:57:12 -07:00

50 KiB

Examples (and some documentation) for the Words in the Library

In [1]:
from notebook_preamble import J, V

Stack Chatter

This is what I like to call the functions that just rearrange things on the stack. (One thing I want to mention is that during a hypothetical compilation phase these "stack chatter" words effectively disappear, because we can map the logical stack locations to registers that remain static for the duration of the computation. This remains to be done but it's "off the shelf" technology.)

clear

In [2]:
J('1 2 3 clear')

dup dupd

In [3]:
J('1 2 3 dup')
1 2 3 3
In [4]:
J('1 2 3 dupd')
1 2 2 3

enstacken disenstacken stack unstack

(I may have these paired up wrong. I.e. disenstacken should be unstack and vice versa.)

In [5]:
J('1 2 3 enstacken') # Replace the stack with a quote of itself.
[3 2 1]
In [6]:
J('4 5 6 [3 2 1] disenstacken')  # Unpack a list onto the stack.
4 5 6 3 2 1
In [7]:
J('1 2 3 stack')  # Get the stack on the stack.
1 2 3 [3 2 1]
In [8]:
J('1 2 3 [4 5 6] unstack')  # Replace the stack with the list on top.
                            # The items appear reversed but they are not,
                            # 4 is on the top of both the list and the stack.
6 5 4

pop popd popop

In [9]:
J('1 2 3 pop')
1 2
In [10]:
J('1 2 3 popd')
1 3
In [11]:
J('1 2 3 popop')
1

roll< rolldown roll> rollup

The "down" and "up" refer to the movement of two of the top three items (displacing the third.)

In [12]:
J('1 2 3 roll<')
2 3 1
In [13]:
J('1 2 3 roll>')
3 1 2

swap

In [14]:
J('1 2 3 swap')
1 3 2

tuck over

In [15]:
J('1 2 3 tuck')
1 3 2 3
In [16]:
J('1 2 3 over')
1 2 3 2

unit quoted unquoted

In [17]:
J('1 2 3 unit')
1 2 [3]
In [18]:
J('1 2 3 quoted')
1 [2] 3
In [19]:
J('1 [2] 3 unquoted')
1 2 3
In [20]:
V('1 [dup] 3 unquoted')  # Unquoting evaluates.  Be aware.
              . 1 [dup] 3 unquoted
            1 . [dup] 3 unquoted
      1 [dup] . 3 unquoted
    1 [dup] 3 . unquoted
    1 [dup] 3 . [i] dip
1 [dup] 3 [i] . dip
      1 [dup] . i 3
            1 . dup 3
          1 1 . 3
        1 1 3 . 

List words

concat swoncat shunt

In [21]:
J('[1 2 3] [4 5 6] concat')
[1 2 3 4 5 6]
In [22]:
J('[1 2 3] [4 5 6] swoncat')
[4 5 6 1 2 3]
In [23]:
J('[1 2 3] [4 5 6] shunt')
[6 5 4 1 2 3]

cons swons uncons

In [24]:
J('1 [2 3] cons')
[1 2 3]
In [25]:
J('[2 3] 1 swons')
[1 2 3]
In [26]:
J('[1 2 3] uncons')
1 [2 3]

first second third rest

In [27]:
J('[1 2 3 4] first')
1
In [28]:
J('[1 2 3 4] second')
2
In [29]:
J('[1 2 3 4] third')
3
In [30]:
J('[1 2 3 4] rest')
[2 3 4]

flatten

In [31]:
J('[[1] [2 [3] 4] [5 6]] flatten')
[1 2 [3] 4 5 6]

getitem at of drop take

at and getitem are the same function. of == swap at

In [32]:
J('[10 11 12 13 14] 2 getitem')
12
In [33]:
J('[1 2 3 4] 0 at')
1
In [34]:
J('2 [1 2 3 4] of')
3
In [35]:
J('[1 2 3 4] 2 drop')
[3 4]
In [36]:
J('[1 2 3 4] 2 take')  # reverses the order
[2 1]

reverse could be defines as reverse == dup size take

remove

In [37]:
J('[1 2 3 1 4] 1 remove')
[2 3 1 4]

reverse

In [38]:
J('[1 2 3 4] reverse')
[4 3 2 1]

size

In [39]:
J('[1 1 1 1] size')
4

swaack

"Swap stack" swap the list on the top of the stack for the stack, and put the old stack on top of the new one. Think of it as a context switch. Niether of the lists/stacks change their order.

In [40]:
J('1 2 3 [4 5 6] swaack')
6 5 4 [3 2 1]

choice select

In [41]:
J('23 9 1 choice')
9
In [42]:
J('23 9 0 choice')
23
In [43]:
J('[23 9 7] 1 select')  # select is basically getitem, should retire it?
9
In [44]:
J('[23 9 7] 0 select')
23

zip

In [45]:
J('[1 2 3] [6 5 4] zip')
[[6 1] [5 2] [4 3]]
In [46]:
J('[1 2 3] [6 5 4] zip [sum] map')
[7 7 7]

Math words

+ add

In [47]:
J('23 9 +')
32

- sub

In [48]:
J('23 9 -')
14

* mul

In [49]:
J('23 9 *')
207

/ div floordiv truediv

In [50]:
J('23 9 /')
2.5555555555555554
In [51]:
J('23 -9 truediv')
-2.5555555555555554
In [52]:
J('23 9 div')
2
In [53]:
J('23 9 floordiv')
2
In [54]:
J('23 -9 div')
-3
In [55]:
J('23 -9 floordiv')
-3

% mod modulus rem remainder

In [56]:
J('23 9 %')
5

neg

In [57]:
J('23 neg -5 neg')
-23 5

pow

In [58]:
J('2 10 pow')
1024

sqr sqrt

In [59]:
J('23 sqr')
529
In [60]:
J('23 sqrt')
4.795831523312719

++ succ -- pred

In [61]:
J('1 ++')
2
In [62]:
J('1 --')
0

<< lshift >> rshift

In [63]:
J('8 1 <<')
16
In [64]:
J('8 1 >>')
4

average

In [65]:
J('[1 2 3 5] average')
2.75
Warning:
Output truncated. This notebook contains too many cells to display efficiently.