zoukankan      html  css  js  c++  java
  • Kotlin有点用力过猛了

    public fun readBlogHtml(blog: String) : Observable<String> {
        return Observable.just(blog)
                        .map{ blog -> assembleUrl(blog) }
                        .flatMap { url -> readHtml(url) }
    }

    搞得我有点迷乱……

    而且其实flatMap是不需要的,因为Kotlin读网页源码就一句话……

    public fun readHtml(url: String) : Observable<String> {
        return Observable.create<String> {
            subscriber -> subscriber.onNext(URL(url).readText()); subscriber.onCompleted()
        }
    }

    还不如不用Observable……

    于是就进一步变成了这样:

    public fun readBlogHtml(blog: String) : Observable<String> = Observable.just(blog)
                        .map { blog -> assembleUrl(blog) }
                        .map { url -> readHtml(url) }
    
    private fun assembleUrl(blog: String?): String = "http://$blog.tumblr.com/page/"
    
    public fun readHtml(url: String) : String = URL(url).readText()

    这代码还要不要写了?

    啊?

    RxJava都不用了……

    可以变成这样:

    fun test() {
        async {
            val html = readBlogHtml("blog")
            uiThread { update(html) }
        }
    }
    
    public fun readBlogHtml(blog: String) : String = readHtml(assembleUrl(blog))
    
    private fun assembleUrl(blog: String?): String = "http://$blog.tumblr.com/page/"
    
    public fun readHtml(url: String) : String = URL(url).readText()

    啊??

  • 相关阅读:
    spring容器启动
    springmvc流程
    bean作用域
    bean的生命周期
    web.xml详解
    设计模式 工厂和抽象工厂
    requests 模块
    爬虫基础
    提高级
    循环语句
  • 原文地址:https://www.cnblogs.com/chihane/p/5020109.html
Copyright © 2011-2022 走看看