Re: [racket-users] predicate as an atom within a regexp?

2016-03-19 Thread Alex Knauth
There's a string-append match expander for that here, although it's pretty slow 
right now.
http://docs.racket-lang.org/match-string/index.html 


Alex Knauth

> On Mar 17, 2016, at 7:17 PM, Matthew Butterick  > wrote:
> 
> Racket's `match` is not far from this. In fact maybe I shouldn't be thinking 
> in terms of hacking regexps, but rather making a new match expander. For 
> instance, you can destructure a list like so:
> 
> (match '("foo" "-42.3" "bar")
>  [(list a (? string->number b) c) 'yay]
>  [else 'boo])
> 
> So it would extend logically to this pseudocode:
> 
> (match "foo-42.3bar"
>  [(string-append a (? string->number b) c) 'yay]
>  [else 'boo])

-- 
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] predicate as an atom within a regexp?

2016-03-19 Thread Andrew Gwozdziewycz
On Thu, Mar 17, 2016 at 1:27 PM, Matthew Butterick  wrote:

> What is the best approach to destructuring strings using predicates? For
> instance "match a substring for which `string->number` is true."
>
>
I am imagining an API that utilizes SCSH style regexes but allows you to do
something like this (fictional):

(define-values (area prefix line) (sre-match (: (= digits 3) (_? "-") (=
digits 3) (_? "-") (= digits 3

The `_` at the head of each component would essentially operate like `_`
does in structural pattern matching (e.g. ignore), but give you the ability
to specify a regexp that you are ignoring.

This may not be exactly what you are thinking of, but it's the half baked
idea that came to mind... The API, would also be pretty damn ugly.

-- 
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] predicate as an atom within a regexp?

2016-03-18 Thread Daniel Prager
Hi Matthew

Do you want the leftmost, longest substring matching the predicate?

E.g. something like (substring/p string->number "foo-46.3bar12789") returns
"-46.3"?

Dan

-- 
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] predicate as an atom within a regexp?

2016-03-18 Thread Andrew Gwozdziewycz
On Thu, Mar 17, 2016 at 4:17 PM, Matthew Butterick  wrote:

>
> On Mar 17, 2016, at 2:05 PM, Andrew Gwozdziewycz  wrote:
>
> I am imagining an API that utilizes SCSH style regexes but allows you to
> do something like this (fictional):
>
> (define-values (area prefix line) (sre-match (: (= digits 3) (_? "-") (=
> digits 3) (_? "-") (= digits 3
>
>
>
> Racket's `match` is not far from this. In fact maybe I shouldn't be
> thinking in terms of hacking regexps, but rather making a new match
> expander. For instance, you can destructure a list like so:
>
> (match '("foo" "-42.3" "bar")
>   [(list a (? string->number b) c) 'yay]
>   [else 'boo])
>
> So it would extend logically to this pseudocode:
>
> (match "foo-42.3bar"
>   [(string-append a (? string->number b) c) 'yay]
>   [else 'boo])
>

Yup! Exactly. I really had `match` in mind. I'm not sure why you wouldn't
use regexes underneath though. As you have it, you'd be forced to call
`string->number` on every subsequence, where as constructing a regex with
grouping seemingly does this in effectively 1 pass (with possible back
tracking of course).

Still, the idea is extremely solid, and I would use such a thing
constantly.

-- 
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] predicate as an atom within a regexp?

2016-03-18 Thread Matthew Butterick

On Mar 17, 2016, at 2:05 PM, Andrew Gwozdziewycz  wrote:
> I am imagining an API that utilizes SCSH style regexes but allows you to do 
> something like this (fictional):
> 
> (define-values (area prefix line) (sre-match (: (= digits 3) (_? "-") (= 
> digits 3) (_? "-") (= digits 3


Racket's `match` is not far from this. In fact maybe I shouldn't be thinking in 
terms of hacking regexps, but rather making a new match expander. For instance, 
you can destructure a list like so:

(match '("foo" "-42.3" "bar")
  [(list a (? string->number b) c) 'yay]
  [else 'boo])

So it would extend logically to this pseudocode:

(match "foo-42.3bar"
  [(string-append a (? string->number b) c) 'yay]
  [else 'boo])



On Mar 17, 2016, at 2:35 PM, Daniel Prager  wrote:
> Do you want the leftmost, longest substring matching the predicate?
> 
Sometimes, but that's not hard to figure out. More often, I want to match 
multiple predicates within one string.
 

#lang racket

(define (substring/p pred str)
  (for*/first ([strlen (in-value (string-length str))]
   [left-pos (in-range strlen)]
   [right-pos (in-range strlen left-pos -1)]
   [this-substring (in-value (substring str left-pos right-pos))]
   #:when (pred this-substring))
  this-substring))

(equal? (substring/p string->number "foo-46.3bar12789") "-46.3")

-- 
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] predicate as an atom within a regexp?

2016-03-18 Thread Matthew Butterick
What is the best approach to destructuring strings using predicates? For 
instance "match a substring for which `string->number` is true." 

I can approximate this using `regexp-match-positions` and stepping through 
possible substrings, testing until one matches, etc. 

But I notice that this amounts to treating a predicate as an atom. Since atoms 
like '\\d' and '\\w' are pretty much predicates in disguise, it made me wonder 
if there were a way to atom-ize an arbitrary predicate.

-- 
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.