Skip to content

Instantly share code, notes, and snippets.

@toomasv
Last active October 18, 2019 18:24
Show Gist options
  • Save toomasv/8562fb66e0b8dc4ef9b0b51dc280cbd4 to your computer and use it in GitHub Desktop.
Save toomasv/8562fb66e0b8dc4ef9b0b51dc280cbd4 to your computer and use it in GitHub Desktop.
`find` with wildcards
Red [
Author: "Toomas Vooglaid"
Date: 2018-08-04
Last: 2018-08-04
Purpose: "Adding wildcards to `find`"
]
find': function [series 'value /part length /only /case /same /any /with wild /skip size /last /reverse /tail /match][
system/words/case [
any [
out: none
cmd: copy [append to-path 'parse]
refinements: collect [foreach i [case part][if get i [keep i]]]
cmd: reduce append/only cmd refinements
append/only cmd series
rule: parse value r: [
collect some [
#"?" keep ('skip)
| #"*" keep ('thru) [end keep ('end) | r]
| keep to [#"?" | #"*" | end ]
]
]
append rule to-set-word 'e
append/only rule append to-paren [out:] either match or tail ['e]['s]
append rule either last [[(l: next l) :l]]['break]
append/only cmd compose/deep/only either match [[s: l: (rule)]][[some [s: l: (rule) | skip]]]
if part [append cmd length]
do cmd
out
]
with []; TBD
'else [
cmd: copy [append to-path 'find]
refinements: collect [foreach i [only case same last reverse tail match][if get i [keep i]]]
args: make block! 2
system/words/case/all [
part [append refinements 'part append args length]
skip [append refinements 'skip append args size]
]
cmd: reduce append/only cmd refinements
append/only cmd series
append append cmd :value args
do cmd
]
]
]
; Examples
comment [
find'/any "abracadabra" "br*"
;== "bracadabra"
find'/any/last "abracadabra" "br*"
;== "bra"
find'/any "abracadabra" "b?a"
;== "bracadabra"
find'/any/last/tail "abracadabra" "ab"
;== "ra"
find'/any/match "abracadabra" "a*r"
;== "acadabra"
find'/any "abracadabra" "a?a*r"
;== "acadabra"
find'/any/last "abracadabra" "a?a*r"
;== "adabra"
]
@toomasv
Copy link
Author

toomasv commented Aug 4, 2018

Adds standard wildcards to find under refinement /any.
Works with other refinements /last, /match, /tail, /part and /case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment