4 deleted 283 characters in body
source | link

The human-readble code (contains an extra argument CAT to show the squeaks):

 sqk <- function(.){
   s=strsplit;u=paste0
   .=s(.,"")[[1]]            # break the argument up into 1-character pieces
   p=s("><)(}{][","")[[1]]   # vector of brackets
   .[!.%in%p]="§"            # replace anything besides brackets by § (--> error)
   for(i in 1:4*2){     
     .[.==p[i]]=sprintf("S('%s',{",p[i])    # '<' -->   S('<',{     ... etc
     .[.==p[i-1]]=sprintf("},'%s');",p[i])  # '>' -->   },'<');     ... etc  
   }
   S=function(H,B,T)if(H!=T)stop()          # define the working horse
   r=try(eval(parse(,,u(.,collapse=""))),1) # evaluate the sequence
   if(inherits(r,"try-error"))FALSE else TRUE   # any errors?
   }

The human-readble code (contains an extra argument CAT to show the squeaks):

 sqk <- function(.){
   s=strsplit;u=paste0
   .=s(.,"")[[1]]
   p=s("><)(}{][","")[[1]]
   .[!.%in%p]="§"
   for(i in 1:4*2){
     .[.==p[i]]=sprintf("S('%s',{",p[i])
     .[.==p[i-1]]=sprintf("},'%s');",p[i])
   }
   S=function(H,B,T)if(H!=T)stop()
   r=try(eval(parse(,,u(.,collapse=""))),1)
   if(inherits(r,"try-error"))FALSE else TRUE
   }

The human-readble code:

 sqk <- function(.){
   s=strsplit;u=paste0
   .=s(.,"")[[1]]            # break the argument up into 1-character pieces
   p=s("><)(}{][","")[[1]]   # vector of brackets
   .[!.%in%p]="§"            # replace anything besides brackets by § (--> error)
   for(i in 1:4*2){     
     .[.==p[i]]=sprintf("S('%s',{",p[i])    # '<' -->   S('<',{     ... etc
     .[.==p[i-1]]=sprintf("},'%s');",p[i])  # '>' -->   },'<');     ... etc  
   }
   S=function(H,B,T)if(H!=T)stop()          # define the working horse
   r=try(eval(parse(,,u(.,collapse=""))),1) # evaluate the sequence
   if(inherits(r,"try-error"))FALSE else TRUE   # any errors?
   }
3 deleted 283 characters in body
source | link

R, over 300, not competing298

function(.){s=strsplit;u=paste0;.=s(.,"")[[1]];p=s("><)(}{][","")[[1]];.[!.%in%p]="§";for(i in 1:4*2){.[.==p[i]]=sprintf("S('%s',{",p[i]);.[.==p[i-1]]=sprintf("},'%s');",p[i])};S=function(H,B,T)if(H!=T)stop();r=try(eval(parse(,,u(.,collapse=""))),1);if(inherits(r,"try-error"))FALSE else TRUE}

This function is the first example of a totally new programming style, squeaking brackets. The ideaapproach here is that the closing bracket will squeak to convert the opening bracketsequence into R code, and each type of opening brackets will only accept a certain type of squeaks. Otherwise, the brackets are convertedthen try to R codeparse and evaluatedevaluate it. If that gives an error, so there may be syntax errors -- these will also result inthen return FALSE.

It works like this:

  1. If the input contains anything besides brackets, the function will immediately return FALSE
  2. Otherwise, the input is converted into squeaking brackets. A squeaking bracket is a function which throws an error if the value of its SQK argument is different from the function's name. This kind of error is called the "Wrong squeak".
  3. The collection of squeaking brackets is parsed and evaluated. If there are no errors, TRUE is returned, otherwise FALSE.

    squeak <- function(., CAT=FALSE){
      "%.%" <- paste0
      . <- strsplit(.,"")[[1]]
      p <- strsplit("(<[{)>]}","")[[1]]
      if(any(!.%in%p))return(FALSE)
      A <- B <- C <- D <- function(x, SQK){
         h <- as.character(match.call()[[1]])
         if(h!=SQK) stop("WRONG SQUEAK!") else TRUE
      }
     for(i in 1:4) {
       j <- i+4
       L <- LETTERS[i]
     .<-sub(p[i],L %.% "EF",  . , fixed=TRUE)
     .<-sub(p[j],paste0("H, SQK='",L,"'G;"),., fixed=TRUE)
     }
     . <- sub("E", "(", .)   # E F G H converted back to brackets
     . <- sub("F", "{", .)
     . <- sub("G", ")", .)
     . <- sub("H", "}", .)
     . <- paste(., collapse="")
     . <- gsub(";)", ")", ., fixed=TRUE)
     . <- gsub(";,", ",", ., fixed=TRUE)
     if(CAT) print(.)
     v <- try(eval(parse(text=.)), silent=!CAT)
     if(inherits(v, "try-error")) FALSE else TRUE
     } 
    

For exampleBut there is a minor problem ... R's rules for brackets are different, so ()< is converted intoand A({}, SQK='A');> are not brackets at all, and the SQK argumentother types have rules of their own. This is solved by a revolutionary approach "A"; the-- a squeaking function A recognizes this as the right squeak, whose only function is to signal an error if its head and returns TRUEtail squeak in different ways. However

For example, (][] is convertedtransformed into AS('[', {}, SQK='C'']');, so thiswhere S is an example of a wrongdefined as ...

S=function(H,B,T)if(H!=T)stop() 

Because the head squeak and tail squeak match, no error is thrown.

A few other examples (the left part is a sequence of brackets and right part is its transformation into valid R code that can be evaluated):

[}     --> squeak S("'[', {}, '}')     # squeaks an error
[(]")]   -->  S('[', CAT=T{S('(',{},'(')}, "[")
[1]({[]}) "A-->  S('(',{S('{',{S('[',{},'[');},'{');},'(');

Some other sequences of brackets will result in parse errors:

[[) SQK='C'   -->   S('[',{S('[',{},'(');"; 
Error

So the remaining part just catches the errors and returns FALSE if there are any, and TRUE if there are none.

The human-readble code (contains an extra argument CAT to show the squeaks):

 insqk A<- function(.){
   s=strsplit;u=paste0
   .=s(.,"")[[1]]
   p=s("><)(}{][","")[[1]]
   .[!.%in%p]="§"
   for(i in 1:4*2){
 WRONG SQUEAK   .[.==p[i]]=sprintf("S('%s',{",p[i])
     .[.==p[i-1]]=sprintf("},'%s');",p[i])
   }
   S=function(H,B,T)if(H!=T)stop()
   r=try(eval(parse(,,u(.,collapse=""))),1)
   if(inherits(r,"try-error"))FALSE else TRUE
   }

It works as expected for the example inputApplying it on sample cases:

truthy<-readLines(textConnection("()
[](){}<>
(((())))
({[<>]})
[{()<>()}[]]
[([]{})<{[()<()>]}()>{}]"))
falsy<-readLines(textConnection("(
}
(<2)>
(()()foobar)
[({}<>)>
(((()))"))
> sapply(truthy,squeaksqk)
                      ()                 [](){}<>                 (((()))) 
                    TRUE                     TRUE                     TRUE 
                ({[<>]})             [{()<>()}[]] [([]{})<{[()<()>]}()>{}] 
                    TRUE                     TRUE                     TRUE 
> sapply(falsy,squeaksqk)
           (            }        (<2)> (()()foobar)     [({}<>)>      (((())) 
       FALSE        FALSE        FALSE        FALSE        FALSE        FALSE 

R, over 300, not competing

This function is the first example of a totally new programming style, squeaking brackets. The idea is that the closing bracket will squeak to the opening bracket, and each type of opening brackets will only accept a certain type of squeaks. Otherwise, the brackets are converted to R code and evaluated, so there may be syntax errors -- these will also result in FALSE.

It works like this:

  1. If the input contains anything besides brackets, the function will immediately return FALSE
  2. Otherwise, the input is converted into squeaking brackets. A squeaking bracket is a function which throws an error if the value of its SQK argument is different from the function's name. This kind of error is called the "Wrong squeak".
  3. The collection of squeaking brackets is parsed and evaluated. If there are no errors, TRUE is returned, otherwise FALSE.

    squeak <- function(., CAT=FALSE){
      "%.%" <- paste0
      . <- strsplit(.,"")[[1]]
      p <- strsplit("(<[{)>]}","")[[1]]
      if(any(!.%in%p))return(FALSE)
      A <- B <- C <- D <- function(x, SQK){
         h <- as.character(match.call()[[1]])
         if(h!=SQK) stop("WRONG SQUEAK!") else TRUE
      }
     for(i in 1:4) {
       j <- i+4
       L <- LETTERS[i]
     .<-sub(p[i],L %.% "EF",  . , fixed=TRUE)
     .<-sub(p[j],paste0("H, SQK='",L,"'G;"),., fixed=TRUE)
     }
     . <- sub("E", "(", .)   # E F G H converted back to brackets
     . <- sub("F", "{", .)
     . <- sub("G", ")", .)
     . <- sub("H", "}", .)
     . <- paste(., collapse="")
     . <- gsub(";)", ")", ., fixed=TRUE)
     . <- gsub(";,", ",", ., fixed=TRUE)
     if(CAT) print(.)
     v <- try(eval(parse(text=.)), silent=!CAT)
     if(inherits(v, "try-error")) FALSE else TRUE
     } 
    

For example, () is converted into A({}, SQK='A'); the SQK argument is "A"; the function A recognizes this as the right squeak and returns TRUE. However, (] is converted into A({}, SQK='C');, so this is an example of a wrong squeak.

> squeak("(]", CAT=T)
[1] "A({}, SQK='C');"
Error in A({ : WRONG SQUEAK!

It works as expected for the example input:

truthy<-readLines(textConnection("()
[](){}<>
(((())))
({[<>]})
[{()<>()}[]]
[([]{})<{[()<()>]}()>{}]"))
falsy<-readLines(textConnection("(
}
(<2)>
(()()foobar)
[({}<>)>
(((()))"))
> sapply(truthy,squeak)
                      ()                 [](){}<>                 (((()))) 
                    TRUE                     TRUE                     TRUE 
                ({[<>]})             [{()<>()}[]] [([]{})<{[()<()>]}()>{}] 
                    TRUE                     TRUE                     TRUE 
> sapply(falsy,squeak)
           (            }        (<2)> (()()foobar)     [({}<>)>      (((())) 
       FALSE        FALSE        FALSE        FALSE        FALSE        FALSE 

R, 298

function(.){s=strsplit;u=paste0;.=s(.,"")[[1]];p=s("><)(}{][","")[[1]];.[!.%in%p]="§";for(i in 1:4*2){.[.==p[i]]=sprintf("S('%s',{",p[i]);.[.==p[i-1]]=sprintf("},'%s');",p[i])};S=function(H,B,T)if(H!=T)stop();r=try(eval(parse(,,u(.,collapse=""))),1);if(inherits(r,"try-error"))FALSE else TRUE}

The approach here is to convert the sequence into R code, and then try to parse and evaluate it. If that gives an error, then return FALSE.

But there is a minor problem ... R's rules for brackets are different, so < and > are not brackets at all, and the other types have rules of their own. This is solved by a revolutionary approach -- a squeaking function, whose only function is to signal an error if its head and tail squeak in different ways.

For example, [] is transformed into S('[', {}, ']'), where S is defined as ...

S=function(H,B,T)if(H!=T)stop() 

Because the head squeak and tail squeak match, no error is thrown.

A few other examples (the left part is a sequence of brackets and right part is its transformation into valid R code that can be evaluated):

[}     -->  S('[', {}, '}')     # squeaks an error
[()]   -->  S('[', {S('(',{},'(')}, "[")
({[]}) -->  S('(',{S('{',{S('[',{},'[');},'{');},'(');

Some other sequences of brackets will result in parse errors:

[[)    -->   S('[',{S('[',{},'('); 

So the remaining part just catches the errors and returns FALSE if there are any, and TRUE if there are none.

The human-readble code (contains an extra argument CAT to show the squeaks):

 sqk <- function(.){
   s=strsplit;u=paste0
   .=s(.,"")[[1]]
   p=s("><)(}{][","")[[1]]
   .[!.%in%p]="§"
   for(i in 1:4*2){
     .[.==p[i]]=sprintf("S('%s',{",p[i])
     .[.==p[i-1]]=sprintf("},'%s');",p[i])
   }
   S=function(H,B,T)if(H!=T)stop()
   r=try(eval(parse(,,u(.,collapse=""))),1)
   if(inherits(r,"try-error"))FALSE else TRUE
   }

Applying it on sample cases:

truthy<-readLines(textConnection("()
[](){}<>
(((())))
({[<>]})
[{()<>()}[]]
[([]{})<{[()<()>]}()>{}]"))
falsy<-readLines(textConnection("(
}
(<2)>
(()()foobar)
[({}<>)>
(((()))"))
> sapply(truthy,sqk)
                      ()                 [](){}<>                 (((()))) 
                    TRUE                     TRUE                     TRUE 
                ({[<>]})             [{()<>()}[]] [([]{})<{[()<()>]}()>{}] 
                    TRUE                     TRUE                     TRUE 
> sapply(falsy,sqk)
           (            }        (<2)> (()()foobar)     [({}<>)>      (((())) 
       FALSE        FALSE        FALSE        FALSE        FALSE        FALSE 
2 added 289 characters in body
source | link

This function is the first example of a totally new programming style, squeaking brackets. The idea is that the closing bracket will squeak to the opening bracket, and each type of opening brackets will only accept a certain type of squeaks. Otherwise, the brackets are converted to R code and evaluated, so there may be syntax errors -- these will also result in FALSE.

It works like this:

This function is the first example of a totally new programming style, squeaking brackets. It works like this:

This function is the first example of a totally new programming style, squeaking brackets. The idea is that the closing bracket will squeak to the opening bracket, and each type of opening brackets will only accept a certain type of squeaks. Otherwise, the brackets are converted to R code and evaluated, so there may be syntax errors -- these will also result in FALSE.

It works like this:

1
source | link