-
-
Notifications
You must be signed in to change notification settings - Fork 0
Snip: Function With Conditional
John Weiss edited this page May 12, 2020
·
12 revisions
Discuss this example here: https://github.com/orgs/contextual-contribs/teams/dream-team/discussions/1
(counts: 1-character per indent)
Javascript 105 chars
greetByName('Jack')
function greetByName(sName) {
if (!sName) {
console.log ('Hi');
}
else {
console.log ('Hello, ' + sName);
};
};
contextual with boolean test and parameter-list
greetByName 'Jack'
greetByName sName
!
Say 'Hello ' sName
Say 'Hi'
without parameter-list
greetByName
!
Say 'Hello ' sName
Say 'Hi'
non-redundant action
greetByName sName
! Say
'Hello ' sName
'Hi'
without boolean test
greetByName sName
Say 'Hello ' sName
Say 'Hi'
without test, parameters, or redundant action
greetByName
Say
'Hello ' sName
'Hi'
current fave
without parameters or redundant action
36 chars
greetByName
! Say
'Hello ' sName
'Hi'
Full contextual version
-
greetByName 'Jack'No parens in the call. Totally superfluous. - Eliminate
functionkeyword. Everything's a function/object, and you don't have to say it. it's assumed. Just get to the name. - No curlies or parens. Indentation shows blocks. A label immediately followed by indented lines is obviously a function. (assure no conflict with other kinds of blocks-- most are prolly contextually-obvious)
- Elimination of almost all punctuation. Only strings need to be indicated in this example.
- 1st line of a block receives first thing passed to it from it's parent. In this case, it's parent is the function signature. By default, a function signature will pass the incoming parameter to it's first child line (in this case,
!). - ! means "perform boolean test on whatever the parent line returned: either a string or a null.
- ! returns a boolean, therefor it's obvious this is a switch. We don't have to say it. No
ifneeded. It's a boolean statement. It's obviously anif. - All boolean tests execute true action first, false action second, by default. It's positional. (if JS parameters can be positional, then so can anything else be positional).
- No concatenation char needed.
Say 'Hello ' sNameis an obvious concatenation. Parser can assume that. - No
end subor closing curlies. A blank line after an indented block means: "end of function".
Without parameters
- Just as JS can provide an unstated parameter list through the
argskeyword. -
Say 'Hello ' sNameContextually, parser can assume this is a passed parameter, because:- It's the first variable that appears in the function. (this may be the only rule needed)
- We're using it in a sentence having not explicitly assigned anything to it. That makes no sense-- unless we assume it's the passed-parameter.
-
Say 'Hello ' sNameFirst time sName is used. Variables are declared and typed when first used. If memory optimization strategies require reserving the memory, let the transpiler deal with that. Stop making the programmer do the compiler's work. - All parameters are private unless fully qualified. Therefor, sName is assumed be be a locally-scoped variable.
Without boolean test
- 1st line of fx receives first thing passed into the fx. If it's a loaded string, that's truthy. If it's a null, that's falsey. So we know this is a boolean test.
- Alternate rule: 2nd line of fx receives falsey from 1st line, which tried to print a null, and failed (cuz you can't print nulls). Therefor the 2nd line executes. This is similar to JS double-pipe behavior, where in this case our newline acts as the double-pipe.