Skip to content

Commit

Permalink
Merge pull request #6 from greymd/feature/sublists
Browse files Browse the repository at this point in the history
Feature/sublists
  • Loading branch information
greymd committed May 25, 2016
2 parents 8091358 + 1c91d4a commit 6d68dfd
Show file tree
Hide file tree
Showing 7 changed files with 313 additions and 17 deletions.
52 changes: 42 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
![logo](./img/logo.png)
<p align="center">
<img src="./img/logo.png" />
</p>

# Strengthen weak points of the shell.

### `conv` : Generate [Bi-gram](https://en.wikipedia.org/wiki/N-gram) from the poem.
### Generate [Bi-gram](https://en.wikipedia.org/wiki/N-gram) from the poem.

COMMAND: `conv`

```bash
$ echo "If a man understands a poem, he shall have troubles." | conv 2
Expand All @@ -16,7 +21,9 @@ shall have
have troubles.
```

### `conv` `mirror` `addl` : Create a zip file nested 100 times.
### Create a zip file nested 100 times.

COMMAND: `conv` `mirror` `addl`

```bash
$ echo file {1..100}.zip | conv 2 | mirror | addl "zip " | sh
Expand All @@ -25,7 +32,9 @@ $ unzip -Z -2 100.zip
99.zip
```

### `flat` `stairl` `wrap` `addl` : Calculate the approximation of Napier's Constant
### Calculate the approximation of Napier's Constant

COMMAND: `flat` `stairl` `wrap` `addl`

```bash
$ seq 10 | flat | stairl ofs="*" | flat | wrap ofs="+" '1/(*)' | addl "1+" | bc -l
Expand Down Expand Up @@ -166,7 +175,7 @@ D

### $ `stairl`

Generate subsets of the fields.
Generate sublist of the fields.
Each result matches to the *left* side of the original input.
In most cases, it looks *stairs*.

Expand Down Expand Up @@ -221,7 +230,7 @@ CA CB CC CD

### $ `stairr`

Generate subsets of the fields.
Generate sublist of the fields.
Results match to the *right* side of the original input.
In most cases, it looks *stairs*.

Expand All @@ -233,12 +242,12 @@ B C D
A B C D
```

### $ `subsets`
### $ `sublist`

Generate all the subsets of the fields.
Generate all the sublist of the fields.

```sh
$ echo A B C D | subsets
$ echo A B C D | sublist
A
A B
B
Expand Down Expand Up @@ -266,7 +275,7 @@ C
C D
D

$ echo A B C D | subsets | sort | uniq
$ echo A B C D | sublist | sort | uniq
A
A B
A B C
Expand All @@ -279,6 +288,29 @@ C D
D
```

### $ `subset`

Generate all the subsets of the fields.

```sh
$ echo A B C D | subset
A
B
C
D
A B
A C
B C
A D
B D
C D
A B C
A B D
A C D
B C D
A B C D
```

### $ `crops`
Crop all the patterns which matches given string (regular expression).
It includes all the patterns (from shortest to longest match).
Expand Down
6 changes: 3 additions & 3 deletions bin/subsets.egi → bin/sublist.egi
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
(match-lambda (list string)
{
[(args-check-opts $opts)
(execution print-each-line filter-subsets opts [] stdin)]
(execution print-each-line filter-sublist opts [] stdin)]
[_ usage]
}))

