Self Contained Namespaces
15 posts
• Page 1 of 2 • 1, 2
Self Contained Namespaces
If you are like me, you use a lot of unnamed namespaces as a datatype, just to pass around variables as a single neat package. If these namespaces contain references to other namespaces that are not children, you won't be able to serialize it (save it in a component file, etc) in the upcoming V16. This is change from V15, which would serialize it but then reading it back in, create all the sub-namespaces as real children. So, in order to save such a namespace in v16, we have to ensure that it is self contained. I think this does the trick:
Not that it matters too much for my purposes, but it appears this is not a tail call at the end, I thought due to the
Is there a way to make this a tail call?
SelfContain←{
⍝ ⍵ ←→ Namespace
⍝ ←→ ⍵ self contained
n←⍵.⎕NL ¯9
0=≢n:⍵
v←⍵.⎕NS¨⍵⍎¨n
⍵⊣∇¨n ⍵.{⍎⍺,'←⍵'}¨v
}
Not that it matters too much for my purposes, but it appears this is not a tail call at the end, I thought due to the
⍵⊣at the beginning of the last line. However, when I remove this, it still does not appear to be a tail call. Does the "each" get in the way of that?
Is there a way to make this a tail call?
- paulmansour
- Posts: 420
- Joined: Fri Oct 03, 2008 4:14 pm
Re: Self Contained Namespaces
On a related note, I have meaning to ask what the consensus is on injecting names and values into a namespace. I tend to use something like:
I've never liked this really. Are there any better alternatives? Would a new primitive or language feature make sense? I find this happens all the time in my code, and while I have cover functions for it, it is not a satisfying solution.
n ⍵.{⍎⍺,'←⍵'}¨v
I've never liked this really. Are there any better alternatives? Would a new primitive or language feature make sense? I find this happens all the time in my code, and while I have cover functions for it, it is not a satisfying solution.
- paulmansour
- Posts: 420
- Joined: Fri Oct 03, 2008 4:14 pm
Re: Self Contained Namespaces
"... does not appear to be a tail call. Does the "each" get in the way of that?"
Yes it does. Currently ∇ ¨ is not a tail call but I have logged an issue [14567] to investigate whether it would be possible.
Yes it does. Currently ∇ ¨ is not a tail call but I have logged an issue [14567] to investigate whether it would be possible.
- JohnS|Dyalog
Re: Self Contained Namespaces
I've always wondered at the utilty of dyadic ⍎ given the fact thatpaulmansour wrote:Would a new primitive or language feature make sense?
ns ⍎ string ←→ ns.⍎ stringsurely
names ⍎ values ←→ names {⍎⍺,'←⍵'} valuesand
names ns.⍎ values ←→ names ns.{⍎⍺,'←⍵'} valueswould be much better uses for the dyad.
-
Phil Last - Posts: 628
- Joined: Thu Jun 18, 2009 6:29 pm
- Location: Wessex
Re: Self Contained Namespaces
JohnS|Dyalog wrote:"Currently ∇ ¨ is not a tail call but I have logged an issue [14567] to investigate whether it would be possible.
Some time in this or the previous century I asked a similar question of ∇ / but I can't remember if there was an answer.
-
Phil Last - Posts: 628
- Joined: Thu Jun 18, 2009 6:29 pm
- Location: Wessex
Re: Self Contained Namespaces
Phil, I like it!
At first glance it looks like your suggestion for dyadic execute would not conflict with the current definition and thus could be an enhancement.
At first glance it looks like your suggestion for dyadic execute would not conflict with the current definition and thus could be an enhancement.
- paulmansour
- Posts: 420
- Joined: Fri Oct 03, 2008 4:14 pm
Re: Self Contained Namespaces
Phil Last wrote:JohnS|Dyalog wrote:"Currently ∇ ¨ is not a tail call but I have logged an issue [14567] to investigate whether it would be possible.
Some time in this or the previous century I asked a similar question of ∇ / but I can't remember if there was an answer.
If ∇ ¨ could be made a tail call, the same technique would apply to all primitive operator expressions, including reduction and, for example, ∇ ⍨ /.
But what about ⊃∇ / and ↑∇ /, which, it seems to me, appear in the majority of such reductions?
The disclose/mix would need either to be recognised at ⎕FX time as part of an idiom, or passed dynamically as a disclose/mix_when_you're_finished flag to the reduction. The latter technique is more general as it allows tail-reductions with any operand function in the same capsule, not just "self" ∇.
- JohnS|Dyalog
Re: Self Contained Namespaces
I wonder if the new @ operator could do the job of injecting values v with names n into space s:
It seems to fit.
If the result was the space itself, which seems only natural (and what else would it be?), then we could create and populate a space in one go:
which I don't think we can do now.
Now all I need is a primitive to replace the offending ⎕NS ''!
(v@n)s
It seems to fit.
If the result was the space itself, which seems only natural (and what else would it be?), then we could create and populate a space in one go:
s←(v@n)⎕ns ''
which I don't think we can do now.
Now all I need is a primitive to replace the offending ⎕NS ''!
Last edited by paulmansour on Sun May 14, 2017 11:44 am, edited 3 times in total.
- paulmansour
- Posts: 420
- Joined: Fri Oct 03, 2008 4:14 pm
Re: Self Contained Namespaces
JohnS|Dyalog wrote:"... does not appear to be a tail call. Does the "each" get in the way of that?"
Yes it does. Currently ∇ ¨ is not a tail call but I have logged an issue [14567] to investigate whether it would be possible.
Thanks John.
Even if this were a tail call, is there a way I can get rid of the leading ⍵⊣ in my particular case above, and still get the result I want, which is the original right arg, and and not the result of ∇¨?
- paulmansour
- Posts: 420
- Joined: Fri Oct 03, 2008 4:14 pm
Re: Self Contained Namespaces
Even if this were a tail call, is there a way I can get rid of the leading ⍵⊣ in my particular case above, and still get the result I want, which is the original right arg, and and not the result of ∇¨?
If ∇ ¨ were a tail call, the only way that springs to mind is to pass back an ignored result from the recursion and call SelfContain via a (⊢⊣ ...) fork. Note line[0] in:
SelfContain ← ⊢⊣{
⍝ ⍵ ←→ Namespace
⍝ ←→ ⍵ self contained
n←⍵.⎕NL ¯9
0=≢n:42 ⍝ (result ignored by above ⊢⊣ fork)
v←⍵.⎕NS¨⍵⍎¨n
∇¨n ⍵.{⍎⍺,'←⍵'}¨v ⍝ look, no ⍵⊣
}
Can someone think of a better way?
- JohnS|Dyalog
15 posts
• Page 1 of 2 • 1, 2
Who is online
Users browsing this forum: No registered users and 1 guest
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group