diff --git a/doc/guide.md b/doc/guide.md index 51fa0beb..81443dcc 100644 --- a/doc/guide.md +++ b/doc/guide.md @@ -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 ``` diff --git a/doc/guide.pdf b/doc/guide.pdf index 4f4b9616..b375d734 100644 Binary files a/doc/guide.pdf and b/doc/guide.pdf differ diff --git a/docs/index.html b/docs/index.html index 3baf75b1..32d37e5d 100644 --- a/docs/index.html +++ b/docs/index.html @@ -362,6 +362,24 @@

File Includes

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 perhaps:

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

Parting Shots

or := [(||)|#f x]
 
diff --git a/examples/trimwhitespace.jac b/examples/trimwhitespace.jac
new file mode 100644
index 00000000..3077c62e
--- /dev/null
+++ b/examples/trimwhitespace.jac
@@ -0,0 +1,3 @@
+@include'lib/sed.jac'
+
+(replace1 /\s+$/ '')"$0
diff --git a/lib/sed.jac b/lib/sed.jac
new file mode 100644
index 00000000..2b7cff30
--- /dev/null
+++ b/lib/sed.jac
@@ -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;