我有一些bash函数输出一些信息:
我一直在编写读取输出和过滤它的函数:
function filter-epson {
find-modelname-in-epson-ppds | sed <bla-blah-blah>
}
function filter-hp {
find-modelname-in-hp-ppds | sed <the same bla-blah-blah>
}
etc ...
Run Code Online (Sandbox Code Playgroud)
但我认为最好做这样的事情:
function filter-general {
(somehow get input) | sed <bla-blah-blah>
}
Run Code Online (Sandbox Code Playgroud)
然后调用另一个高级函数:
function high-level-func {
# outputs filtered information
find-modelname-in-hp/epson/...-ppds | filter-general
}
Run Code Online (Sandbox Code Playgroud)
如何通过最好的bash实践实现这一目标?
我正在阅读有关功能更新的React Hook 文档并查看此引用:
“+”和“-”按钮使用函数形式,因为更新的值是基于之前的值
但是我看不出需要功能更新的目的是什么,它们与直接使用旧状态计算新状态有什么区别。
为什么 React useState Hook 的更新器函数根本需要函数式更新表单? 我们可以清楚地看到差异的示例有哪些(因此使用直接更新会导致错误)?
例如,如果我从文档中更改此示例
function Counter({initialCount}) {
const [count, setCount] = useState(initialCount);
return (
<>
Count: {count}
<button onClick={() => setCount(initialCount)}>Reset</button>
<button onClick={() => setCount(prevCount => prevCount + 1)}>+</button>
<button onClick={() => setCount(prevCount => prevCount - 1)}>-</button>
</>
);
}
Run Code Online (Sandbox Code Playgroud)
count直接更新:
function Counter({initialCount}) {
const [count, setCount] = useState(initialCount);
return (
<>
Count: {count}
<button onClick={() => setCount(initialCount)}>Reset</button>
<button onClick={() => setCount(count + 1)}>+</button>
<button onClick={() => setCount(count - …Run Code Online (Sandbox Code Playgroud) 假设我有下表:
+--------------------+--------------------+------+------------+--------------------+
| host| path|status|content_size| time|
+--------------------+--------------------+------+------------+--------------------+
|js002.cc.utsunomi...|/shuttle/resource...| 404| 0|1995-08-01 00:07:...|
| tia1.eskimo.com |/pub/winvn/releas...| 404| 0|1995-08-01 00:28:...|
|grimnet23.idirect...|/www/software/win...| 404| 0|1995-08-01 00:50:...|
|miriworld.its.uni...|/history/history.htm| 404| 0|1995-08-01 01:04:...|
| ras38.srv.net |/elv/DELTA/uncons...| 404| 0|1995-08-01 01:05:...|
| cs1-06.leh.ptd.net | | 404| 0|1995-08-01 01:17:...|
|dialip-24.athenet...|/history/apollo/a...| 404| 0|1995-08-01 01:33:...|
| h96-158.ccnet.com |/history/apollo/a...| 404| 0|1995-08-01 01:35:...|
| h96-158.ccnet.com |/history/apollo/a...| 404| 0|1995-08-01 01:36:...|
| h96-158.ccnet.com |/history/apollo/a...| 404| 0|1995-08-01 01:36:...|
| h96-158.ccnet.com |/history/apollo/a...| 404| 0|1995-08-01 01:36:...|
| h96-158.ccnet.com |/history/apollo/a...| 404| 0|1995-08-01 01:36:...|
| h96-158.ccnet.com |/history/apollo/a...| 404| 0|1995-08-01 01:36:...|
| h96-158.ccnet.com …Run Code Online (Sandbox Code Playgroud) dataframe apache-spark apache-spark-sql pyspark spark-dataframe
Deque根据Algorithms, Coursera的第1部分,我正在编写关于Java的课程.目前我的基于数组的方法Deque有removeLast():
public Item removeLast() {
if (size() == array.length / 4) {
resize(array.length / 2);
}
if (head != tail) {
Item tmp = array[--head];
array[head] = null;
return tmp;
}
throw new NoSuchElementException("Stack underflow");
}
Run Code Online (Sandbox Code Playgroud)
如果 head == tailmeans Deque是空的,我根据作业规范抛出异常,在方法结束而不是return 语句.这段代码给出了关于不变量(head != tail)的直接意图.
另一方面,方法可能会像这样重写:
public Item removeLastRewritten() {
if (size() == array.length / 4) {
resize(array.length / 2);
}
if (head == tail) {
throw …Run Code Online (Sandbox Code Playgroud) 为什么Linux认为主线程终止为僵尸进程的进程有什么办法可以避免这种情况?
在下面的代码中我:
pthread_exit 主线程pthread_exit 分离的线程在#3之前,将ps(1)我的流程显示为正常流程.但是,在#3之后ps(1),即使它仍然有运行的线程,它也会将我的进程显示为僵尸(例如2491 pts/0 00:00:00 thread-app <defunct>).
是否可以退出主线程但避免进入僵尸状态?
码:
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>
void *thread_function(void *args)
{
printf("The is new thread! Sleep 20 seconds...\n");
sleep(20);
printf("Exit from thread\n");
pthread_exit(0);
}
int main(int argc, char **argv)
{
pthread_t thrd;
pthread_attr_t attr;
int res = 0;
res = pthread_attr_init(&attr);
res = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
res = pthread_create(&thrd, &attr, thread_function, NULL);
res = pthread_attr_destroy(&attr);
printf("Main …Run Code Online (Sandbox Code Playgroud) 可能重复:
bash脚本获取自身完整路径的可靠方式?
我有bash脚本test.sh,它search.sh通过以下行使用来自另一个脚本的函数:
source ../scripts/search.sh
<call some functions from search.sh>
Run Code Online (Sandbox Code Playgroud)
这两个脚本都位于git存储库中.search.sh在<git_root>/scripts/目录中,test.sh位于同一目录中(但是,一般来说,可以位于<git_root>目录内的任何位置- 我的意思是我不能依赖于以下source search.sh方法).
当我test.sh从<git_root>/scripts/一切调用脚本运行良好,但一旦我更改当前工作目录test.sh失败:
cd <git_root>/scripts/
./test.sh //OK
cd ..
./scripts/test.sh //FAILS
./scripts/test.sh: line 1: ../scripts/search.sh: No file or directory ...
Run Code Online (Sandbox Code Playgroud)
因此,我有:
search.sh脚本对<git_root>目录的相对路径我想要的是:能够test.sh从内部任何地方运行<git_root>而不会出错.
PS:无法使用永久绝对路径,search.sh因为git存储库可以克隆到任何位置.
Scala函数具有以下链接方法:
fn1.andThen(fn2)
fn1.compose(fn2)
Run Code Online (Sandbox Code Playgroud)
但是如何写这种情况:
我有cleanUp()必须始终作为最后一步调用的函数。我还有很多其他功能,例如:
class Helper {
private[this] val umsHelper = new UmsHelper()
private[this] val user = umsHelper.createUser()
def cleanUp = ... // delete user/ and other entities
def prepareModel(model: TestModel) = {
// create model on behalf of the user
}
def commitModel() = {
// commit model on behalf of the user
}
}
Run Code Online (Sandbox Code Playgroud)
而且一些外部代码可以使用如下代码:
val help = new Helper()
help.prepareModel()
help.commitModel()
// last step should be called implicitly cleanUp
Run Code Online (Sandbox Code Playgroud)
如何以一种功能性的方式编写代码,即链接将始终cleanUp隐式调用函数作为最后一步?
注意:我将其视为C …
我有两个lambda函数(谓词):
final Predicate<Node> isElement = node -> node.getNodeType() == Node.ELEMENT_NODE;
final BiPredicate<Node, String> hasName = (node, name) -> node.getNodeName().equals(name);
Run Code Online (Sandbox Code Playgroud)
我希望以一种简洁的方式组合,如下所示:
// Pseudocode
isElement.and(hasName("tag")) // type of Predicate
Run Code Online (Sandbox Code Playgroud)
然后传递给另一个lambda函数:
final BiFunction<Node, Predicate<Node>, List<Node>> getChilds = (node, cond) -> {
List<Node> resultList = new ArrayList<>();
NodeList nodeList = node.getChildNodes();
for (int i = 0; i < nodeList.getLength(); ++i) {
Node tmp = nodeList.item(i);
if (cond.test(tmp)) {
resultList.add(tmp);
}
}
return resultList;
};
Run Code Online (Sandbox Code Playgroud)
结果我期待它看起来像下面这样:
List<Node> listNode = getChilds.apply(document, isElement.and(hasName("tag")));
Run Code Online (Sandbox Code Playgroud)
但是and方法Predicate …
在Kotlin文档中,我发现了以下示例:
for ((index, value) in array.withIndex()) {
println("the element at $index is $value")
}
Run Code Online (Sandbox Code Playgroud)
是否有可能(以及如何)使用2D矩阵进行类似的操作:
for ((i, j, value) in matrix2D.withIndex()) {
// but iterate iver double index: i - row, j - column
if (otherMatrix2D[i, j] > value) doSomething()
}
Run Code Online (Sandbox Code Playgroud)
如何在Kotlin类中支持此功能?
我想通过使用pydantic验证器验证包含from字段(Python 中的保留字)的JSON 对象(它在Telegram Bot API 中)。所以我的模型应该如下所示:
class Message(BaseModel):
message_id: int
from: Optional[str]
date: int
chat: Any
...
Run Code Online (Sandbox Code Playgroud)
但是在这种情况下不允许使用from关键字。
我怎么能这样做?
注:这是不同的不是“为什么我们不能使用关键字作为属性”因为在这里我们得到外部JSON我们无法控制,我们无论如何应该处理JSON的领域。
bash ×2
java ×2
shell ×2
apache-spark ×1
c ×1
coding-style ×1
composition ×1
dataframe ×1
implicit ×1
iteration ×1
iterator ×1
java-8 ×1
json ×1
kotlin ×1
lambda ×1
linux ×1
methods ×1
pipe ×1
posix ×1
pthreads ×1
pydantic ×1
pyspark ×1
python ×1
react-hooks ×1
reactjs ×1
scala ×1
telegram-bot ×1