Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mksub return value should be the whole collection #8

Open
miner opened this issue Dec 6, 2013 · 1 comment
Open

mksub return value should be the whole collection #8

miner opened this issue Dec 6, 2013 · 1 comment

Comments

@miner
Copy link
Collaborator

miner commented Dec 6, 2013

The return value from mksub is not the full matching collection. This caused problems for me especially when matching vectors of one item.

user=> (require '[squarepeg.core :as sp])
nil
user=> (def v1 (sp/mksub (sp/mklit 1)))

'user/v1

user=> (v1 [[1]] {} {} {})
{:i (), :b {}, :r 1, :s [1], :m {}}
; expected :r [1]

user=> (def v12 (sp/mkseq (sp/mksub (sp/mklit 1)) (sp/mksub (sp/mklit 2))))

'user/v12

user=> (v12 [[1] [2]] {} {} {})
{:i (), :b {}, :r [1 2], :s [1 2], :m {}}
; expected :r [[1] [2]]

I'm now using a modified version of mksub which returns the full matching collection and tries to maintain the seq/vector distinction of the input. Also, I wanted to match exact sequences so I added sp/end to the sub-rule so that it would reject any extra stuff. I take an optional predicate so that I can restrict my match to vector? or seq? in cases where that matters. You're welcome to use any or all of these changes if you like them.

(require '[squarepeg.core :as sp])
(defn mk-sequential
  ([rule] (mk-sequential sequential? rule))
  ([pred rule]
     (let [coll-rule (if rule (sp/mkseq rule sp/end) sp/end)]
       (fn [input bindings context memo]
         (if (and (seq input) (pred (first input)))
           (let [r (coll-rule (first input) bindings context memo)]
             (if (sp/success? r)
               ;; try to maintain seq/vector distinction of input, :s value is vector
               (let [res (if (seq? (first input)) (seq (:s r)) (:s r))]
                 (sp/succeed res [res] (rest input) (:b r) (:m r)))
               r))
           (sp/fail "Input not a seq." memo))))))
@ericnormand
Copy link
Owner

Hi Steve,

Thanks. I did not have a good use case for this. Yours is good, so I'll go
with what you've got. I'll address it soon.

On Fri, Dec 6, 2013 at 8:33 AM, Steve Miner [email protected]:

The return value from mksub is not the full matching collection. This
caused problems for me especially when matching vectors of one item.

user=> (require '[squarepeg.core :as sp])
nil
user=> (def v1 (sp/mksub (sp/mklit 1)))
#'user/v1
user=> (v1 [[1]] {} {} {})
{:i (), :b {}, :r 1, :s [1], :m {}}
; expected :r [1]

user=> (def v12 (sp/mkseq (sp/mksub (sp/mklit 1)) (sp/mksub (sp/mklit 2))))
#'user/v12
user=> (v12 [[1] [2]] {} {} {})
{:i (), :b {}, :r [1 2], :s [1 2], :m {}}
; expected :r [[1] [2]]

I'm now using a modified version of mksub which returns the full matching
collection and tries to maintain the seq/vector distinction of the input.
Also, I wanted to match exact sequences so I added sp/end to the sub-rule
so that it would reject any extra stuff. I take an optional predicate so
that I can restrict my match to vector? or seq? in cases where that
matters. You're welcome to use any or all of these changes if you like
them.

(require '[squarepeg.core :as sp])(defn mk-sequential
([rule](mk-sequential sequential? rule))
([pred rule](let [coll-rule %28if rule %28sp/mkseq rule sp/end%29 sp/end%29]
%28fn [input bindings context memo]
%28if %28and %28seq input%29 %28pred %28first input%29%29%29
%28let [r %28coll-rule %28first input%29 bindings context memo%29]
%28if %28sp/success? r%29
;; try to maintain seq/vector distinction of input, :s value is vector
%28let [res %28if %28seq? %28first input%29%29 %28seq %28:s r%29%29 %28:s r%29%29]
%28sp/succeed res [res] %28rest input%29 %28:b r%29 %28:m r%29%29%29
r%29%29
%28sp/fail "Input not a seq." memo))))))


Reply to this email directly or view it on GitHubhttps://github.com//issues/8
.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants