简体   繁体   English

默写 4: 'x&{.&.;: y'

[英]Write 4 : 'x&{.&.;: y' tacitly

Conor Hoekstra recently solved a Leetcode problem in APL https://youtu.be/QtvvQ7MdwKY The problem is to take the first x words from a character string y Conor Hoekstra 最近在 APL https://youtu.be/QtvvQ7MdwKY中解决了一个 Leetcode 问题,问题是从字符串y中取出前x单词

In J, using &.在 J 中,使用&. (Under) and ;: (Words) I can come up with a nice explicit one liner (Under) and ;: (Words) 我可以想出一个很好的明确的单线

solve =. 4 : 'x&{.&.;: y'         NB. Box words in y -> take first x-> unbox words in result while retaining spaces between
   s=. 'Hello how are you Contestant'
   4 solve s
Hello how are you

The trouble I am having is finding the tacit version that still includes &.我遇到的麻烦是找到仍然包含&. , mainly because I believe x needs to be bound to {. ,主要是因为我认为x需要绑定到{. during the creation of the verb.在创建动词的过程中。 This is also an example where the magic 13: conversion is not helpful这也是魔术13:转换没有帮助的例子

   13 : 'x&{.&.;: y'
4 : 'x&{.&.;: y'

I can solve it tacitly by using ;:^:_1 to create the inverse of ;:我可以通过使用;:^:_1创建;:

   solve2=. (;:^:_1)@:({. ;:)
   4 solve2 s
Hello how are you

But that is not nearly as pretty as the tacit version of 4: 'x&{.&.;: y' could be.但这并不像4: 'x&{.&.;: y'的默认版本那样漂亮。
Anyone have a pretty tacit solution for 4: 'x&{.&.;: y' ?有人对4: 'x&{.&.;: y'有一个相当默契的解决方案吗?

With semiduals (introduced with J902 or J903?) you can write 4 {.&.(a:`;:) s .使用对偶(由 J902 或 J903 引入?)您可以编写4 {.&.(a:`;:) s Then ;: gets only applied to the right argument, while still doing the inverse later.然后;:仅适用于正确的参数,而稍后仍会进行相反的操作。 u&.(f`a:) would apply f only on the left argument. u&.(f`a:)f仅应用于左侧参数。

mainly because I believe x needs to be bound to {.主要是因为我认为x需要绑定到{. during the creation of the verb.在创建动词的过程中。

If creating a verb with an input is the key, shouldn't you use an adverb?如果用输入创建动词是关键,你不应该使用副词吗?

   solve =: 1 : 'm&{.&.;:'
   4 solve
4&{.&.;:
   (4 solve ,: 2 solve) 'Hello how are you Contestant'
Hello how are you
Hello how        

solve itself isn't tacit (or a verb), but 4 solve is tacit verb. solve本身不是默认的(或动词),但4 solve是默认的动词。

I don't think you can write this as a tacit verb, but it's easy to do as a tacit adverb: ]:&{.&.;: .我不认为你可以把它写成一个默认动词,但它很容易作为一个默认副词: ]:&{.&.;:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM