从 J 中的 STDIN 读取一行

Reading a line from STDIN in J

TL;DR 如何在不消耗所有标准输入的情况下从 J 中的标准输入读取一行字符?我正在寻找一种可移植的方法来执行此操作。

我一直在尝试制作一个交互式 J 程序来读取用户的一行输入。在我的研究过程中,我遇到了 this page,上面写着(强调):

J's native file operations are file-oriented rather than stream-oriented; that is, they read an entire file at a time, and there is no notion of a 'current file pointer', or a newline character, or a readline verb that returns just one record. Such facilities are easy to write, but it is usually better to work with entire files at a time, just as for ordinary computation J works with whole arrays at a time. Read your file in, split it into records, and work on the list of records.

但是,我对编写这样的实用程序感到茫然。如果 J 有一个 getchar 函数,这个任务就会变得微不足道,但据我所知,读取输入的唯一方法是通过外部调用 1!:1(3) (或 stdin'').这几乎不是理想的解决方案,因为我希望能够与用户的输入进行交互。

例如,假设我想复制此 Python 3 程序的行为:

print("Enter grade: ")
grade = int(input())
if grade > 90:
    print("Good job!")
else:
    print(":/")

print("Enter some text: ")
text = input()
print("Reversed: " + text[::-1])

这在 J 中可以大致翻译为:

getgrade := 3 : 0
  grade =. ". y
  if. grade > 90 do.  echo 'Good job!'
  else.               echo ':/'
  end.
)

echo 'Enter grade: '
getgrade readline ''
echo 'Enter some text: '
echo 'Reversed: ' , |. readline ''

当然假设 readline 的正确定义。

想法

我想也许我可以使用 J 的 shellspawn 命令来调用读取一行的可执行文件。但是,shell 需要从动词本身传递输入,我无法使 spawn 在我的 J 安装上工作,尽管我怀疑它是否会产生正确的行为。

我觉得这个

Such facilities are easy to write

本着 "such facilities are easy to implement in the core language" 的精神。

我找不到令人满意的方式来实现您想要的。以下内容可能会有所帮助。

1。制作一个 qt-app 或 lab

jqt 中有很多表单和提示实用程序。 window driver 可以做很多事情。

2。使用 general/misc/prompt

制作脚本:

load'general/misc/prompt'
main =: 3 : 0
 a =: prompt 'Enter grade: '
 echo 'Grade entered: ',a
)

但是你将不得不使用 REPL

 load'yourscript'
 main ''

3。使用包装脚本。

--- wrap.sh ---
#!/usr/bin/env bash
j <<EOF
+/ $(read -p 'Grades: ' k; echo $k)
EOF
---
./wrap.sh
Grades: 3 5 9
17