Pls. discuss & improve: inspiration by ScriptFollows

MiServer is Dyalog's APL-based web development framework

Pls. discuss & improve: inspiration by ScriptFollows

Postby MBaas on Tue Jul 28, 2015 7:15 pm

Something I do not like about my MiServer-code is that length of some lines that needs lots of linewrapping. Thinking about this, I wrote a little tool that enables me to transform lines such a this (and this is a short example, but happens to be my first test-case):
Code: Select all
          ⍝ d.Add label'Klasse <i class="fa fa-tag fa-fw"></i>'('class' 'control-label col-lg-3' 'for' 'kls')
          ⍝ sl←(d.Add div'' '.col-lg-9').Add Select'kls'('Bitte Klasse auswählen' '5.1' '5.2' '5.3' '5.4' '5.5' '5.6',[1.5]'' 5.1 5.2 5.3 5.4 5.5 5.6)

into this (which is larger, but more readable and by far easier to maintain, I hope):
Code: Select all
          d.Add label AttributesFollow
          ⍝ 'Klasse <i class="fa fa-tag fa-fw"></i>'
          ⍝  (
          ⍝    'class' 'control-label col-lg-3'
          ⍝    'for' 'kls'
          ⍝ )
          sl←(d.Add div'' '.col-lg-9').Add Select AttributesFollow
          ⍝ 'kls'
          ⍝ (7 2⍴
          ⍝    'Bitte Klasse auswählen' ''
          ⍝    '5.1' 5.1
          ⍝    '5.2' 5.2
          ⍝    '5.3' 5.3
          ⍝    '5.4' 5.4
          ⍝    '5.5' 5.5
          ⍝    '5.6' 5.6
          ⍝ )

The idea is very simple: we just do an ⍎ with the result of "ScriptFollows" ;-)
Code: Select all
    ∇ r←AttributesFollow
     ⍝ treat following commented lines in caller as a collection of attributes
     ⍝ = ScriptFollows + ⍎ ;-)
      r←{∊{'⍝'=⊃⍵:'' ⋄ ' ',dtlb ⍵}¨1↓¨⍵/⍨∧\'⍝'=⊃¨⍵}dtlb¨(1+2⊃⎕LC)↓⎕NR 2⊃⎕SI
      r←⍎r
    ∇

Any ideas for improvements? Other comments?
User avatar
MBaas
 
Posts: 156
Joined: Thu Oct 16, 2008 1:17 am
Location: Gründau / Germany

Re: Pls. discuss & improve: inspiration by ScriptFollows

Postby MBaas on Wed Jul 29, 2015 5:48 am

After sleeping over it, I did a major change:
Code: Select all
∇ r←DataFollows
...sounds more generic, I think :-)
User avatar
MBaas
 
Posts: 156
Joined: Thu Oct 16, 2008 1:17 am
Location: Gründau / Germany

Re: Pls. discuss & improve: inspiration by ScriptFollows

Postby sjt on Wed Jul 29, 2015 7:52 am

Although, like you, a fan of ScriptFollows, I share the common aversion to avoidable ⍎.

Within a general practice of defining a name once only (ie not changing the value of a 'variable') I except the Heraclitean variable ∆ (Mutandum?), reading it only on the line on which it is set, or the next one. Thus

∆←('Bitte Klasse auswählen' '')('5.1' 5.1)('5.2' 5.2)('5.3' 5.3)('5.4' 5.4 )('5.5' 5.5)('5.6' 5.6)
sl←(d.Add div'' '.col-lg-9').Add Select 'kls' (↓⌽↑∆)

Like you, I prefer here a list of pairs to a pair of lists, but avoid the 7 in the code, which must always match the length of the list.
sjt
 
Posts: 21
Joined: Fri Nov 05, 2010 6:26 am

Re: Pls. discuss & improve: inspiration by ScriptFollows

Postby MBaas on Wed Jul 29, 2015 9:21 am

Ok, you are right - I picked a bad example. The following case is a bit more obvious ;-)
Code: Select all
ip←(d1.Add div'' '.col-lg-1').Add _html.input''('id' 'plz' 'name' 'plz' 'placeholder' 'Postleitzahl' 'type' 'text' 'class' 'form-control col-lg-1' 'maxlength' '5' 'value' 63571 'style' 'width: 68px;' 'data-validate' 'required,postcode')

vs.
Code: Select all
          ip←(d1.Add div'' '.col-lg-1').Add _html.input'' DataFollows
          ⍝ 'id' 'plz'
          ⍝ 'name' 'plz'
          ⍝ 'placeholder' 'Postleitzahl'
          ⍝ 'type' 'text'
          ⍝ 'class' 'form-control col-lg-1'
          ⍝ 'maxlength' '5'
          ⍝ 'value' 63571
          ⍝ 'style' 'width: 68px;'
          ⍝ 'data-validate' 'required,postcode'


I also love short functions which take less than a screenful and with that style they easily grow beyond, but I prefer that anyway, because it is so much easier (and less error-prone) to read and edit...
User avatar
MBaas
 
Posts: 156
Joined: Thu Oct 16, 2008 1:17 am
Location: Gründau / Germany

Re: Pls. discuss & improve: inspiration by ScriptFollows

Postby Phil Last on Wed Jul 29, 2015 9:32 am

And won't it be nice when we can just code this instead
      ip←(d1.Add div'' '.col-lg-1').Add _html.input'' ([
'id' 'plz'
'name' 'plz'
'placeholder' 'Postleitzahl'
'type' 'text'
'class' 'form-control col-lg-1'
'maxlength' '5'
'value' 63571
'style' 'width: 68px;'
'data-validate' 'required,postcode'
])
or at least something very much like it?
User avatar
Phil Last
 
Posts: 628
Joined: Thu Jun 18, 2009 6:29 pm
Location: Wessex

Re: Pls. discuss & improve: inspiration by ScriptFollows

Postby MBaas on Wed Jul 29, 2015 10:51 am

The ultimate solution!

BTW: did you take that screenshot with an early 15-beta? ;-)
User avatar
MBaas
 
Posts: 156
Joined: Thu Oct 16, 2008 1:17 am
Location: Gründau / Germany

Re: Pls. discuss & improve: inspiration by ScriptFollows

Postby MBaas on Mon Sep 14, 2015 3:54 pm

I found another nice use for SF today:

Code: Select all
sql←ScriptFollows
⍝ SELECT {many fields}
⍝ FROM {one table}
⍝ JOIN {another table} ON {some fields}
⍝ WHERE {several conditions are met}
⍝ ORDER BY {a field}
r←#.SQL.Do src sql


MUCH better than one-liners! ;-)
User avatar
MBaas
 
Posts: 156
Joined: Thu Oct 16, 2008 1:17 am
Location: Gründau / Germany


Return to MiServer

Who is online

Users browsing this forum: No registered users and 0 guests