[dev] less lines of code suck less

2021-05-03 Thread Greg Reagle
Would sbase suck less if the program head, which is currently a C program of 77 
lines, were replaced with something like 
  #!/bin/sh
  sed "$1"q

I know that it would need to be a bit more elaborate than that to handle the -n 
flag, but still.  Is there any advantage to having a separate C program?



[dev] Re: less lines of code suck less

2021-05-03 Thread Greg Reagle
I'm sorry I forgot to include this in my initial message.  I found no 
difference in performance time or memory usage (with a small file), so that 
does not seem to be an advantage.

And I am serious about this--I really want to know.  Is there any good reason 
to have a separate C program?

On Mon, May 3, 2021, at 16:28, Greg Reagle wrote:
> Would sbase suck less if the program head, which is currently a C 
> program of 77 lines, were replaced with something like 
>   #!/bin/sh
>   sed "$1"q
> 
> I know that it would need to be a bit more elaborate than that to 
> handle the -n flag, but still.  Is there any advantage to having a 
> separate C program?
> 



Re: [dev] less lines of code suck less

2021-05-03 Thread Jeremy
On 05/03/21 04:28PM, Greg Reagle wrote:
> Would sbase suck less if the program head, which is currently a C program of 
> 77 lines, were replaced with something like 
>   #!/bin/sh
>   sed "$1"q
> 

Great job.

> I know that it would need to be a bit more elaborate than that to handle the 
> -n flag, but still.  Is there any advantage to having a separate C program?
> 

The spec gets a bit tricky when handling multiple files - see:
seq 3 | awk '{ print $1 > $1 }'
head -n1 $(seq 3)

This is simple enough to do in awk, with very few lines of code.

I'd argue that requiring awk to use `head` would create more
complexity(for the end user) than it would solve for the developer.

Jeremy



Re: [dev] less lines of code suck less

2021-05-03 Thread Greg Reagle
On Mon, May 3, 2021, at 16:51, Jeremy wrote:
> I'd argue that requiring awk to use `head` would create more
> complexity(for the end user) than it would solve for the developer.

I assume that you mean requiring head to use awk.  I cannot imagine how it 
would have any effect at all on the end user.  And if it has less lines, in a 
higher level language, sounds like a win.



Re: [dev] less lines of code suck less

2021-05-03 Thread Greg Reagle
> Would sbase suck less if the program head, which is currently a C 
> program of 77 lines, were replaced with something like 
>   #!/bin/sh
>   sed "$1"q

Here it is in 37 lines of glorious rc shell code.  Note that head.c also 
depends on several functions in libutil, so it is more than 77 lines really.

#!/usr/bin/env rc

fn usage {printf '%s [-n num] [file ...]\n' $0; exit $1}
fn numeric {printf '%s' $1 | grep -Eq '^[0-9]+$'}
fn positive {expr $1 '>' 0 > /dev/null}
lines=10
while (! ~ $#* 0 && ~ $1 -* && ! ~ $1 --) {
switch($1) {
case -n
if (~ $2 ?*) {
if (numeric $2 && positive $2) {
lines=$2
shift
}; if not {   # else
usage 1
}
}; if not {
usage 2
}
case -*;  usage 3
}
shift
}
if (~ $1 --) {shift}
switch($#*) {
case 0
echo sed $lines^q
case 1
echo sed $lines^q $1
case *
while (! ~ $#* 0) {
printf '==> %s <==\n' $1
echo sed $lines^q $1
shift
}
}
true  # otherwise script will exit with error


head.rc
Description: application/vnd.kde.kxmlguirc


Re: [dev] less lines of code suck less

2021-05-03 Thread Hiltjo Posthuma
On Mon, May 03, 2021 at 04:28:47PM -0400, Greg Reagle wrote:
> Would sbase suck less if the program head, which is currently a C program of 
> 77 lines, were replaced with something like 
>   #!/bin/sh
>   sed "$1"q
> 

No

> I know that it would need to be a bit more elaborate than that to handle the 
> -n flag, but still.  Is there any advantage to having a separate C program?
> 

Yes

-- 
Kind regards,
Hiltjo