Something pretty damn interesting is that m and u can both exist after all.

test2=: 2 : 0
smoutput (u`'');isNoun 'u'
if. 1=# (}: m) do. (({. m) `:6) @:((({: m) `:6) @: v) else. 
(}: m) test2 (({: m) `:6) @: v end.
)

isNoun =: (0 = 4!:0 ( :: 0:))@:<

test2 is a conjunction that expects a gerund as m, and recursively returns a 
conjunction with one less item from the gerund list, until it runs out of them.

   +:`+:`+: test2 +: 2
┌──────────┬─┐
│┌──┬──┬──┐│1│
││+:│+:│+:││ │
│└──┴──┴──┘│ │
└──────────┴─┘
┌───────┬─┐
│┌──┬──┐│1│
││+:│+:││ │
│└──┴──┘│ │
└───────┴─┘
32
   -:@:+:`+:`+: test2 +: 2
┌────────────────────┬─┐
│┌────────────┬──┬──┐│1│
││┌──┬───────┐│+:│+:││ │
│││@:│┌──┬──┐││  │  ││ │
│││  ││-:│+:│││  │  ││ │
│││  │└──┴──┘││  │  ││ │
││└──┴───────┘│  │  ││ │
│└────────────┴──┴──┘│ │
└────────────────────┴─┘
┌─────────────────┬─┐
│┌────────────┬──┐│1│
││┌──┬───────┐│+:││ │
│││@:│┌──┬──┐││  ││ │
│││  ││-:│+:│││  ││ │
│││  │└──┴──┘││  ││ │
││└──┴───────┘│  ││ │
│└────────────┴──┘│ │
└─────────────────┴─┘
16

u is the full verb phrase to the left, but m is any partial noun phrase to the 
left.




----- Original Message -----
From: Dan Bron <j...@bron.us>
To: programm...@jsoftware.com
Cc: 
Sent: Monday, February 24, 2014 1:12:24 PM
Subject: Re: [Jprogramming] best way to write modifier that can have m or u

That's the basic pattern. 

The name 'u' refers to the adverb's argument, so it will always exist and
be either a verb or a noun.  If the adverb is passed a verb, then u will
exist and be a verb; if the adverb is passed a noun, then u will exist and
be a noun. 

In addition to u, if the adverb is passed a noun, then the name 'm' will
exist, and refer to that noun. If the adverb is passed a verb, m will not
exist. Therefore, m, if it exists, is always a noun. 

Summarized as a table:

Adverb argument   Nameclass of 'u'   Nameclass of 'm'
verb              3 (verb)           _1 (doesn't exist)
noun              0 (noun)            0 (noun)

So you have a few options, using either positive or negative checks:

   noun =  nc :: _3: @:< 'u'   NB. u is a noun
   noun =  nc :: _3: @:< 'm'   NB. m is a noun (i.e. m exists)

   verb ~: nc :: _3: @:< 'u'   NB. u is NOT a verb
     _1 ~: nc :: _3: @:< 'm'   NB. m does NOT fail to exist

Here, I use _3 to indicate nc threw an error, because _1 and _2 are already
valid results for nc which have their own intepretations ("unassigned" and
"invalid name" respectively) , and _3 would indicate something much more
serious is going on (if the string you pass in doesn't refer to an
existing name, or a sequence of characters which _could_ form a valid
name, or even a sequence of characters which _could not_ be used as a
name, then something is very wrong).

Anyway, these phrases all answer the same question, so which you choose
depends on how'd you'd like to express it, and which you find the clearest
in your context or code.

There are also ways to use u without explictly inspecting or switching on
its nameclass, but these are use-case specific, obviously. One common
approach is  1 : 'stuff =. u"_ data' which will apply u to 'data' if u is
a verb, or give 'stuff' the same value as u if the user passed in a noun. 
Then you just deal with 'stuff' and forget about u .  Pretty nice way to
let a user provide either a specific value (passing in a noun) or a way to
produce a desired value (passing in a verb).  If you post your fuller
use-case or current adverb, we might be able to help you minimize
bookkeeping on the argument's nameclass.

-Dan



----- Original Message ---------------

Subject: [Jprogramming] best way to write modifier that can have m or u
   From: Pascal Jasmin <godspiral2...@yahoo.ca>
   Date: Mon, 24 Feb 2014 09:28:54 -0800 (PST)
     To: "programm...@jsoftware.com" <programm...@jsoftware.com>





For some reason, I remember there was an easier way to get an adverb to
notice whether it had an m or u parameter, but I can't recall if that was
the case, or how to do it.  Is there an easier way to write the adverb
below?

isNoun =: (0 = 4!:0 ( :: 0:))@:<


   2 (1 : 'if. isNoun ''u'' do. u else. u y end.') 3
2
   +: (1 : 'if. isNoun ''u'' do. u else. u y end.') 3
6
   '+: y' (1 : 'if. isNoun ''u'' do. (3 : u) y  else. u y end.') 3
6

   '+: y' (1 : 'if. isNoun ''u'' do. u =. (13 : u) end.  u ') 3  NB. TIL
you can add code after 'end.' on same line.
6

----------------------------------------------------------------------
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

Reply via email to