最近,我正在学习F#.我尝试以不同的方式解决问题.像这样:
(*
[0;1;2;3;4;5;6;7;8] -> [(0,1,2);(3,4,5);(6,7,8)]
*)
//head-recursive
let rec toTriplet_v1 list=
match list with
| a::b::c::t -> (a,b,c)::(toTriplet_v1 t)
| _ -> []
//tail-recursive
let toTriplet_v2 list=
let rec loop lst acc=
match lst with
| a::b::c::t -> loop t ((a,b,c)::acc)
| _ -> acc
loop list []
//tail-recursive(???)
let toTriplet_v3 list=
let rec loop lst accfun=
match lst with
| a::b::c::t -> loop t (fun ls -> accfun ((a,b,c)::ls))
| _ -> accfun []
loop list (fun x -> …Run Code Online (Sandbox Code Playgroud) 直接打印到终端:
$ ls
a.out avg.c avg.h
Run Code Online (Sandbox Code Playgroud)
管道到 cat
$ ls | cat
a.out
avg.c
avg.h
Run Code Online (Sandbox Code Playgroud)
为什么ls根据目的地提供不同的输出?
我有三个UTF-8蜇伤:
hello, world
hello, ??
hello, ?rld
Run Code Online (Sandbox Code Playgroud)
我只想要前10个ascii-char-width,以便括号在一列中:
[hello, wor]
[hello, ? ]
[hello, ?r]
Run Code Online (Sandbox Code Playgroud)
在控制台中:
width('??')==width('worl')
width('? ')==width('wor') #a white space behind '?'
Run Code Online (Sandbox Code Playgroud)
一个中文字符是三个字节,但在控制台中显示时只有2个ascii字符宽度:
>>> bytes("hello, ??", encoding='utf-8')
b'hello, \xe4\xb8\x96\xe7\x95\x8c'
Run Code Online (Sandbox Code Playgroud)
format()当UTF-8字符混入时,python 没有帮助
>>> for s in ['[{0:<{1}.{1}}]'.format(s, 10) for s in ['hello, world', 'hello, ??', 'hello, ?rld']]:
... print(s)
...
[hello, wor]
[hello, ?? ]
[hello, ?rl]
Run Code Online (Sandbox Code Playgroud)
它不漂亮:
-----------Songs-----------
| 1: ?? |
| 2: ??? |
| 3: ?????? |
| 4: ????? |
| 5: ???(CUCURRUCUCU …Run Code Online (Sandbox Code Playgroud) 有时,我需要一些functor-helper来操作列表.我尽量将范围保持在本地.
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
struct Square
{
int operator()(int x)
{
return x*x;
}
};
int a[5] = {0, 1, 2, 3, 4};
int b[5];
transform(a, a+5, b, Square());
for(int i=0; i<5; i++)
cout<<a[i]<<" "<<b[i]<<endl;
}
Run Code Online (Sandbox Code Playgroud)
hello.cpp: In function ‘int main()’:
hello.cpp:18:34: error: no matching function for call to ‘transform(int [5], int*, int [5], main()::Square)’
Run Code Online (Sandbox Code Playgroud)
如果我搬出Square去main(),没关系.
我可以使用unicode代码点搜索CJKchar(例如?):
/\%u5c0f
/[\u5c0f]
Run Code Online (Sandbox Code Playgroud)
我不能通过使用来搜索所有CJK字符[\u4E00-\u9FFF],因为vim手册说:
?help /[]
NOTE: The other backslash codes mentioned above do not work inside []!
这是一种做这项工作的方法吗?
这是生成Markdown大纲的vim脚本:
fun! TOC()
call setloclist(0, [])
let save_cursor = getpos(".")
call cursor(1, 1)
while search("^#", 'W') > 0
let msg = printf('%s:%d:%s', expand('%'), line('.'), substitute(getline('.'), '#', '»', 'g'))
laddexpr msg
endwhile
call setpos('.', save_cursor)
endfun
com! -bar TOC call TOC()
Run Code Online (Sandbox Code Playgroud)
示例降价文件:https://github.com/progit/progit/raw/master/zh/01-introduction/01-chapter1.markdown
运行:TOC命令后,这是快速列表:
01-chapter1.markdown|5| »» ?????? »»
01-chapter1.markdown|11| »»» ???????? »»»
01-chapter1.markdown|22| »»» ?????????? »»»
01-chapter1.markdown|33| »»» ????????? »»»
01-chapter1.markdown|42| »» Git ?? »»
01-chapter1.markdown|56| »» Git ?? »»
01-chapter1.markdown|60| »»» ????????????? »»»
01-chapter1.markdown|74| »»» ???????????? …Run Code Online (Sandbox Code Playgroud) $VIMRUNTIME/ftplugin/(例如python.vim和ada.vim)中的某些脚本没有定义b:undo_ftplugin.该cpo选项的默认值是aABceFs.
当我set ft=python,然后set ft=css.$VIMRUNTIME/ftplugin/css.vim立即完成.而且omnifunc=pythoncomplete#Complete一直都是.
每个人都ftplugin/name.vim需要定义b:undo_ftplugin吗?
这是 /usr/share/vim/vim73/ftplugin.vim:
" Vim support file to switch on loading plugins for file types
"
" Maintainer: Bram Moolenaar <Bram@vim.org>
" Last change: 2006 Apr 30
if exists("did_load_ftplugin")
finish
endif
let did_load_ftplugin = 1
augroup filetypeplugin
au FileType * call s:LoadFTPlugin()
func! s:LoadFTPlugin()
if exists("b:undo_ftplugin")
exe b:undo_ftplugin
unlet! b:undo_ftplugin b:did_ftplugin
endif …Run Code Online (Sandbox Code Playgroud) x = 0
class Foo:
print(x)
x = 1
print(x)
print(x)
Run Code Online (Sandbox Code Playgroud)
0
1
0
Run Code Online (Sandbox Code Playgroud)
x = 0
def foo():
print(x)
x = 1
print(x)
foo()
Run Code Online (Sandbox Code Playgroud)
UnboundLocalError: local variable 'x' referenced before assignment.
Run Code Online (Sandbox Code Playgroud)
为什么x可以引用两个名称空间中的对象class block?
我不明白为什么Code 1不扔一个UnboundLocalError.
功能和课堂之间的不一致让我烦恼.
在阅读了几次Python文档之后,我仍然无法理解范围规则.
以下是块:模块,函数体和类定义....[跳跃]...
如果名称绑定在块中,则它是该块的局部变量,除非声明为非本地.如果名称在模块级别绑定,则它是全局变量.(模块代码块的变量是局部的和全局的.)如果在代码块中使用了变量但在那里没有定义,则它是一个自由变量.
如果名称绑定操作发生在代码块中的任何位置,则块中名称的所有使用都将被视为对当前块的引用.在绑定之前在块中使用名称时,这可能会导致错误.这条规则很微妙.Python缺少声明,并允许在代码块中的任何位置进行名称绑定操作.可以通过扫描块的整个文本以确定名称绑定操作来确定代码块的局部变量.
此示例使用整数,运算符和另一个整数读取行.例如,
// sstream-line-input.cpp - Example of input string stream.
// This accepts only lines with an int, a char, and an int.
// Fred Swartz 11 Aug 2003
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
//================================================================ main
int main() {
string s; // Where to store each line.
int a, b; // Somewhere to put the ints.
char op; // Where to save the char (an operator)
istringstream instream; // Declare an input string stream
while …Run Code Online (Sandbox Code Playgroud) 我想在bash脚本中将二进制数据(png,jpg,gif等)插入到sqlite3数据库中.
我使用独立的二进制文件sqlite3.我该如何编写SQL语句?
谢谢你的帮助.