(define $usage
(do {
(print "Usage: subsets [OPTIONS]")
(print "Usage: sublist [OPTIONS]")
option-usage
(print "tldr:")
(print " $ echo A B C D | subsets")
(print " $ echo A B C D | sublist")
(print " A")
(print " A B")
(print " B")
Expand Down
35 changes: 35 additions & 0 deletions bin/subset.egi
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env egison
(load-file "../lib/egzact/utils.egi")
(load-file "../lib/egzact/filters.egi")

(define $main
(match-lambda (list string)
{
[(args-check-opts $opts)
(execution print-each-line filter-subset opts [] stdin)]
[_ usage]
}))

(define $usage
(do {
(print "Usage: subset [OPTIONS]")
option-usage
(print "tldr:")
(print " $ echo A B C D | subset")
(print " A")
(print " B")
(print " C")
(print " D")
(print " A B")
(print " A C")
(print " B C")
(print " A D")
(print " B D")
(print " C D")
(print " A B C")
(print " A B D")
(print " A C D")
(print " B C D")
(print " A B C D")
}
))
186 changes: 186 additions & 0 deletions doc/examples.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
# egzact examples

## Enumerate all the possible parent domains from the sub domain.

```bash
$ echo hoge.huga.pre.cure.example.com | stairr fs=.
com
example.com
cure.example.com
pre.cure.example.com
huga.pre.cure.example.com
hoge.huga.pre.cure.example.com
```

## Enumerate all the possible parent directories from the sub directory.

```bash
$ pwd | stairl fs=/
/usr
/usr/local
/usr/local/bin
```

## Enumerate all the possible FQDN

```bash
$ echo aaa.bbb.ccc.example.com/a/b/c/d | stairr fs=. | stairl fs=/
com
com/a
com/a/b
com/a/b/c
com/a/b/c/d
example.com
example.com/a
example.com/a/b
example.com/a/b/c
example.com/a/b/c/d
ccc.example.com
ccc.example.com/a
ccc.example.com/a/b
ccc.example.com/a/b/c
ccc.example.com/a/b/c/d
bbb.ccc.example.com
bbb.ccc.example.com/a
bbb.ccc.example.com/a/b
bbb.ccc.example.com/a/b/c
bbb.ccc.example.com/a/b/c/d
aaa.bbb.ccc.example.com
aaa.bbb.ccc.example.com/a
aaa.bbb.ccc.example.com/a/b
aaa.bbb.ccc.example.com/a/b/c
aaa.bbb.ccc.example.com/a/b/c/d
```

## Enumerate all the `that ... that` parts from [the complicated sequence](https://en.wikipedia.org/wiki/That_that_is_is_that_that_is_not_is_not_is_that_it_it_is) .

``stairl | stairr`` generates all the sublist from the input.

```bash
$ echo "That that is is that that is not is not is that it it is" | stairl | stairr | grep -o "that.*that" | sort | uniq
that is is that
that is is that that
that is is that that is not is not is that
that is not is not is that
that that
that that is not is not is that
```

## Split the file into 17 indivisual files.

```sh
$ seq $(awk 'END{print NR}' mytext) | slit 17 | awk '{print "sed -n "$1","$NF"p mytext > mytext."NR}'
sed -n 1,2p mytext > mytext.1
sed -n 3,4p mytext > mytext.2
sed -n 5,6p mytext > mytext.3
sed -n 7,8p mytext > mytext.4
sed -n 9,10p mytext > mytext.5
sed -n 11,12p mytext > mytext.6
sed -n 13,14p mytext > mytext.7
sed -n 15,15p mytext > mytext.8
sed -n 16,16p mytext > mytext.9
sed -n 17,17p mytext > mytext.10
sed -n 18,18p mytext > mytext.11
sed -n 19,19p mytext > mytext.12
sed -n 20,20p mytext > mytext.13
sed -n 21,21p mytext > mytext.14
sed -n 22,22p mytext > mytext.15
sed -n 23,23p mytext > mytext.16
sed -n 24,24p mytext > mytext.17

# Execute
$ seq $(awk 'END{print NR}' mytext) | slit 17 | awk '{print "sed -n "$1","$NF"p mytext > mytext."NR}' | sh
```

## Generate [Bi-gram](https://en.wikipedia.org/wiki/N-gram) from the poem.

```bash
$ echo "If a man understands a poem, he shall have troubles." | conv 2
If a
a man
man understands
understands a
a poem,
poem, he
he shall
shall have
have troubles.
```

## Create a zip file nested 100 times.

```bash
$ echo file {1..100}.zip | conv 2 | mirror | addl "zip "
zip 1.zip file
zip 2.zip 1.zip
zip 3.zip 2.zip
zip 4.zip 3.zip
zip 5.zip 4.zip
zip 6.zip 5.zip
zip 7.zip 6.zip
zip 8.zip 7.zip
zip 9.zip 8.zip
zip 10.zip 9.zip
...

# Execute
$ echo file {1..100}.zip | conv 2 | mirror | addl "zip " | sh

$ unzip -Z -2 100.zip
99.zip
```

## Calculate the approximation of Napier's Constant

```bash
$ seq 10 | flat | stairl ofs="*" | flat | wrap ofs="+" '1/(*)' | addl "1+" | bc -l
2.71828180114638447967
```

## Calculate the approximation of PI

```bash
$ seq 1 2 50 | nl | awk '$1=$1"^2/"' | addr '+' | mirror | flat | addr ' 1' | nestr '(*)' | wrap ifs="_" '(4/ *)' | bc -l
3.14159265358979323651
```

## Generate too nexted DOM element

```bash
$ echo {1..10} | nestr "<p>*</p>"
<p> 1 <p> 2 <p> 3 <p> 4 <p> 5 <p> 6 <p> 7 <p> 8 <p> 9 <p> 10 </p> </p> </p> </p> </p> </p> </p> </p> </p> </p>
```

## Generate too nexted JSON element

`jq` command is necessary.

```bash
$ echo A B C D E F | wrap '"*":' | addr "\"G\"" | nestr "{*}" | jq .
{
"A": {
"B": {
"C": {
"D": {
"E": {
"F": "G"
}
}
}
}
}
}
```

## Easily create table element.

```
$ COL=3
$ echo A B C D E F G H | wrap '<td>*</td>' | flat $COL | wrap fs=_ '<tr>*</tr>' | addt '<table border=1>' | addb '</table>'
<table border=1>
<tr><td>A</td> <td>B</td> <td>C</td></tr>
<tr><td>D</td> <td>E</td> <td>F</td></tr>
<tr><td>G</td> <td>H</td></tr>
</table>
```
8 changes: 7 additions & 1 deletion lib/egzact/filters.egi
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,18 @@
{(take upper-num-count (repeat1 upper-num))
(take lower-num-count (repeat1 lower-num))}))))

(define $filter-subsets
(define $filter-sublist
(lambda [$empty $line]
(match-all line (list string)
[<join _ <join (& $x !<nil>) _>>
{@x}])))

(define $filter-subset
(lambda [$empty $line]
(match-all line (set string)
[<join (& $x !<nil>) _>
{@x}])))

(define $filter-stairl
(lambda [$empty $line]
(match-all line (list string) [<join (& $x !<nil>) $xs> [x]])))
Expand Down
Loading

0 comments on commit 6d68dfd

Please sign in to comment.