当我的数据与一个独立于其参数的函数相关时,我何时应该优先使用块封装而不是局部封装?
我什么时候应该使用:
(let [hello "Hello "]
(defn do-greet
"Print a greeting."
[name]
(println (str hello name))))
Run Code Online (Sandbox Code Playgroud)
与:
(defn do-greet
"Print a greeting."
[name]
(let [hello "Hello "]
(println (str hello name))))
Run Code Online (Sandbox Code Playgroud) 请参阅:https://github.com/clojure/clojurescript/wiki/Differences-from-Clojure#namespaces
习惯性的clojure是困扰我的:
(ns clojure-example
(:require [clojure.set :refer [union]]))
Run Code Online (Sandbox Code Playgroud)
但惯用的clojurescript是:
(ns clojurescript-example
(:use [clojure.set :only [union]]))
Run Code Online (Sandbox Code Playgroud)
当然,clojurescript代码将在工作的Clojure为好,但带来了恶魔谁说我应该使用require带:refer,而不是!
这是什么原因?
在Linux上使用sysfs GPIO时,会指示您poll参与POLLPRI和POLLERR事件.
这很容易:
poll = select.poll()
poll.register(filename, select.POLLPRI | select.POLLERR)
result = poll.poll(timeout=timeout)
Run Code Online (Sandbox Code Playgroud)
但是,我想为此代码编写测试,并为依赖它的应用程序进行模拟测试.所以,我需要能够引发一个POLLPRI事件.
我尝试过使用Unix域套接字,但是在域套接字连接后,我无法打开文件进行读取(errno 6没有这样的设备).我也尝试使用套接字SOCK_DGRAM,但如果尚未创建文件,则无法找到该文件,或者拒绝连接.
我想要一种方法来打开常规文件或创建一个可以像常规文件一样打开的文件,并能够发送一个被视为"紧急数据"的消息流.即MSG_OOB.
我能做什么?
要将命令的stdout(在本例中为echo hi)写入文件,您可以执行以下操作:
echo hi > outfile
Run Code Online (Sandbox Code Playgroud)
我想要一个命令而不是重定向或管道,这样我就不需要调用shell.这最终是用于调用python的Ansible subprocess.POpen.
我在寻找:
stdout-to-file outfile echo hi
Run Code Online (Sandbox Code Playgroud)
tee 使stdout很容易复制到文件,但它接受stdin,而不是单独的命令.
是否有一个通用的,可移植的命令来执行此操作?当然,编写一个很容易,但这不是问题.最后,在Ansible,我想做:
command: to-file /opt/binary_data base64 -d {{ base64_secret }}
Run Code Online (Sandbox Code Playgroud)
代替:
shell: base64 -d {{ base64_secret }} > /opt/binary_data
Run Code Online (Sandbox Code Playgroud)
编辑:寻找可用于RHEL 7,Fedora 21的命令
我安装了emacs,创建了一个.emacs.d目录并创建了一个init.el文件:
(require 'package)
(add-to-list 'package-archives
'("melpa" . "http://melpa.milkbox.net/packages/") t)
(package-initialize)
(when (not package-archive-contents)
(package-refresh-contents))
(defvar my-packages
'(starter-kit
starter-kit-bindings
starter-kit-lisp
clojure-mode
color-theme
nrepl))
(dolist (p my-packages)
(when (not (package-installed-p p))
(package-install p)))
(require 'color-theme)
(color-theme-initialize)
(color-theme-charcoal-black)
(color-theme-install-frame-params '((background-color . "black")))
Run Code Online (Sandbox Code Playgroud)
当我打开emacs时,我最终得到的color-theme-charcoal-black颜色是默认的灰色背景.如果我打开init.el eval-buffer,背景会根据需要变黑.
如何在不需要的情况下获得这种效果eval-buffer?
我也尝试过:
(add-hook 'after-init-hook
'(lambda () (color-theme-install-frame-params
'((background-color . "black"))))
Run Code Online (Sandbox Code Playgroud)
与此问题类似:https: //superuser.com/questions/481793/permanently-override-background-colour-of-emacs-theme
我试着按照文档进行操作clojure.instant/read-instant-timestamp,内容如下:
clojure.instant/read-instant-timestamp To read an instant as a java.sql.Timestamp, bind *data-readers* to a map with this var as the value for the 'inst key. Timestamp preserves fractional seconds with nanosecond precision. The timezone offset will be used to convert into UTC.`
以下结果出乎意料:
(do
(require '[clojure.edn :as edn])
(require '[clojure.instant :refer [read-instant-timestamp]])
(let [instant "#inst \"1970-01-01T00:00:09.999-00:00\""
reader-map {'inst #'read-instant-timestamp}]
;; This binding is not appearing to do anything.
(binding [*data-readers* reader-map]
;; prints java.util.Date -- unexpected
(->> instant edn/read-string …Run Code Online (Sandbox Code Playgroud) 我的文件编辑器创建带有前缀的临时文件..
我在跑步:
watchmedo shell-command -p '*.py' -R -c 'echo "${watch_src_path}"'
Run Code Online (Sandbox Code Playgroud)
当我正在编辑时,我看到临时文件的事件,然后文件保存两个事件(可能是因为它执行了删除和写入).
我想看一个事件 - 只有当我保存文件时.
有没有办法让我只使用CLI执行此操作?我对创建python脚本并直接使用watchdog API不感兴趣.
什么是告诉某人"我想要应用于func每个元素iterable的副作用" 的首选方式.
# Option 1... clear, but two lines.
for element in iterable:
func(element)
# Option 2... even more lines, but could be clearer.
def walk_for_side_effects(iterable):
for element in iterable:
pass
walk_for_side_effects(map(func, iterable)) # Assuming Python3's map.
# Option 3... builds up a list, but this how I see everyone doing it.
[func(element) for element in iterable]
Run Code Online (Sandbox Code Playgroud)
我喜欢选项2; 标准库中是否有一个已经等效的函数?