Here's an implementation of default that deals with multiple assignments:
default=:4 :0
nms=. ;:^:(1-L.) x
need=. 0>nc nms
if.+/need do.(need#nms)=:need#y end.EMPTY
)
That said, note that the locale mechanism can also be used to provide
defaults using straight =: (put the default assignment in the locale
path and then override it in the current locale). This way you do not
have to erase your values if you want to change the defaults in your
defining script.
default=:4 :0
if.-.(<'default')e.copath coname'' do.
((}:,(<'default'),{:) copath coname'') copath coname ''
end.
cocurrent'default'
(x)=:y
EMPTY
)
Thanks,
--
Raul
On Tue, Jul 11, 2017 at 4:02 AM, Marshall Lochbaum <[email protected]> wrote:
> Does this scheme support easily assigning a default value to a variable,
> that is, assigning only if the name is currently unused? This is a
> common case for me and I'm just now realizing that the "default" option
> doesn't really do that.
>
> See https://github.com/mlochbaum/JSound/blob/master/wav.ijs for an
> example, where I'm forced to write an awkward line of code to set the
> default format to 16-bit 44.1kHz. This seems easy enough to support with
> another trailing character on the name, like ^ or ?. So I would write
> 'FMT? F?' =: 1 16;44100
> instead of the current version.
>
> On a side note, Earl Grey is a good language to look at if you are
> interested in more sophisticated assignment schemes. I don't know if
> much of it is relevant to J, but it's very cool to see what can happen
> if you build a language to handle pattern matching from the bottom up.
> See http://www.earl-grey.io/doc.html, up to the section on pattern
> matching.
>
> Marshall
>
> On Tue, Jul 04, 2017 at 01:40:52AM -0400, HenryRich wrote:
>> Here is a feature that I have wanted for a long time:
>>
>> http://code.jsoftware.com/wiki/System/Interpreter/Requests#Expanded_Multiple_Assignment
>>
>> It attempts to solve the problem of moving verb arguments into
>> variable-names. Allows defaults and multilevel arguments. I will implement
>> this within a month or so, so get your comments in ASAP.
>>
>> Henry Rich
>>
>> ---
>> This email has been checked for viruses by AVG.
>> http://www.avg.com
>>
>> ----------------------------------------------------------------------
>> For information about J forums see http://www.jsoftware.com/forums.htm
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm