Cantor set: Difference between revisions

Content deleted Content added
Hout (talk | contribs)
Hout (talk | contribs)
→‎{{header|Excel}}: Added a draft in terms of Excel LAMBDA expressions
Line 648: Line 648:
{{out}}
{{out}}
Same result of Java
Same result of Java

=={{header|Excel}}==
===LAMBDA===
Binding names to the following lambda expressions in the Name Manager of the Excel WorkBook:

(See [https://www.microsoft.com/en-us/research/blog/lambda-the-ultimatae-excel-worksheet-function/ LAMBDA: The ultimate Excel worksheet function])

{{Works with|Office 365 betas 2021}}
<lang lisp>CANTOR
=LAMBDA(n,
APPLYN(n)(
LAMBDA(grid,
APPENDROWS(grid)(
CANTOROW(
LASTROW(grid)
)
)
)
)({0,1})
)


CANTOROW
=LAMBDA(xys,
LET(
nCols, COLUMNS(xys),

IF(2 > nCols,
xys,
IF(3 < nCols,
APPENDCOLS(
CANTORSLICES(TAKECOLS(2)(xys))
)(
CANTOROW(DROPCOLS(2)(xys))
),
CANTORSLICES(TAKECOLS(2)(xys))
)
)
)
)


CANTORSLICES
=LAMBDA(ab,
LET(
a, INDEX(ab, 1),
b, INDEX(ab, 2),
third, (b - a) / 3,

CHOOSE({1,2,3,4}, a, a + third, b - third, b)
)
)


SHOWCANTOR
=LAMBDA(grid,
LET(
leaves, LASTROW(grid),
leafWidth, INDEX(leaves, 1, 2) - INDEX(leaves, 1, 1),
leafCount, 1 / leafWidth,

SHOWCANTROWS(leafCount)(grid)
)
)


SHOWCANTROWS
=LAMBDA(leafCount,
LAMBDA(grid,
LET(
xs, FILTERP(
LAMBDA(x, NOT(ISNA(x)))
)(
HEADCOL(grid)
),

runLengths, LAMBDA(x,
CEILING.MATH(leafCount * x))(
SUBTRACT(TAILROW(xs))(INITROW(xs)
)
),

iCols, SEQUENCE(1, COLUMNS(runLengths)),

CONCAT(
REPT(
IF(ISEVEN(iCols), " ", "█"),
runLengths
)
) & CHAR(10) & IF(1 < ROWS(grid),
SHOWCANTROWS(leafCount)(
TAILCOL(grid)
),
""
)
)
)
)</lang>

and also assuming the following generic bindings in the Name Manager for the WorkBook:

<lang lisp>APPENDCOLS
=LAMBDA(xs,
LAMBDA(ys,
LET(
nx, COLUMNS(xs),
ny, COLUMNS(ys),
colIndexes, SEQUENCE(1, nx + ny),
rowIndexes, SEQUENCE(MAX(ROWS(xs), ROWS(ys))),

IFERROR(
IF(nx < colIndexes,
INDEX(ys, rowIndexes, colIndexes - nx),
INDEX(xs, rowIndexes, colIndexes)
),
NA()
)
)
)
)


APPENDROWS
=LAMBDA(xs,
LAMBDA(ys,
LET(
nx, ROWS(xs),
rowIndexes, SEQUENCE(nx + ROWS(ys)),
colIndexes, SEQUENCE(
1,
MAX(COLUMNS(xs), COLUMNS(ys))
),

IFERROR(
IF(rowIndexes <= nx,
INDEX(xs, rowIndexes, colIndexes),
INDEX(ys, rowIndexes - nx, colIndexes)
),
NA()
)
)
)
)


APPLYN
=LAMBDA(n,
LAMBDA(f,
LAMBDA(x,
IF(0 < n,
APPLYN(n - 1)(f)(
f(x)
),
x
)
)
)
)


DROPCOLS
=LAMBDA(n,
LAMBDA(xs,
INDEX(
xs,
SEQUENCE(ROWS(xs), 1),
SEQUENCE(1, (COLUMNS(xs) - n), 1 + n, 1)
)
)
)


FILTERP
=LAMBDA(p,
LAMBDA(xs,
FILTER(xs, p(xs))
)
)


HEADCOL
=LAMBDA(xs,
LET(REM, "The first item of each column in xs",

INDEX(xs, 1, SEQUENCE(1, COLUMNS(xs)))
)
)


INITROW
=LAMBDA(xs,
INDEX(
xs,
SEQUENCE(
1,
COLUMNS(xs) - 1,
1, 1
)
)
)


LASTROW
=LAMBDA(xs,
INDEX(
xs,
ROWS(xs),
SEQUENCE(1, COLUMNS(xs), 1, 1)
)
)


SUBTRACT
=LAMBDA(a,
LAMBDA(b, a - b)
)


TAILCOL
=LAMBDA(cols,
LET(REM, "The tail of each column in the grid.",

INDEX(
cols,
SEQUENCE(ROWS(cols) - 1, 1, 2, 1),
SEQUENCE(1, COLUMNS(cols))
)
)
)


TAILROW
=LAMBDA(xs,
LET(REM,"The tail of each row in the grid",
n, COLUMNS(xs) - 1,

IF(0 < n,
INDEX(
xs,
SEQUENCE(ROWS(xs), 1, 1, 1),
SEQUENCE(1, n, 2, 1)
),
NA()
)
)
)


TAKECOLS
=LAMBDA(n,
LAMBDA(xs,
INDEX(
xs,
SEQUENCE(ROWS(xs)),
SEQUENCE(1, n)
)
)
)</lang>

{{Out}}
As a string, with the format of the cell set to a mono-spaced font, and with Format > Cells > Alignment > Wrap text = True, to allow for the display of Excel's (platform - independent) CHAR(10) line breaks:
{| class="wikitable"
|-
|||style="text-align:right; font-family:serif; font-style:italic; font-size:120%;"|fx
! colspan="2" style="text-align:left; vertical-align: bottom; font-family:Arial, Helvetica, sans-serif !important;"|=SHOWCANTOR(CANTOR(3))
|- style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff;"
|
| A
| B
|- style="text-align:right;"
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 1
| style="text-align:right; font-weight:bold" |
| style="text-align:left; font-weight:bold" | Cantor set level 3
|- style="text-align:right;"
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 2
| style="text-align:right; font-weight:bold" |
| style="background-color:#cbcefb;" | <lang>███████████████████████████
█████████ █████████
███ ███ ███ ███
█ █ █ █ █ █ █ █</lang>
|}




=={{header|Factor}}==
=={{header|Factor}}==