预期的Int,得到了IntProgression

时间:2016-08-03 16:12:52

标签: kotlin

我正在尝试上Kotlin所以我正在关注他们自己的this tutorial

所以他们试图创建一个给定字符串的序列,例如:

"a vect" -> [
     a vect  :
     a vec   :   t
     a ve    :   ct
     ...
] 

根据视频,这样做的方法如下:

val seq = sequenceOf(canonicalisedInput.lastIndex + 1 downTo 0).map {
    canonicalisedInput.substring(0, it) to canonicalisedInput.substring(it)
}

我得到了我所做的(好吧,它的想法)。问题是substring需要两个Int,而it(我假设它是downTo进展中某些类型的隐式迭代器)是{{ 1}}。第二次IntProgression电话也是如此。

我错过了什么?

2 个答案:

答案 0 :(得分:4)

您发布的代码包含错误:传递单个参数的sequenceOf(...)会返回包含该项的序列,即C:\Program Files (x86)\Jenkins>java -jar jenkins.war Running from: C:\Program Files (x86)\Jenkins\jenkins.war webroot: $user.home/.jenkins ao¹t 03, 2016 5:27:01 PM org.eclipse.jetty.util.log.JavaUtilLog info INFOS: Logging initialized @606ms ao¹t 03, 2016 5:27:01 PM winstone.Logger logInternal INFOS: Beginning extraction from war file ao¹t 03, 2016 5:27:01 PM org.eclipse.jetty.util.log.JavaUtilLog warn AVERTISSEMENT: Empty contextPath ao¹t 03, 2016 5:27:01 PM org.eclipse.jetty.util.log.JavaUtilLog info INFOS: jetty-9.2.z-SNAPSHOT ao¹t 03, 2016 5:27:02 PM org.eclipse.jetty.util.log.JavaUtilLog info INFOS: NO JSP Support for /, did not find org.eclipse.jetty.jsp.JettyJspServlet Jenkins home directory: C:\Users\Acer\.jenkins found at: $user.home/.jenkins ao¹t 03, 2016 5:27:03 PM org.eclipse.jetty.util.log.JavaUtilLog info INFOS: Started w.@2e3967ea{/,file:/C:/Users/Acer/.jenkins/war/,AVAILABLE}{C:\Users\Acer\.jenkins\war} ao¹t 03, 2016 5:27:03 PM org.eclipse.jetty.util.log.JavaUtilLog warn AVERTISSEMENT: FAILED ServerConnector@51c668e3{HTTP/1.1}{0.0.0.0:8080}: java.net.BindException: Address already in use: bind java.net.BindException: Address already in use: bind at sun.nio.ch.Net.bind0(Native Method) at sun.nio.ch.Net.bind(Unknown Source) at sun.nio.ch.Net.bind(Unknown Source) at sun.nio.ch.ServerSocketChannelImpl.bind(Unknown Source) at sun.nio.ch.ServerSocketAdaptor.bind(Unknown Source) at org.eclipse.jetty.server.ServerConnector.open(ServerConnector.java:321) at org.eclipse.jetty.server.AbstractNetworkConnector.doStart(AbstractNetworkConnector.java:80) at org.eclipse.jetty.server.ServerConnector.doStart(ServerConnector.java:236) at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68) at org.eclipse.jetty.server.Server.doStart(Server.java:366) at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68) at winstone.Launcher.<init>(Launcher.java:152) at winstone.Launcher.main(Launcher.java:352) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at Main._main(Main.java:246) at Main.main(Main.java:91) ao¹t 03, 2016 5:27:03 PM org.eclipse.jetty.util.log.JavaUtilLog warn AVERTISSEMENT: FAILED org.eclipse.jetty.server.Server@6221a451: java.net.BindException: Address already in use: bind java.net.BindException: Address already in use: bind at sun.nio.ch.Net.bind0(Native Method) at sun.nio.ch.Net.bind(Unknown Source) at sun.nio.ch.Net.bind(Unknown Source) at sun.nio.ch.ServerSocketChannelImpl.bind(Unknown Source) at sun.nio.ch.ServerSocketAdaptor.bind(Unknown Source) at org.eclipse.jetty.server.ServerConnector.open(ServerConnector.java:321) at org.eclipse.jetty.server.AbstractNetworkConnector.doStart(AbstractNetworkConnector.java:80) at org.eclipse.jetty.server.ServerConnector.doStart(ServerConnector.java:236) at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68) at org.eclipse.jetty.server.Server.doStart(Server.java:366) at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68) at winstone.Launcher.<init>(Launcher.java:152) at winstone.Launcher.main(Launcher.java:352) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at Main._main(Main.java:246) at Main.main(Main.java:91) ao¹t 03, 2016 5:27:03 PM org.eclipse.jetty.util.log.JavaUtilLog info INFOS: Stopped ServerConnector@51c668e3{HTTP/1.1}{0.0.0.0:8080} ao¹t 03, 2016 5:27:03 PM hudson.WebAppMain$4 run INFOS: Shutting down a Jenkins instance that was still starting up java.lang.Throwable: reason at hudson.WebAppMain$4.run(WebAppMain.java:383) at hudson.security.ACL.impersonate(ACL.java:198) at hudson.WebAppMain.contextDestroyed(WebAppMain.java:374) at org.eclipse.jetty.server.handler.ContextHandler.callContextDestroyed(ContextHandler.java:808) at org.eclipse.jetty.servlet.ServletContextHandler.callContextDestroyed(ServletContextHandler.java:457) at org.eclipse.jetty.server.handler.ContextHandler.doStop(ContextHandler.java:842) at org.eclipse.jetty.servlet.ServletContextHandler.doStop(ServletContextHandler.java:215) at org.eclipse.jetty.webapp.WebAppContext.doStop(WebAppContext.java:529) at org.eclipse.jetty.util.component.AbstractLifeCycle.stop(AbstractLifeCycle.java:89) at org.eclipse.jetty.util.component.ContainerLifeCycle.stop(ContainerLifeCycle.java:143) at org.eclipse.jetty.util.component.ContainerLifeCycle.doStop(ContainerLifeCycle.java:162) at org.eclipse.jetty.server.handler.AbstractHandler.doStop(AbstractHandler.java:73) at org.eclipse.jetty.server.Server.doStop(Server.java:456) at org.eclipse.jetty.util.component.AbstractLifeCycle.stop(AbstractLifeCycle.java:89) at winstone.Launcher.shutdown(Launcher.java:305) at winstone.Launcher.<init>(Launcher.java:165) at winstone.Launcher.main(Launcher.java:352) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at Main._main(Main.java:246) at Main.main(Main.java:91) ao¹t 03, 2016 5:27:03 PM org.eclipse.jetty.util.log.JavaUtilLog info INFOS: Stopped w.@2e3967ea{/,file:/C:/Users/Acer/.jenkins/war/,UNAVAILABLE}{C:\Users\Acer\.jenkins\war} ao¹t 03, 2016 5:27:03 PM winstone.Logger logInternal INFOS: Winstone shutdown successfully ao¹t 03, 2016 5:27:03 PM winstone.Logger logInternal GRAVE: Container startup failed java.io.IOException: Failed to start Jetty at winstone.Launcher.<init>(Launcher.java:154) at winstone.Launcher.main(Launcher.java:352) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at Main._main(Main.java:246) at Main.main(Main.java:91) Caused by: java.net.BindException: Address already in use: bind at sun.nio.ch.Net.bind0(Native Method) at sun.nio.ch.Net.bind(Unknown Source) at sun.nio.ch.Net.bind(Unknown Source) at sun.nio.ch.ServerSocketChannelImpl.bind(Unknown Source) at sun.nio.ch.ServerSocketAdaptor.bind(Unknown Source) at org.eclipse.jetty.server.ServerConnector.open(ServerConnector.java:321) at org.eclipse.jetty.server.AbstractNetworkConnector.doStart(AbstractNetworkConnector.java:80) at org.eclipse.jetty.server.ServerConnector.doStart(ServerConnector.java:236) at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68) at org.eclipse.jetty.server.Server.doStart(Server.java:366) at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68) at winstone.Launcher.<init>(Launcher.java:152) ... 7 more ao¹t 03, 2016 5:27:03 PM hudson.util.BootFailure publish GRAVE: Failed to initialize Jenkins hudson.util.HudsonFailedToLoad: java.lang.InterruptedException at hudson.WebAppMain$3.run(WebAppMain.java:244) Caused by: java.lang.InterruptedException at java.lang.Object.wait(Native Method) at java.lang.Object.wait(Unknown Source) at org.jvnet.hudson.reactor.Reactor.execute(Reactor.java:267) at jenkins.InitReactorRunner.run(InitReactorRunner.java:45) at jenkins.model.Jenkins.executeReactor(Jenkins.java:1010) at jenkins.model.Jenkins.<init>(Jenkins.java:878) at hudson.model.Hudson.<init>(Hudson.java:85) at hudson.model.Hudson.<init>(Hudson.java:81) at hudson.WebAppMain$3.run(WebAppMain.java:227) ao¹t 03, 2016 5:27:03 PM jenkins.model.Jenkins cleanUp INFOS: Stopping Jenkins ao¹t 03, 2016 5:27:03 PM jenkins.model.Jenkins$21 onAttained INFOS: Started termination ao¹t 03, 2016 5:27:03 PM jenkins.model.Jenkins$21 onTaskFailed GRAVE: Failed NioChannelSelector.cleanUp java.lang.IllegalArgumentException: Unable to inject class jenkins.slaves.NioChannelSelector at hudson.init.TaskMethodFinder.lookUp(TaskMethodFinder.java:125) at hudson.init.TaskMethodFinder.invoke(TaskMethodFinder.java:104) at hudson.init.TaskMethodFinder$TaskImpl.run(TaskMethodFinder.java:175) at org.jvnet.hudson.reactor.Reactor.runTask(Reactor.java:282) at org.jvnet.hudson.reactor.Reactor$2.run(Reactor.java:210) at org.jvnet.hudson.reactor.Reactor$Node.run(Reactor.java:117) at jenkins.model.Jenkins$20.execute(Jenkins.java:3020) at org.jvnet.hudson.reactor.Reactor$Node.runIfPossible(Reactor.java:139) at org.jvnet.hudson.reactor.Reactor$Node.run(Reactor.java:128) at jenkins.model.Jenkins$20.execute(Jenkins.java:3020) at org.jvnet.hudson.reactor.Reactor$Node.runIfPossible(Reactor.java:139) at org.jvnet.hudson.reactor.Reactor.execute(Reactor.java:263) at jenkins.model.Jenkins._cleanUpRunTerminators(Jenkins.java:3017) at jenkins.model.Jenkins.cleanUp(Jenkins.java:2939) at hudson.WebAppMain$3.run(WebAppMain.java:248) ao¹t 03, 2016 5:27:03 PM jenkins.model.Jenkins$21 onAttained INFOS: Completed termination ao¹t 03, 2016 5:27:03 PM jenkins.model.Jenkins _cleanUpDisconnectComputers INFOS: Starting node disconnection ao¹t 03, 2016 5:27:03 PM jenkins.model.Jenkins _cleanUpShutdownPluginManager INFOS: Stopping plugin manager ao¹t 03, 2016 5:27:03 PM jenkins.model.Jenkins _cleanUpPersistQueue INFOS: Persisting build queue ao¹t 03, 2016 5:27:04 PM jenkins.model.Jenkins cleanUp INFOS: Jenkins stopped。要获得一系列索引(Sequence<IntProgression>),请使用asSequence()函数:

Sequence<Int>

名为second的 (canonicalisedInput.lastIndex + 1 downTo 0).asSequence().map { ... } 函数是the overload that returns the substring starting from the index passed as the argument

substring(...)implicit name for the innermost lambda single parameter,在您的情况下,它是it的参数,即lambda要映射的序列项。

因此,lambda中的表达式是两个子串的一对(由to infix function创建),一个从原始字符串的开头到序列中的索引,另一个 - 从该索引到字符串的结尾。

所以代码肯定应该与索引序列一起使用,这就是为什么错误很明显。

答案 1 :(得分:3)

sequenceOf(canonicalisedInput.lastIndex + 1 downTo 0) - 此表达式创建一个由单个IntProgression项组成的序列。

如果您想将IntProgression转换为Sequence<Int>,请使用asSequence扩展功能:

(canonicalisedInput.length downTo 0).asSequence()