robpike / ivy

ivy, an APL-like calculator
Other
1.33k stars 103 forks source link

provide enclose and disclose operations #132

Closed fzipp closed 1 year ago

fzipp commented 1 year ago

One can implement equivalent operations to APL's monadic ⊂ and ⊃ like this:

op enclose  a = 1 take a a
op disclose a = a[1]

Unfortunately, the implementation of disclose depends on the origin. Would it perhaps make sense to provide them as builtins?

Other names could be box or nest for enclose, and first for disclose.

arl commented 1 year ago

Unfortunately, the implementation of disclose depends on the origin.

Isn't there a way to programmatically query whether the origin is 0 or 1 ? In which case the implementation of disclose could be made in pure ivy.

fzipp commented 1 year ago

Isn't there a way to programmatically query whether the origin is 0 or 1 ?

You can query the origin via the )origin command, but that's not an expression.

fzipp commented 1 year ago

I tried iota 1 and (0 iota 1)+1 (*) to get the origin, but both return a vector of length 1, not a scalar, so I can't use them as the index for disclose.

*) because A iota B returns origin-1 if B not found in A

robpike commented 1 year ago

What's missing first is the equivalent of the quad "operator", and I've been thinking about a good way to provide that. I actually had a prototype working but want to proceed carefully.

fzipp commented 1 year ago

Thanks for the response. I assume you mean something like ⎕IO (index origin).

It just came to me that I can get the origin as a scalar if I add up the result of iota 1. The implementations of enclose and disclose in my lib.ivy are now:

op enclose  a = 1 take a 0
op disclose a = a[+/iota 1]
robpike commented 1 year ago

Clever!

fzipp commented 1 year ago

@robpike first does not yet work as expected:

first first box box 3 3 rho iota 9
((0 1 2||
||3 4 5||
||6 7 8))

Expected:

first first box box 3 3 rho iota 9
0 1 2
3 4 5
6 7 8
robpike commented 1 year ago

The documentation I can find is unclear. Maybe the next round will be correct by your understanding.

fzipp commented 1 year ago

Thank you, I'm happy with it now.

robpike commented 1 year ago

Glad to hear it. Thanks for the bug reports.