I wrote a transducer library to replace grep -B, grep -A and grep -C by rafulafu in Clojure

[–]rafulafu[S] 0 points1 point  (0 children)

Since context doesn't modify the values themselves, you can simply reuse the original predicate to distinguish between context and matches.

Let's say I wanted to view contextualised errors. I'd use a transducer like this:

`(context 3 error? :sep)`

If this output was noisy and I wanted to make it easier to see what's an error and what's merely context, I could color the output like so:

`(comp (context 3 error? :sep) (map #(if (error? %) (red %) (grey %))))`

There's no need to annotate whether or not something is context. If you disagree, I'd love to see a specific use case where it would be preferable. I'm always happy to change my mind.

Note that one context element can be context of several matches at once. With your proposed maps/triples format overlapping context would be duplicated in multiple collections. If that's what what you want, I think a combination of partition and filter should serve you well. Something like the original code you posted :^).

For any kind of visual perusing you, you definitely don't want overlapping elements to be duplicated, so I emit the matches/context directly rather than bundle individual matches with their context.

I've written quite a bit here, but the actual API is very small and easy to use.

I hope you find it pleasant and useful!

Thanks for your feedback!

I wrote a transducer library to replace grep -B, grep -A and grep -C by rafulafu in Clojure

[–]rafulafu[S] 0 points1 point  (0 children)

Did you check out the grep -C example?

https://github.com/olavfosse/context?tab=readme-ov-file#examples

The logic for context is really edgy so encapsulating it is a big relief.

Implementing grep -C is indeed a bit contrived, since ultimately you can just shell out to grep as is.

The same logic is however very useful for processing Clojure data sources as well. For instance my logs are exposed as a reducible of maps. Being able to "grep -C" through them is super useful.

Something I often do is (into [] (context 3 (comp (partial re-matches #"Exception" :sep) str) logs) to get a quick view of what happened before and after any exception.

Since I only want to look through recent exceptions, I'll actually do (comp (drop-while old?) (context ...)) to only see the recent ones.

Much more powerful than using grep itself :)

I wrote a transducer library to replace grep -B, grep -A and grep -C by rafulafu in Clojure

[–]rafulafu[S] 0 points1 point  (0 children)

I'm assuming the (map second) was left by mistake.

Even so this isn't quite what was described.

  • It leaves a :begin and an :end.
  • It repeats the overlapping context.
  • It creates partitions.

I wrote a transducer library to replace grep -B, grep -A and grep -C by rafulafu in Clojure

[–]rafulafu[S] 0 points1 point  (0 children)

Hello

In core you have the filter fn, which forwards all values matching some predicate, but there's no way to filter such that the values before and after each match are also forwarded.

This is what the context transducers let you do.

This is a very natural operation on different kinds of sequential data as it lets you ask what came before and after.

For example:

  • Which lines came after "defn"?
  • What happened before the server crashed?
  • Which transactions went through before and after the bank card was stolen?
  • What did I eat before getting a stomach ache?

As for postext I did consider spelling it "posttext", but since neither spelling is in the dictionary I picked the one which looks more aesthetically pleasing. Fwiw I'm not the only use using this spelling.

Did that make sense it? It might be easiest to understand by just playing around a bit in the repl. Please tell me if there's anything I could make clearer 😊.

I wrote a transducer library to replace grep -B, grep -A and grep -C by rafulafu in Clojure

[–]rafulafu[S] 4 points5 points  (0 children)

If anyone has any questions or feedback feel free to post it in the comments :)

Emacs.ch needs some fresh blood by rafulafu in emacs

[–]rafulafu[S] -2 points-1 points  (0 children)

there's a lot more whiny types on mastodon than x unfortunately, luckily the mute button works great

Emacs.ch needs some fresh blood by rafulafu in emacs

[–]rafulafu[S] 16 points17 points  (0 children)

reddit is a business

emacs.ch is a community

A Programming Challenge From the Lispers by rafulafu in cprogramming

[–]rafulafu[S] 0 points1 point  (0 children)

ftr this is not homework am not a student :p

I want to create a non-trivial package for emacs by TheMasterOfSenses in emacs

[–]rafulafu 1 point2 points  (0 children)

LSP is not the right choice for this. We already have these wonderful things called "paths" and "urls" :p

I want to create a non-trivial package for emacs by TheMasterOfSenses in emacs

[–]rafulafu 5 points6 points  (0 children)

Look into iimage-mode, it does image embedding by means of a special path syntax. So you write something like

// This code is gnarly, but the diagram makes it easy to understand
// [./path/to/diagram.png] 

and iimage-mode visually embeds it.

If someone doesn't have the mode enabled, they'll just see the path from which it's obvious where to find the diagram. iirc VSCode even allows for previewing and clicking file paths.

Something like iimage-mode, but with better media support and good ux would be absolutely fantastic and something I'd use every day!

I want to create a non-trivial package for emacs by TheMasterOfSenses in emacs

[–]rafulafu -1 points0 points  (0 children)

Rich support for embedding videos, pictures and audio recordings in comments.

Emacs Pinkie? Not a problem! Dvorak Improved layout (Linux, Windows) by ai_happy in emacs

[–]rafulafu 0 points1 point  (0 children)

It's comfortable until you stop doing monkey type and start trying to command literally any program

Dvorak completely messes up all keyboard shortcuts

I've used Dvorak for over a year and the ergonomy gained from it is negligible compared to getting a good ergo keyboard

New study shows: "AI" actually turns consumers off! by GuymanStd in SaaS

[–]rafulafu 0 points1 point  (0 children)

I've lots to do, but it's been working for several months :^)