zoukankan      html  css  js  c++  java
  • List的一阶函数操作代码实战详解之Scala学习笔记-25

    package com.leegh.dataset

    /**
    * @author Guohui Li
    */
    object MergedSort {

    def main(args: Array[String]): Unit = {
    def mergedsort[T](less: (T, T) => Boolean)(input: List[T]): List[T] = {
    def merge(xList: List[T], yList: List[T]): List[T] =
    (xList, yList) match {
    case (Nil, _) => yList
    case (_, Nil) => xList
    case (x :: xtail, y :: ytail) =>
    if (less(x, y)) x :: merge(xtail, yList)
    else y :: merge(xList, ytail)
    }
    val n = input.length / 2
    if (n == 0) input
    else {
    val (x, y) = input splitAt n
    merge(mergedsort(less)(x), mergedsort(less)(y))
    }
    }
    println(mergedsort((x: Int, y: Int) => x < y)(List(3, 7, 9, 5)))
    val reversed_mergedsort = mergedsort((x: Int, y: Int) => x > y)_
    println(reversed_mergedsort(List(3, 7, 9, 5)))
    }

    }

    附:

    本博客说明:

    1.整理思路,提高自己。

    2.受教于王家林老师,​有所收获,故推荐。

    3.博客注重实践,多余的文字就不多说了,都是做技术的。

    4.信息来源于 DT大数据梦工厂微信公众账号:DT_Spark。​

    DT大数据梦工厂的微信公众号是DT_Spark,每天都会有大数据实战视频发布,请您持续学习。

    Scala 深入浅出实战经典(1-64讲)完整视频、PPT、代码下载:

    百度云盘:http://pan.baidu.com/s/1c0noOt6
    腾讯微云:http://url.cn/TnGbdC
    360云盘:http://yunpan.cn/cQ4c2UALDjSKy 访问密码 45e2


    DT大数据梦工厂的微信公众号是DT_Spark,每天都会有大数据实战视频发布,请您持续学习。

  • 相关阅读:
    vim how to set nu with 0-index instead of 1-index
    @property的介绍与使用
    X[:,0]和X[:,1]
    the best guide for git
    sorted(列表)
    Java的匿名函数
    成员变量和局部变量的区别
    Java数组合并
    Java中random的使用
    Git常见错误---git branch不显示本地分支的问题
  • 原文地址:https://www.cnblogs.com/leegh1992/p/4738325.html
Copyright © 2011-2022 走看看