Re: [racket-users] Help needed in file processing idioms

2016-08-20 Thread WarGrey Gyoudmon Ju
see docs for (read-line)

On Sat, Aug 20, 2016 at 5:16 PM, Pekka Niiranen 
wrote:

> Thank you guys,
>
>
> both functions work. Looks like single function (for/or) could be used
> if the calling side handles #f, #t and a list.
>
> Another issue:
>
> I am planning to support both Unix and Windows, but have not found
> a way to define 'return-linefeed as system-wide default (aside using
> global variable):
> Whenever one does file-I/O; 'return-linefeed is used as newline.
> I do like not to be forced to give the 3rd parameter (return-linefeed)
> like below
>
> (in-lines inp 'return-linefeed)
>
> -pekka-
>
> On 20/08/16 02:47, Gustavo Massaccesi wrote:
> > Minor fix: In the first function "read-on-row" you must replace
> > for/first with for/last.
> >
> > Gustavo
> >
> > PS for Pekka: In this case it's better to use for/last and for/or, but
> > in more complicated cases you can try with for/fold.
> >
> >
> > On Fri, Aug 19, 2016 at 10:34 AM, WarGrey Gyoudmon Ju
> >  wrote:
> >> (define (read-on-row *params*)
> >>;; Returns the last line in "myfile" which contains
> >>;; substring "ON", otherwise #f
> >>(call-with-input-file (hash-ref *params* "myfile")
> >>  (lambda (inp)
> >>(for/first ([row (in-lines inp 'return-linefeed)]
> >>#:when (string-contains? row "ON"))
> >>   row
> >>
> >> (define (on-row-exists *params*)
> >>;; Returns #t if there is line in "myfile" which contains
> >>;; substring "ON", otherwise #f
> >>(call-with-input-file (hash-ref *params* "myfile")
> >>  (lambda (inp)
> >>(for/or ([row (in-lines inp 'return-linefeed)])
> >>(string-contains? row "ON")
> >>
> >>
> >> On Fri, Aug 19, 2016 at 9:18 PM, Pekka Niiranen <
> pekka.niira...@pp5.inet.fi>
> >> wrote:
> >>> Hello users,
> >>>
> >>> I would like to read lines from a file, process each line
> >>> and return either the success of the operation or the processed line to
> >>> another function located in another module.
> >>>
> >>> Below are two functions that work OK, but are in my opinion "ugly"
> because
> >>> they use "set!".
> >>>
> >>> It there a way to make both functions work without "set!"?
> >>>
> >>>
> >>> ;;
> >>> (define (read-on-row *params*)
> >>> ;; Returns the last line in "myfile" which contains
> >>> ;; substring "ON", otherwise #f
> >>>
> >>>(define pdata #f) ;; I do not like this extra variable!
> >>>
> >>>(call-with-input-file (hash-ref *params* "myfile")
> >>>  (lambda (inp)
> >>>(for/last ([row (in-lines inp 'return-linefeed)]
> >>>   #:when (string-contains? "ON" row))
> >>>  (set! pdata row
> >>>
> >>>pdata)
> >>>
> >>> ;;
> >>>
> >>> (define (on-row-exists *params*)
> >>> ;; Returns #t if there is line in "myfile" which contains
> >>> ;; substring "ON", otherwise #f
> >>>
> >>>(define found-on #f) ;; I do not like this extra variable!
> >>>
> >>>(call-with-input-file (hash-ref *params* "myfile")
> >>>  (lambda (inp)
> >>>(for/last ([row (in-lines inp 'return-linefeed)]
> >>>   #:when (string-contains? "ON" row))
> >>>  (set! found-on #t
> >>>
> >>>found-on)
> >>>
> >>> -pekka-
> >>>
> >>>
> >>> --
> >>> You received this message because you are subscribed to the Google
> Groups
> >>> "Racket Users" group.
> >>> To unsubscribe from this group and stop receiving emails from it, send
> an
> >>> email to racket-users+unsubscr...@googlegroups.com.
> >>> For more options, visit https://groups.google.com/d/optout.
> >>
> >> --
> >> You received this message because you are subscribed to the Google
> Groups
> >> "Racket Users" group.
> >> To unsubscribe from this group and stop receiving emails from it, send
> an
> >> email to racket-users+unsubscr...@googlegroups.com.
> >> For more options, visit https://groups.google.com/d/optout.
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to racket-users+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Help needed in file processing idioms

2016-08-20 Thread Pekka Niiranen

Thank you guys,


both functions work. Looks like single function (for/or) could be used
if the calling side handles #f, #t and a list.

Another issue:

I am planning to support both Unix and Windows, but have not found
a way to define 'return-linefeed as system-wide default (aside using
global variable):
Whenever one does file-I/O; 'return-linefeed is used as newline.
I do like not to be forced to give the 3rd parameter (return-linefeed)
like below

(in-lines inp 'return-linefeed)

-pekka-

On 20/08/16 02:47, Gustavo Massaccesi wrote:
> Minor fix: In the first function "read-on-row" you must replace
> for/first with for/last.
>
> Gustavo
>
> PS for Pekka: In this case it's better to use for/last and for/or, but
> in more complicated cases you can try with for/fold.
>
>
> On Fri, Aug 19, 2016 at 10:34 AM, WarGrey Gyoudmon Ju
>  wrote:
>> (define (read-on-row *params*)
>>;; Returns the last line in "myfile" which contains
>>;; substring "ON", otherwise #f
>>(call-with-input-file (hash-ref *params* "myfile")
>>  (lambda (inp)
>>(for/first ([row (in-lines inp 'return-linefeed)]
>>#:when (string-contains? row "ON"))
>>   row
>>
>> (define (on-row-exists *params*)
>>;; Returns #t if there is line in "myfile" which contains
>>;; substring "ON", otherwise #f
>>(call-with-input-file (hash-ref *params* "myfile")
>>  (lambda (inp)
>>(for/or ([row (in-lines inp 'return-linefeed)])
>>(string-contains? row "ON")
>>
>>
>> On Fri, Aug 19, 2016 at 9:18 PM, Pekka Niiranen 


>> wrote:
>>> Hello users,
>>>
>>> I would like to read lines from a file, process each line
>>> and return either the success of the operation or the processed line to
>>> another function located in another module.
>>>
>>> Below are two functions that work OK, but are in my opinion "ugly" 
because

>>> they use "set!".
>>>
>>> It there a way to make both functions work without "set!"?
>>>
>>>
>>> ;;
>>> (define (read-on-row *params*)
>>> ;; Returns the last line in "myfile" which contains
>>> ;; substring "ON", otherwise #f
>>>
>>>(define pdata #f) ;; I do not like this extra variable!
>>>
>>>(call-with-input-file (hash-ref *params* "myfile")
>>>  (lambda (inp)
>>>(for/last ([row (in-lines inp 'return-linefeed)]
>>>   #:when (string-contains? "ON" row))
>>>  (set! pdata row
>>>
>>>pdata)
>>>
>>> ;;
>>>
>>> (define (on-row-exists *params*)
>>> ;; Returns #t if there is line in "myfile" which contains
>>> ;; substring "ON", otherwise #f
>>>
>>>(define found-on #f) ;; I do not like this extra variable!
>>>
>>>(call-with-input-file (hash-ref *params* "myfile")
>>>  (lambda (inp)
>>>(for/last ([row (in-lines inp 'return-linefeed)]
>>>   #:when (string-contains? "ON" row))
>>>  (set! found-on #t
>>>
>>>found-on)
>>>
>>> -pekka-
>>>
>>>
>>> --
>>> You received this message because you are subscribed to the Google 
Groups

>>> "Racket Users" group.
>>> To unsubscribe from this group and stop receiving emails from it, 
send an

>>> email to racket-users+unsubscr...@googlegroups.com.
>>> For more options, visit https://groups.google.com/d/optout.
>>
>> --
>> You received this message because you are subscribed to the Google 
Groups

>> "Racket Users" group.
>> To unsubscribe from this group and stop receiving emails from it, 
send an

>> email to racket-users+unsubscr...@googlegroups.com.
>> For more options, visit https://groups.google.com/d/optout.


--
You received this message because you are subscribed to the Google Groups "Racket 
Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Help needed in file processing idioms

2016-08-19 Thread Gustavo Massaccesi
Minor fix: In the first function "read-on-row" you must replace
for/first with for/last.

Gustavo

PS for Pekka: In this case it's better to use for/last and for/or, but
in more complicated cases you can try with for/fold.


On Fri, Aug 19, 2016 at 10:34 AM, WarGrey Gyoudmon Ju
 wrote:
> (define (read-on-row *params*)
>   ;; Returns the last line in "myfile" which contains
>   ;; substring "ON", otherwise #f
>   (call-with-input-file (hash-ref *params* "myfile")
> (lambda (inp)
>   (for/first ([row (in-lines inp 'return-linefeed)]
>   #:when (string-contains? row "ON"))
>  row
>
> (define (on-row-exists *params*)
>   ;; Returns #t if there is line in "myfile" which contains
>   ;; substring "ON", otherwise #f
>   (call-with-input-file (hash-ref *params* "myfile")
> (lambda (inp)
>   (for/or ([row (in-lines inp 'return-linefeed)])
>   (string-contains? row "ON")
>
>
> On Fri, Aug 19, 2016 at 9:18 PM, Pekka Niiranen 
> wrote:
>>
>> Hello users,
>>
>> I would like to read lines from a file, process each line
>> and return either the success of the operation or the processed line to
>> another function located in another module.
>>
>> Below are two functions that work OK, but are in my opinion "ugly" because
>> they use "set!".
>>
>> It there a way to make both functions work without "set!"?
>>
>>
>> ;;
>> (define (read-on-row *params*)
>> ;; Returns the last line in "myfile" which contains
>> ;; substring "ON", otherwise #f
>>
>>   (define pdata #f) ;; I do not like this extra variable!
>>
>>   (call-with-input-file (hash-ref *params* "myfile")
>> (lambda (inp)
>>   (for/last ([row (in-lines inp 'return-linefeed)]
>>  #:when (string-contains? "ON" row))
>> (set! pdata row
>>
>>   pdata)
>>
>> ;;
>>
>> (define (on-row-exists *params*)
>> ;; Returns #t if there is line in "myfile" which contains
>> ;; substring "ON", otherwise #f
>>
>>   (define found-on #f) ;; I do not like this extra variable!
>>
>>   (call-with-input-file (hash-ref *params* "myfile")
>> (lambda (inp)
>>   (for/last ([row (in-lines inp 'return-linefeed)]
>>  #:when (string-contains? "ON" row))
>> (set! found-on #t
>>
>>   found-on)
>>
>> -pekka-
>>
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Racket Users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to racket-users+unsubscr...@googlegroups.com.
>> For more options, visit https://groups.google.com/d/optout.
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to racket-users+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Help needed in file processing idioms

2016-08-19 Thread WarGrey Gyoudmon Ju
(define (read-on-row *params*)
  ;; Returns the last line in "myfile" which contains
  ;; substring "ON", otherwise #f
  (call-with-input-file (hash-ref *params* "myfile")
(lambda (inp)
  (for/first ([row (in-lines inp 'return-linefeed)]
  #:when (string-contains? row "ON"))
 row

(define (on-row-exists *params*)
  ;; Returns #t if there is line in "myfile" which contains
  ;; substring "ON", otherwise #f
  (call-with-input-file (hash-ref *params* "myfile")
(lambda (inp)
  (for/or ([row (in-lines inp 'return-linefeed)])
  (string-contains? row "ON")


On Fri, Aug 19, 2016 at 9:18 PM, Pekka Niiranen 
wrote:

> Hello users,
>
> I would like to read lines from a file, process each line
> and return either the success of the operation or the processed line to
> another function located in another module.
>
> Below are two functions that work OK, but are in my opinion "ugly" because
> they use "set!".
>
> It there a way to make both functions work without "set!"?
>
>
> ;;
> (define (read-on-row *params*)
> ;; Returns the last line in "myfile" which contains
> ;; substring "ON", otherwise #f
>
>   (define pdata #f) ;; I do not like this extra variable!
>
>   (call-with-input-file (hash-ref *params* "myfile")
> (lambda (inp)
>   (for/last ([row (in-lines inp 'return-linefeed)]
>  #:when (string-contains? "ON" row))
> (set! pdata row
>
>   pdata)
>
> ;;
>
> (define (on-row-exists *params*)
> ;; Returns #t if there is line in "myfile" which contains
> ;; substring "ON", otherwise #f
>
>   (define found-on #f) ;; I do not like this extra variable!
>
>   (call-with-input-file (hash-ref *params* "myfile")
> (lambda (inp)
>   (for/last ([row (in-lines inp 'return-linefeed)]
>  #:when (string-contains? "ON" row))
> (set! found-on #t
>
>   found-on)
>
> -pekka-
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to racket-users+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[racket-users] Help needed in file processing idioms

2016-08-19 Thread Pekka Niiranen

Hello users,

I would like to read lines from a file, process each line
and return either the success of the operation or the processed line to 
another function located in another module.


Below are two functions that work OK, but are in my opinion "ugly" 
because they use "set!".


It there a way to make both functions work without "set!"?


;;
(define (read-on-row *params*)
;; Returns the last line in "myfile" which contains
;; substring "ON", otherwise #f

  (define pdata #f) ;; I do not like this extra variable!

  (call-with-input-file (hash-ref *params* "myfile")
(lambda (inp)
  (for/last ([row (in-lines inp 'return-linefeed)]
 #:when (string-contains? "ON" row))
(set! pdata row

  pdata)

;;

(define (on-row-exists *params*)
;; Returns #t if there is line in "myfile" which contains
;; substring "ON", otherwise #f

  (define found-on #f) ;; I do not like this extra variable!

  (call-with-input-file (hash-ref *params* "myfile")
(lambda (inp)
  (for/last ([row (in-lines inp 'return-linefeed)]
 #:when (string-contains? "ON" row))
(set! found-on #t

  found-on)

-pekka-


--
You received this message because you are subscribed to the Google Groups "Racket 
Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.