如何在J中过滤列表? [英] How to filter a list in J?

查看:81
本文介绍了如何在J中过滤列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在学习迷人的J编程语言,但是我无法弄清的一件事是如何过滤列表.

I'm currently learning the fascinating J programming language, but one thing I have not been able to figure out is how to filter a list.

假设我有任意列表3 2 2 7 7 2 9,并且我想删除2s,但其他所有内容保持不变,即我的结果将是3 7 7 9.我到底该怎么做?

Suppose I have the arbitrary list 3 2 2 7 7 2 9 and I want to remove the 2s but leave everything else unchanged, i.e., my result would be 3 7 7 9. How on earth do I do this?

推荐答案

简短答案

   2 (~: # ]) 3 2 2 7 7 2 9
3 7 7 9


长答案

我为您提供了答案,但是在您应熟悉一些详细信息之前.我们走了.

I have the answer for you, but before you should get familiar with some details. Here we go.

J中的动词有两种类型: monads dyads .前者仅接受一个参数,后者仅接受两个参数.

There are two types of verbs in J: monads and dyads. The former accept only one parameter, the latter accept two parameters.

例如,将唯一的参数传递给称为 tally monadic 动词#,计算列表中元素的数量:

For example passing a sole argument to a monadic verb #, called tally, counts the number of elements in the list:

   # 3 2 2 7 7 2 9
7

接受两个参数(左右)的动词#被称为 copy ,它是 dyadic ,用于复制右边列表中的元素左列表中各个元素指定的次数(列表中也可能是唯一元素):

A verb #, which accepts two arguments (left and right), is called copy, it is dyadic and is used to copy elements from the right list as many times as specified by the respective elements in the left list (there may be a sole element in the list also):

   0 0 0 3 0 0 0 # 3 2 2 7 7 2 9
7 7 7

叉子

在J中有一个 fork 的概念,它是三个或三个动词,分别以二元或二元方式应用于其自变量.

Fork

There's a notion of fork in J, which is a series of 3 verbs applied to their arguments, dyadically or monadically.

这是我在第一段代码中使用的 fork 的示意图:

Here's the diagram of a kind of fork I used in the first snippet:

 x (F G H) y

      G
    /   \
   F     H
  / \   / \
 x   y x   y

它描述了动词应用于其自变量的顺序.因此发生了这些应用程序:

It describes the order in which verbs are applied to their arguments. Thus these applications occur:

   2 ~: 3 2 2 7 7 2 9
1 0 0 1 1 0 1

在此示例中,~:(不等于)为 dyadic ,并生成一个布尔值列表,当参数不等于2时,这些布尔值为true.根据图,这是F应用程序.

The ~: (not equal) is dyadic in this example and results in a list of boolean values which are true when an argument doesn't equal 2. This was the F application according to diagram.

下一个应用程序是H:

   2 ] 3 2 2 7 7 2 9
3 2 2 7 7 2 9

]( identity )可以是 monad dyad ,但是它总是返回传递给动词的正确参数(有一个相反的动词[返回.是的,左边的自变量!:)

] (identity) can be a monad or a dyad, but it always returns the right argument passed to a verb (there's an opposite verb, [ which returns.. Yes, the left argument! :)

到目前为止,太好了.应用程序后,FH相应地返回了这些值:

So far, so good. F and H after application returned these values accordingly:

1 0 0 1 1 0 1
3 2 2 7 7 2 9

要执行的唯一步骤是G动词应用程序.

The only step to perform is the G verb application.

正如我之前提到的,动词# dyadic (接受两个参数),它使我们能够从正确的参数中重复项的次数与在相应位置中指定的次数相同.左参数.因此:

As I noted earlier, the verb #, which is dyadic (accepts two arguments), allows us to duplicate the items from the right argument as many times as specified in the respective positions in the left argument. Hence:

   1 0 0 1 1 0 1 # 3 2 2 7 7 2 9
3 7 7 9

我们刚刚从2中过滤出了列表.

We've just got the list filtered out of 2s.

这两个文档中描述了叉子钩子和其他primitves(略有不同)的种类:

Slightly different kind of fork, hook and other primitves (including abovementioned ones) are described in these two documents:

  • A Brief J Reference (175 KiB)
  • Easy-J. An Introduction to the World's most Remarkable Programming Language (302 KiB)

其他有用的信息来源是 Jsoftware网站,其中包含

Other useful sources of information are the Jsoftware site with their wiki and a few mail list archives in internets.

这篇关于如何在J中过滤列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