忽略这是一个不好的add功能.这是一个关于在TypeScript中使用带有扩展语法的数组解构的问题.
这就是我正在尝试的
const add = ([x,...xs]) => {
if (x === undefined)
return 0
else
return x + add(xs)
}
console.log(add([1,2,3])) //=> 6
Run Code Online (Sandbox Code Playgroud)
但我不知道如何添加TypeScript类型.我最好的猜测是做这样的事情(最直接的翻译)
const add = (xs: number[]): number => {
if (xs[0] === undefined)
return 0;
else
return xs[0] + add(xs.slice(1));
};
console.log(add([1,2,3])); // => 6
Run Code Online (Sandbox Code Playgroud)
这两种职能的工作,但在打字稿我失去解构阵列参数,函数体被junked了像一堆难看的东西的能力,并xs[0]与xs.slice(1)-即使我这些抽象到自己的职能,这是除了点.
是否可以在TypeScript中添加类型来解构扩展参数?
到目前为止我尝试过的
这样的东西适用于固定数组
// compiles
const add = ([x,y,z]: [number, number, number]): number => ...
Run Code Online (Sandbox Code Playgroud)
但当然我需要可变长度数组输入.我试过这个,但它没有编译
// does not compile
const add = ([x, ...xs]: …Run Code Online (Sandbox Code Playgroud) 假设我有一个基于any的DOM元素数组 selector
var elems = document.querySelectorAll(selector);
Run Code Online (Sandbox Code Playgroud)
我不知道包含了什么elems,但让我们假设elems.length > 0
我想使用querySelectorAll(或一些等效函数)elems来查找与其他选择器匹配的所有元素.
// example: find all buttons with class `foo` within `elems` array
var buttons = elems.querySelectorAll("button.foo");
Run Code Online (Sandbox Code Playgroud)
这不起作用(显而易见的原因),但我不确定如何做到这一点:(
这是我创建的包装器,用于处理@Tibos接受的答案
// Element.matches wrapper
(function(e){
if (typeof e.matches !== "function") {
e.matches = e.webkitMatchesSelector ||
e.mozMatchesSelector ||
e.msMatchesSelector ||
e.oMatchesSelector ||
e.matchesSelector;
}
})(Element.prototype);
Run Code Online (Sandbox Code Playgroud) 我知道在perl中没有排序哈希.我关心的是我是否可以依赖索引关系出现的键和值.
说我有这个哈希
my %h = ("a" => 1, "b" => 2, "c" => 3, "d" => 4);
Run Code Online (Sandbox Code Playgroud)
如果我这样做keys %h,我可能会得到
("b", "a", "d", "c")
Run Code Online (Sandbox Code Playgroud)
我能保证values %h以相同的顺序出现以匹配密钥吗?我可以期待吗?
(2, 1, 4, 3)
Run Code Online (Sandbox Code Playgroud)
或者不能保证keys %h和之间有任何指数关系values %h?
A.TXT
1
2
3
4
5
6
Run Code Online (Sandbox Code Playgroud)
b.txt
10
2
3
40
50
6
70
Run Code Online (Sandbox Code Playgroud)
我想对这些生成以下输出的文件运行一些命令.
10
40
50
70
Run Code Online (Sandbox Code Playgroud)
如何在两个文件上运行差异,但只显示更改的行.我不希望输出周围有任何其他元数据.
我也不希望看到更改行的任何上下文.
如何获得生成器的第n个值?
function *index() {
let x = 0;
while(true)
yield x++;
}
// the 1st value
let a = index();
console.log(a.next().value); // 0
// the 3rd value
let b = index();
b.next();
b.next();
console.log(b.next().value); // 2
// the nth value?
let c = index();
let n = 10;
console.log(...); // 9
Run Code Online (Sandbox Code Playgroud) 可以说我有以下几点:
nested_object = [0, 1, 2, {foo: 'bar'}]
Run Code Online (Sandbox Code Playgroud)
搜索更深的嵌套对象时,如何使用dig选择数组的最后一个元素?
以下类型取自此问题
(* contains an error, later fixed by the OP *)
type _ task =
| Success : 'a -> 'a task
| Fail : 'a -> 'a task
| Binding : (('a task -> unit) -> unit) -> 'a task
| AndThen : ('a -> 'b task) * 'a task -> 'b task
| OnError : ('a -> 'b task) * 'a task -> 'b task
type _ stack =
| NoStack : 'a stack
| AndThenStack : …Run Code Online (Sandbox Code Playgroud) 下面我有一个button试图加载远程内容...
import Post exposing (Post)
import Html exposing (..)
import Html.Events exposing (..)
import Http
import Json.Decode as Decode
type alias Model =
{ posts : List Post }
type Msg
= Search String
| PostsReceived (Result Http.Error (List Post))
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
Search s ->
let
cmd =
(Decode.list Post.decode)
|> Http.get ("/posts?author=" ++ s)
|> Http.send PostsReceived
in
( model, cmd …Run Code Online (Sandbox Code Playgroud) 我有一个我想要运行的脚本
$ myscript < mydata.dat
Run Code Online (Sandbox Code Playgroud)
在里面myscript我需要将STDIN分叉/管道到多个目的地
#!/usr/bin/env bash
php script1.php > out1
php script2.php > out2
php script3.php > out3
Run Code Online (Sandbox Code Playgroud)
每个人都需要一份STDIN.可能吗?
就像是
# obviously this is broken ...
STDIN | php script1.php > out1
STDIN | php script2.php > out2
STDIN | php script3.php > out3
Run Code Online (Sandbox Code Playgroud) 我想做一些函数组合。我已经知道这一点:
如果f3(x)应该和f1(f2(x))
那时一样f3 = _.flowRight(f1,f2);
如果f3(x,y)和f1(x, f2(y))
那时一样……?
(用例是 node.js/express 中间件函数的组合。)
javascript ×3
hash ×2
arrays ×1
bash ×1
composition ×1
diff ×1
dom ×1
ecmascript-6 ×1
elm ×1
gadt ×1
generator ×1
lodash ×1
ocaml ×1
perl ×1
pipe ×1
polymorphism ×1
ruby ×1
shell ×1
stdin ×1
typescript ×1
variant ×1