Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

BOFH excuse #335: the AA battery in the wallclock sends magnetic interference


comp / comp.lang.lisp / Re: Beginner code - splitting lines on whitespace

SubjectAuthor
* Re: Beginner code - splitting lines on whitespaceB. Pym
`- Re: Beginner code - splitting lines on whitespaceB. Pym

1
Subject: Re: Beginner code - splitting lines on whitespace
From: B. Pym
Newsgroups: comp.lang.lisp
Organization: A noiseless patient Spider
Date: Sat, 20 Jul 2024 08:56 UTC
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Nobody447095@here-nor-there.org (B. Pym)
Newsgroups: comp.lang.lisp
Subject: Re: Beginner code - splitting lines on whitespace
Date: Sat, 20 Jul 2024 08:56:27 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 58
Message-ID: <v7fu3n$3ffis$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Injection-Date: Sat, 20 Jul 2024 10:56:27 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="ccef5f04218b39d638a08dde3da2f478";
logging-data="3653212"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/TY/JjZRX6oXenXf9vDRif"
User-Agent: XanaNews/1.18.1.6
Cancel-Lock: sha1:7Ix3Ld4MihYb1JrZxM2aKcEzhR0=
View all headers

> (defun white-space-p (c)
> (or (char= c #\Space) (char= c #\Tab)))
>
> (defun split-on-space (string)
> (loop
> for b = (position-if-not #'white-space-p string)
> then (position-if-not #'white-space-p string :start e)
> for e = (when b (position-if #'white-space-p string :start b))
> while b
> collect (subseq string b e)
> while e))

Gauche Scheme

(use srfi-13) ;; string ops.

(define (white-space? c) (member c '(#\space #\tab)))

(define (split-on-space str :optional (start 0))
(let1 b (and start (string-skip str white-space? start))
(if b
(let1 e (string-index str white-space? b)
(cons (string-copy str b e) (split-on-space str e)))
'())))

(split-on-space " foo bar ")
===>
("foo" "bar")

(split-on-space "foo")
===>
("foo")

(split-on-space "")
===>
()

Another way:

(define-method object-apply ((s <string>) (i <integer>) j)
(string-copy s i j))

(define (split-on-space str :optional (start 0))
(let1 b (and start (string-skip str white-space? start))
(if b
(let1 e (string-index str white-space? b)
(cons (str b e) (split-on-space str e)))
'())))

The easy way.

(use srfi-13)

(define (split-on-space str)
(string-tokenize str))

Subject: Re: Beginner code - splitting lines on whitespace
From: B. Pym
Newsgroups: comp.lang.lisp
Organization: A noiseless patient Spider
Date: Mon, 22 Jul 2024 13:25 UTC
References: 1
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Nobody447095@here-nor-there.org (B. Pym)
Newsgroups: comp.lang.lisp
Subject: Re: Beginner code - splitting lines on whitespace
Date: Mon, 22 Jul 2024 13:25:51 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 111
Message-ID: <v7lmkp$lo3u$1@dont-email.me>
References: <v7fu3n$3ffis$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Injection-Date: Mon, 22 Jul 2024 15:25:51 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="daacc4272803ca921008e0762ddc3b22";
logging-data="712830"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/2VkbxNKthYMvebD9kEVl8"
User-Agent: XanaNews/1.18.1.6
Cancel-Lock: sha1:lC+mq3imOtArkIvglXtMd4gFBdI=
View all headers

B. Pym wrote:

> > (defun white-space-p (c)
> > (or (char= c #\Space) (char= c #\Tab)))
> >
> > (defun split-on-space (string)
> > (loop
> > for b = (position-if-not #'white-space-p string)
> > then (position-if-not #'white-space-p string :start e)
> > for e = (when b (position-if #'white-space-p string :start b))
> > while b
> > collect (subseq string b e)
> > while e))
>
> Gauche Scheme
>
> (use srfi-13) ;; string ops.
>
> (define (white-space? c) (member c '(#\space #\tab)))
>
> (define (split-on-space str :optional (start 0))
> (let1 b (and start (string-skip str white-space? start))
> (if b
> (let1 e (string-index str white-space? b)
> (cons (string-copy str b e) (split-on-space str e)))
> '())))
>
> (split-on-space " foo bar ")
> ===>
> ("foo" "bar")
>
> (split-on-space "foo")
> ===>
> ("foo")
>
> (split-on-space "")
> ===>
> ()
>
>
> Another way:
>
>
> (define-method object-apply ((s <string>) (i <integer>) j)
> (string-copy s i j))
>
> (define (split-on-space str :optional (start 0))
> (let1 b (and start (string-skip str white-space? start))
> (if b
> (let1 e (string-index str white-space? b)
> (cons (str b e) (split-on-space str e)))
> '())))
>
>
> The easy way.
>
> (use srfi-13)
>
> (define (split-on-space str)
> (string-tokenize str))

Another way.

Gauche Scheme

(use srfi-13) ;; string ops.
(use gauche.generator)

(define (white-space? c) (member c '(#\space #\tab)))

(define (make-token-generator str)
(let ((str str)
(start 0)
(end 0))
(lambda ()
(set! start
(and start end (string-skip str white-space? end)))
(if start
(begin
(set! end (string-index str white-space? start))
(string-copy str start end))
(eof-object)))))

(define (split-on-space str)
(generator->list (make-token-generator str)))

(split-on-space "")
===>
()

(split-on-space " ")
===>
()

(split-on-space "foo")
===>
("foo")

(split-on-space " foo ")
===>
("foo")

(split-on-space " foo bar ")
===>
("foo" "bar")

(split-on-space " 3.14 foo? [bar] ")
===>
("3.14" "foo?" "[bar]")

1

rocksolid light 0.9.8
clearnet tor