[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.lisp

Re: The Right Way to process streams, strings, etc. etc.

William James

7/25/2015 11:39:00 PM

Erann Gat wrote:

> The recent thread on translating strings made me give some though to the
> Right Way to do these things, and I've decided that the Right Way is to
> generalize the functionality of the MAP function so that it works for
> streams as well as sequences, e.g.:
>
> (defmethod gmap (type f (s sequence)) (map type f s))
>
> (defmethod gmap (type f (s stream))
> (map type f (read-all-characters-from-stream s)))
>
> Of course, you'd want a more efficient version that didn't actually read
> the entire contents of the stream into memory, e.g.:
>
> (defun stream-char-generator (s)
> (fn () (or (read-char s nil nil) (values nil t))))
>
> (defun walk-generator (gen f)
> (loop
> (receive (elt done?) (funcall gen)
> (if done? (return nil))
> (funcall f elt))))
>
> (defmethod gmap (type f (s stream))
> (gmap type f (stream-char-generator s)))
>
> (defmethod gmap (type f (g function))
> (cond
> ( (null type) (walk-generator g f) )
> ( (subtypep type 'list)
> (with-collector collect
> (walk-generator g (fn (c) (collect (funcall f c))))) )
> ( (subtypep type 'string)
> (with-output-to-string (out)
> (walk-generator g (fn (c) (princ (funcall f c) out)))) )
> ( (subtypep type 'vector)
> (let ( (v (make-array 0 :adjustable t :fill-pointer t)) )
> (walk-generator g (fn (c) (vector-push-extend c v)))
> v) )
> (t (error "~S is not a valid output specifier for ->" type))))
>
> Now you can do:
>
> ? (gmap nil 'print "foo")
>
> #\f
> #\o
> #\o
> NIL


Gauche Scheme:

(use gauche.sequence)
(for-each print "foo")
f
o
o
#t


> ? (gmap nil 'print (make-string-input-stream "foo"))
>
> #\f
> #\o
> #\o
> NIL
> ?


(with-input-from-string "foo" (cut generator-for-each print read-char))
===>
f
o
o
#<undef>


>
> You can even walk over stream entities other than characters:
>
> (defun stream-line-generator (s)
> (fn () (or (read-line s nil nil) (values nil t))))
>
> ? (gmap 'list 'identity
> (stream-line-generator
> (make-string-input-stream "foo
> baz
> bar")))
> ("foo" "baz" "bar")


(with-input-from-string "foo
bar
baz"
(cut generator-map values read-line))
===>
("foo" "bar" "baz")


>
> And all this in under thirty minutes. Damn, Lisp is cool!
>
> E.
>
> P.S. To run this code you'll also need:
>
> (defmacro fn (args &body body) `(lambda ,args ,@body))

--
[Jesse Jackson] would spit into the food of white patrons he hated and then
smilingly serve it to them. He did this, he said, "because it gave me
psychological gratification." -- Life Magazine, 1969-11-29
Blacks are an estimated 39 times more likely to commit a violent crime
against a white than vice versa, and 136 times more likely to commit robbery.
www.colorofcrime.com/2005/10/the-color-of-crime-2005/
5 Answers

Coptic Priest Zakaria Boutros ( Botros ) , the instigator for the film Innocences of the M

1/17/2014 6:59:00 AM

0

well

Either
you have a Hakka Chinese Taksin family dominates
everything in Thai' s Land .

or
you have the Thai general who use Thai King ' s name
to
make sure a Hakka Chinese family Taksin
is REALLY out of Politics .






n Thursday, January 16, 2014 10:02:57 PM UTC-8, Hir...@Hiroko.happyclub.com..jp wrote:
> Thanks, that is exactly what it was and is now. I also think that if the
>
> people of Thailand cannot accept the current democracy of one-man-one-vote
>
> election, it is better to have military rule over them than to be ruled by
>
> warlord.
>
>
>
>
>
>
>
>
>
> "AleXX" wrote in message news:lb2k9s$eb9$1@dont-email.me...
>
>
>
> The whole problem is the military. They sided with the Democratic Party
>
> elites and the Palace King. At the same time they want the world to see that
>
> rules and laws are still "democratic" in Thailand. Better to see Thailand to
>
> have military rules just like Burma when it was under the rules of the
>
> warlords for decades. Only then will these unlawful gatherings get back on
>
> track to lawful demonstrations.
>
>
>
> ********************************************
>
>
>
> "Hiroko@Hiroko.happyclub.com.jp" wrote in message
>
> news:lau9kj$o8q$1@dont-email.me...
>
>
>
>
>
> Protest leader Suthep Thaugsuban has promised to close the city tomorrow,
>
> but what does he hope to achieve and how will it affect Bangkokians?
>
>
>
>
>
> http://www.bangkokpost.com/news/investigation/389138/roads-to-...
>
>
>
> Please credit and share this article with others using this
>
> link:http://www.bangkokpost.com/news/investigation/389138/roads-to-....
>
> View our policies at http://go... and http://go.... © Post
>
> Publishing PCL. All rights reserved.

rst9

1/17/2014 3:39:00 PM

0

On Thursday, January 16, 2014 10:58:39 PM UTC-8, Jesus 's Mother Mary copied Gaia Godeese of Earth wrote:

So who is really corrupt?

Hiroko@Hiroko.happyclub.com.jp

1/17/2014 5:52:00 PM

0


Maybe for the peace of Hakka Thai Chinese in Thailand, let them have the
military general that runs the country. She can then sit back and watch at
them.





"Jesus 's Mother Mary copied Gaia Godeese of Earth" wrote in message
news:480848bf-7c90-4c1d-8043-af7565eb7122@googlegroups.com...

well

Either
you have a Hakka Chinese Taksin family dominates
everything in Thai' s Land .

or
you have the Thai general who use Thai King ' s name
to
make sure a Hakka Chinese family Taksin
is REALLY out of Politics .






n Thursday, January 16, 2014 10:02:57 PM UTC-8,
Hir...@Hiroko.happyclub.com.jp wrote:
> Thanks, that is exactly what it was and is now. I also think that if the
>
> people of Thailand cannot accept the current democracy of
> one-man-one-vote
>
> election, it is better to have military rule over them than to be ruled by
>
> warlord.
>
>
>
>
>
>
>
>
>
> "AleXX" wrote in message news:lb2k9s$eb9$1@dont-email.me...
>
>
>
> The whole problem is the military. They sided with the Democratic Party
>
> elites and the Palace King. At the same time they want the world to see
> that
>
> rules and laws are still "democratic" in Thailand. Better to see Thailand
> to
>
> have military rules just like Burma when it was under the rules of the
>
> warlords for decades. Only then will these unlawful gatherings get back on
>
> track to lawful demonstrations.
>
>
>
> ********************************************
>
>
>
> "Hiroko@Hiroko.happyclub.com.jp" wrote in message
>
> news:lau9kj$o8q$1@dont-email.me...
>
>
>
>
>
> Protest leader Suthep Thaugsuban has promised to close the city tomorrow,
>
> but what does he hope to achieve and how will it affect Bangkokians?
>
>
>
>
>
> http://www.bangkokpost.com/news/investigation/389138/roads-to-...
>
>
>
> Please credit and share this article with others using this
>
> link:http://www.bangkokpost.com/news/investigation/389138/roads-to-....
>
> View our policies at http://go... and http://go.... ? Post
>
> Publishing PCL. All rights reserved.

Coptic Priest Zakaria Boutros ( Botros ) , the instigator for the film Innocences of the M

1/17/2014 7:40:00 PM

0

Thai King wins




On Friday, January 17, 2014 7:38:44 AM UTC-8, rst9 wrote:
> On Thursday, January 16, 2014 10:58:39 PM UTC-8, Jesus 's Mother Mary copied Gaia Godeese of Earth wrote:
>
>
>
> So who is really corrupt?

Coptic Priest Zakaria Boutros ( Botros ) , the instigator for the film Innocences of the M

1/17/2014 7:41:00 PM

0

Thai King afraid Chinese Hakka family Taksin will rule Thai ' s Land .



On Friday, January 17, 2014 7:38:44 AM UTC-8, rst9 wrote:
> On Thursday, January 16, 2014 10:58:39 PM UTC-8, Jesus 's Mother Mary copied Gaia Godeese of Earth wrote:
>
>
>
> So who is really corrupt?