Skip to content

Commit

Permalink
Merge pull request #5 from greymd/feature/refactoring
Browse files Browse the repository at this point in the history
Refactoring
  • Loading branch information
greymd committed May 19, 2016
2 parents 4e717fa + b063717 commit 8091358
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 67 deletions.
70 changes: 36 additions & 34 deletions lib/egzact/filters.egi
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,15 @@
(lambda [$num $line]
(let {[$min-num (min [num (length line)])]}
(match-all line (list string)
[(loop $i [1 min-num]
<join _ <cons $a_i ...>>
_)
(map (1#a_%1 $) (between 1 min-num))]))))
[(loop $i [1 min-num] <join _ <cons $a_i ...>> _)
(map (1#a_%1 $) (between 1 min-num))]))))

(define $filter-perm
(lambda [$num $line]
(let {[$min-num (min [num (length line)])]}
(match-all line (multiset string)
[(loop $i [1 min-num]
<cons $a_i ...>
_)
(map (1#a_%1 $) (between 1 min-num))]))))
[(loop $i [1 min-num] <cons $a_i ...> _)
(map (1#a_%1 $) (between 1 min-num))]))))

(define $filter-conv-each
(lambda [$num $line]
Expand All @@ -51,9 +47,7 @@
(define $filter-conv
(lambda [$num $line]
(match-all line (list string)
[<join _ (loop $i [1 num]
<cons $a_i ...>
_) >
[<join _ (loop $i [1 num] <cons $a_i ...> _) >
(map (1#a_%1 $) (between 1 num))])))

(define $filter-dupl
Expand All @@ -62,12 +56,13 @@

(define $filter-flat
(lambda [$num $line]
(match line (list string) {
[(loop $i [1 num] <cons $a_i ...> $xs)
{(map 1#a_%1 (between 1 num)) @(filter-flat num xs)}]
[<join $x <cons $xs <nil>>> {(append x {xs})}]
[<nil> {}]
})))
(match line (list string)
{
[(loop $i [1 num] <cons $a_i ...> $xs)
{(map 1#a_%1 (between 1 num)) @(filter-flat num xs)}]
[<join $x <cons $xs <nil>>> {(append x {xs})}]
[<nil> {}]
})))

(define $filter-slit
(lambda [$num $line]
Expand All @@ -77,11 +72,10 @@
{
[<nil> {}]
[(loop $n [1 (car part-nums)] <cons $a_n ...> $rest)
[(concat
{{(map (1#a_%1 $) (between 1 (car part-nums)))}
(exec-partition (cdr part-nums) rest)})]]
}))
]}
(concat
{{(map (1#a_%1 $) (between 1 (car part-nums)))}
(exec-partition (cdr part-nums) rest)})]
}))]}
(exec-partition
(equalized-partition-nums (length line) num)
line))))
Expand Down Expand Up @@ -127,17 +121,21 @@

(define $filter-takelx
(lambda [$str $line]
{(match line (list string) {[<join $left <cons (& $elem ?1#(not (empty? (regex str %1))) !<nil>) _>>
(append left {elem})]
[_ {}] ;If there is no mached patterns.
})}))
{(match line (list string)
{
[<join $left <cons (& $elem ?1#(not (empty? (regex str %1))) !<nil>) _>>
(append left {elem})]
[_ {}] ;If there is no mached patterns.
})}))

(define $filter-takerx
(lambda [$str $line]
{(match line (list string) {[<nioj $right <snoc (& $elem ?1#(not (empty? (regex str %1))) !<nil>) _>>
(append {elem} right)]
[_ {}] ;If there is no mached patterns.
})}))
{(match line (list string)
{
[<nioj $right <snoc (& $elem ?1#(not (empty? (regex str %1))) !<nil>) _>>
(append {elem} right)]
[_ {}] ;If there is no mached patterns.
})}))

(define $filter-dropl
(lambda [$num $line]
Expand All @@ -151,8 +149,10 @@

(define $filter-zrep
(lambda [$str $line]
{(match line (list string) {[ <join $left <cons (& $elem ?1#(not (empty? (regex str %1))) !<nil>) $right>>
(concat {{elem} @(filter-zrep str right)})]
{(match line (list string)
{
[ <join $left <cons (& $elem ?1#(not (empty? (regex str %1))) !<nil>) $right>>
(concat {{elem} @(filter-zrep str right)})]
[_ {}] ;If there is no mached patterns.
})}))

Expand All @@ -167,8 +167,10 @@

(define $filter-wrap-impl
(lambda [$pre $suf $line]
{(match line (list string) {[ <join _ <cons $elem $right>>
(concat {{(S.concat {pre elem suf})} @(filter-wrap-impl pre suf right)})]
{(match line (list string)
{
[ <join _ <cons $elem $right>>
(concat {{(S.concat {pre elem suf})} @(filter-wrap-impl pre suf right)})]
[_ {}] ;If there is no mached patterns.
})}))

Expand Down
69 changes: 36 additions & 33 deletions lib/egzact/utils.egi
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@
[_ #f]
})))


(define $num-validation
(lambda [$num]
(match {num} (list integer) {
[<cons (& $n ?(integer? $) ?(gt? $ 0)) _> [(min n ARGMAX)]]
[<cons (& $n ?(string? $)) _> [(num-validation (read n))]]
[_ ARGMAX]
})))
(match {num} (list integer)
{
[<cons (& $n ?(integer? $) ?(gt? $ 0)) _> [(min n ARGMAX)]]
[<cons (& $n ?(string? $)) _> [(num-validation (read n))]]
[_ ARGMAX]
})))

(define $option-usage
(do {
Expand All @@ -49,7 +49,6 @@
(print " eos=STR End of set.")
}))


(define $execution
(lambda [$this-printer $this-filter $this-opts $this-arg $this-input]
(let {[[$IFS $OFS $EOR $EOS $EOF]
Expand Down Expand Up @@ -116,10 +115,12 @@

; Convert string collection to hash {"P1=A" "P2=B"} => {| ["P1" "A"] ["P2" "B"] |}
(define $opts2hash
(lambda [$args] (read (S.concat {"{| " (join-string " "
(map 3#(show [%1 %3])
(map 1#(car (regex "=" %1))
args))) " |}"}))))
(lambda [$args] (read (S.concat {"{| "
(join-string " "
(map 3#(show [%1 %3])
(map 1#(car (regex "=" %1))
args)))
" |}"}))))

; Convert two-dimensional-collection to string
; (proc "-" "@" {{"a" "b" "c"} {"d" "e" "f"} {"g"}}) => "a-b-c@d-e-f@g"
Expand All @@ -131,11 +132,12 @@
; (proc "," {"a" "b" "c"}); => "a,b,c"
(define $join-string
(lambda [$fs $ls]
(match ls (list string) {
[<nil> ""] ; in case of empty element
[<cons (& $lst ?(string? $)) <nil>> lst]
[<cons $x $xs> [(S.append (S.append x fs) (join-string fs xs))]]
})))
(match ls (list string)
{
[<nil> ""] ; in case of empty element
[<cons (& $lst ?(string? $)) <nil>> lst]
[<cons $x $xs> [(S.append (S.append x fs) (join-string fs xs))]]
})))

; new split-string function which can deal with empty separator
(define $S.split2
Expand All @@ -147,31 +149,32 @@
; (proc " " {"a b c" "1 2 3"}) => {"a" "b" "c" "1" "2" "3"}
(define $split-whole-input
(lambda [$str $line]
(match line (list string) {
[<nil> {}]
[<cons $x $xs>
(concat {(S.split2 str x) (split-whole-input str xs)})]
})))

(match line (list string)
{
[<nil> {}]
[<cons $x $xs>
(concat {(S.split2 str x) (split-whole-input str xs)})]
})))

; (proc " " {"a b c" "1 2 3"}) => {{"a" "b" "c"} {"1" "2" "3"}}
; (proc "" {"a b c" "1 2 3"}) => {{"a" " " "b" " " "c"} {"1" " " "2" " " "3"}}
(define $split-each-line
(lambda [$str $line]
(match line (list string) {
[<nil> {}]
[<cons $x $xs>
(append {(S.split2 str x)} (split-each-line str xs))]
})))
(match line (list string)
{
[<nil> {}]
[<cons $x $xs>
(append {(S.split2 str x)} (split-each-line str xs))]
})))

; The behavior is same as intersperse.
; But it works even the input is infinite stream.
; (proc "---" {"a b c" "1 2 3" "e f g"}) => {"a b c" "---" "1 2 3" "---" "e f g"}
(define $intersperse2
(lambda [$str $line]
(match line (list string){
[<cons $x <nil>> ;last element
{x}]
[<cons $x $xs> ;first and before last element
(append (append {x} {str}) (intersperse2 str xs))]
})))
(match line (list string)
{
[<cons $x <nil>> {x}] ;last element
[<cons $x $xs>
(append (append {x} {str}) (intersperse2 str xs))] ;first and before last element
})))

0 comments on commit 8091358

Please sign in to comment.