还有其他方法可以在 J 中模拟“tr”吗?
几周前我拿起了 J,大约在同一时间 CodeGolf.SE 测试版向公众开放。
在使用 J 时,(我的)经常遇到的问题是重新格式化输入和输出以适应问题规范。所以我倾向于使用这样的代码:
( ] ` ('_'"0) ) @. (= & '-')
由于各种原因,这个代码未经测试(如果错误请编辑我);预期含义是“将 -
转换为 _
”。还经常出现:将换行符转换为空格(并相反)、将数字与 j
合并、更改括号。
这占用了相当多的字符,并且不太方便集成到程序的其余部分。
还有其他方法可以继续吗?最好更短,但如果它有其他优点,我很乐意学习其他东西。此外,具有隐含函数相反的解决方案也会减轻很多负担。
I picked up J a few weeks ago, about the same time the CodeGolf.SE beta opened to the public.
A recurrent issue (of mine) when using J over there is reformatting input and output to fit the problem specifications. So I tend to use code like this:
( ] ` ('_'"0) ) @. (= & '-')
This one untested for various reasons (edit me if wrong); intended meaning is "convert -
to _
". Also come up frequently: convert newlines to spaces (and converse), merge numbers with j
, change brackets.
This takes up quite a few characters, and is not that convenient to integrate to the rest of the program.
Is there any other way to proceed with this? Preferably shorter, but I'm happy to learn anything else if it's got other advantages. Also, a solution with an implied functional obverse would relieve a lot.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用库方法有时违背了代码高尔夫的本质,但在字符串库中, charsub 方法非常有用:
It sometimes goes against the nature of code golf to use library methods, but in the string library, the charsub method is pretty useful:
rplc
通常是简单替换的缩写:Amend
m}
在特殊情况下非常简短:但当列表必须是动词时会变得混乱:
其中 g 必须是某物就像
g =: ('b' E. ]) # ('b' E. ]) * [: i. #
。还有很多其他的“技巧”可以根据具体情况而定。手册中的示例:
rplc
is generally short for simple replacements:Amend
m}
is very short for special cases:but becomes messy when the list has to be a verb:
where g has to be something like
g =: ('b' E. ]) # ('b' E. ]) * [: i. #
.There are a lot of other "tricks" that work on a case by case basis. Example from the manual:
我只处理了换行符和 CSV,而不是一般的替换情况,但以下是我处理它们的方式。我假设 Unix 行结尾(或用 toJ 固定的行结尾)带有最后换行符。
".{:('1 2 3',LF)
(还没有使用这个)(".;._2) (' 1 2 3',LF,'4 5 6',LF)
或(,;._2)
(<; ;._2)
(还没有使用过这个。)".;._1}:',',('1,2,3',LF)< /code>
这根本不会取代
tr
,但确实有助于处理行结尾和其他垃圾。I've only dealt with the newlines and CSV, rather than the general case of replacement, but here's how I've handled those. I assume Unix line endings (or line endings fixed with toJ) with a final line feed.
".{:('1 2 3',LF)
(Haven't gotten to use this yet)(".;._2) ('1 2 3',LF,'4 5 6',LF)
or(,;._2)
(<;._2)
(Haven't used this yet either.)".;._1}:',',('1,2,3',LF)
This doesn't replace
tr
at all, but does help with line endings and other garbage.您可能需要考虑使用8!:2 外国:
You might want to consider using the 8!:2 foreign: