Skip to content

Commit

Permalink
trim?
Browse files Browse the repository at this point in the history
add trim-whitespace/sed example

doc
  • Loading branch information
vmchale committed Feb 9, 2022
1 parent ade8902 commit a99ef7b
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 0 deletions.
30 changes: 30 additions & 0 deletions doc/guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,36 @@ path"$0

`intercalate` is defined in `lib/string.jac`.

#### Example

Suppose we want to mimic some functionality of `sed` - we'd like to replace
some regular expression with a string (no capture groups, only first replacement
per line)

```
@include'prelude/fn.jac'
fn replace1(re, str, line) :=
let
val insert := \line. \str. \ixes.
take (ixes->1) line + str + drop (ixes->2) line
in option line (insert line str) (match line re) end;
```

Then we could trim whitespace from a file with

```
@include'lib/sed.jac'
(replace1 /\s+$/ '')"$0
```

Jacinda does not modify files in-place so one would need to use [sponge](https://joeyh.name/code/moreutils/) perhaps:

```
ja run trimwhitespace.jac -i FILE | sponge FILE
```

#### Parting Shots

```
Expand Down
Binary file modified doc/guide.pdf
Binary file not shown.
18 changes: 18 additions & 0 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,24 @@ <h4 id="file-includes">File Includes</h4>
path&quot;$0</code></pre>
<p><code>intercalate</code> is defined in
<code>lib/string.jac</code>.</p>
<h4 id="example">Example</h4>
<p>Suppose we want to mimic some functionality of <code>sed</code> -
we’d like to replace some regular expression with a string (no capture
groups, only first replacement per line)</p>
<pre><code>@include&#39;prelude/fn.jac&#39;

fn replace1(re, str, line) :=
let
val insert := \line. \str. \ixes.
take (ixes-&gt;1) line + str + drop (ixes-&gt;2) line
in option line (insert line str) (match line re) end;</code></pre>
<p>Then we could trim whitespace from a file with</p>
<pre><code>@include&#39;lib/sed.jac&#39;

(replace1 /\s+$/ &#39;&#39;)&quot;$0</code></pre>
<p>Jacinda does not modify files in-place so one would need to use <a
href="https://joeyh.name/code/moreutils/">sponge</a> perhaps:</p>
<pre><code>ja run trimwhitespace.jac -i FILE | sponge FILE</code></pre>
<h4 id="parting-shots">Parting Shots</h4>
<pre><code>or := [(||)|#f x]

Expand Down
3 changes: 3 additions & 0 deletions examples/trimwhitespace.jac
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@include'lib/sed.jac'

(replace1 /\s+$/ '')"$0
8 changes: 8 additions & 0 deletions lib/sed.jac
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@include'prelude/fn.jac'

{. example: (replace /^var/ 'export const')"$0
fn replace1(re, str, line) :=
let
val insert := \line. \str. \ixes.
take (ixes->1) line + str + drop (ixes->2) line
in option line (insert line str) (match line re) end;

0 comments on commit a99ef7b

Please sign in to comment.