Comments (2)
Building further off what was shown previously, in the absence of the proposed purpose-built search_replace
function written in Go as a core part of the Zed language, it could be written as a user-defined function such as this search-replace-func.zed
.
func search_replace (s, change_map, default): (
coalesce((over change_map with s | switch (case regexp(key, s) != null => yield value | head 1)), default)
)
I've also broken out the map of pairs for search/replace in this changes.zed
.
const changes = |{
"foo|bar": "The Foobar category",
"a.*\\-z.*": "The A-to-Z category"
}|
Putting it all together at the command line:
$ zq -I search-replace-func.zed -I changes.zed 'category := search_replace(desc, changes, "The default category")' descriptions.zson
{desc:"It's foo time",category:"The Foobar category"}
{desc:"a is the first letter-z is last",category:"The A-to-Z category"}
{desc:"Something else",category:"The default category"}
This shows simple modularity at the CLI. We've also discussed the idea of more formally introducing "Zed modules" (#2599) and related concepts like having a standard library of functionality written as Zed to augment the rest of the core functionality implemented in Go, community users being able to share their own modules/libraries easily such as via GitHub, and so forth. This opens the door to being able to deliver enhancements in the short term as Zed that might be hacky at times but can be used like a black box. If perf problems or functional gaps in such an implementation limit usability, usage in its initial form would help validate the use cases for more ambitious core functionality later written in Go.
from zed.
Also related: The original user later expressed an interest in being able to pull the search/replace pairings from a pool rather than defining them in a const
. I was able to hack something together that does this using existing building blocks, but it's super ugly. A better approach would be if we had what's described in #3201.
Separately, @mattnibs has looked over everything shown here and had the impression that a "cross-product join" would be the way he'd approach this problem.
from zed.
Related Issues (20)
- Can't successfully pass "this" as part of an array parameter to user-defined op HOT 2
- Something like jq's "debug" HOT 2
- Accessing the index of current array element within an "over" scope HOT 1
- "Strict mode" for Zed language
- Spread values from an array to become the multiple args to coalesce()
- Show filename when searching across multiple files with zq
- "this" as optional second argument in functions
- Dynamic field names in record expressions HOT 1
- Incorrect comparison result with union value HOT 1
- Most functions don't support unions
- Warn/error on "over ... with this"
- [SUGESTION] Syntax highlighting for HTML template literals HOT 1
- Nanosecond precision output with strftime
- Presenting times in non-UTC timezones
- Hung debug with larger input and head HOT 1
- Post-merge operator prevents debug on flowgraph branches
- "sort" causes Zed lake service crash
- Don't colorize redirected debug output
- nest_dotted function panics on non-record value
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from zed.