使用多个if-elif条件将Python函数重写为Scala if-else函数样式

时间:2017-06-14 23:05:33

标签: scala unit-testing if-statement functional-programming

我目前正在编写一个Scala对象,将24小时格式(即00:00 - 23:59)转换为Time As Words(即八点钟,八点半,四点到九点等) 。我编写了大部分代码,并且我正忙于尝试决定最合适的函数和可测试样式来创建更小的函数来处理需要根据整数单词等效的Map查找返回字符串的多个条件,并根据一天中的时间返回一个字符串。以下方法是否满足函数式编程风格,因为函数将被拆分,因为以下三个分组条件中的每一个的逻辑都不同。

所以我最初的想法是创建当前time_conversion的较小函数,以便由于逻辑和测试能力而分离出以下条件:

建议三个较小的功能:

  1. 中午和午夜异常
  2. 分钟(0,15,30,45),因为字符串模式类似,并且可以使用Map查找来生成输出字符串(即o' clock,Quater past,Half past,Quarter to)。 / LI>
  3. 小时2 +分钟> 30到期模式匹配以生成输出字符串
  4. 单词查找的Python版本:

    var children = await GetClient().Files.ListFolderAsync(dbxItem.PathLower == DROPBOX_ROOT_PATH ? string.Empty : dbxItem.PathLower, limit:100); // limit controls the page size
    
    while (children.HasMore)
    {
         children = await GetClient().Files.ListFolderContinueAsync(children.Cursor);               
    }
    return children.Entries;
    

    if-elif-else条件的Python版本:

    words_dict = {1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: 'five',
                      6: 'six', 7: 'seven', 8: 'eight', 9: 'nine', 10: 'ten',
                      11: 'eleven', 12: 'twelve', 13: 'thirteen',
                      14: 'fourteen', 16: 'sixteen', 17: 'seventeen',
                      18: 'eighteen', 19: 'nineteen', 20: 'twenty',
                      21: 'twenty one', 22: 'twenty two', 23: 'twenty three',
                      24: 'twenty four', 25: 'twenty five', 26: 'twenty six',
                      27: 'twenty seven', 28: 'twenty eight', 29: 'twenty nine'}
    

    当前Scala对象。

    def time_conversion(words_dict, hours, minutes, period):
        """Return time as words
        based on relevant condition"""
        if hours == 12:
            hours2 = words_dict.get(1)
        else:
            hours2 = words_dict.get(hours+1)
        if hours == 12 and minutes == 0 and period == 'before midday':
            time_words = 'Midnight'
        elif hours == 12 and minutes == 0 and period == 'after midday':
            time_words = 'Midday'
        elif minutes == 0:
            time_words = "{0} o'clock {1}.".format(str(words_dict.get(hours)).title(),
                                                   period)
        elif minutes == 15:
            time_words = "Quarter past {0} {1}.".format(words_dict.get(hours),
                                                        period)
        elif minutes == 30:
            time_words = "Half past {0} {1}.".format(words_dict.get(hours),
                                                     period)
        elif minutes == 45:
            time_words = "Quarter to {0} {1}.".format(hours2,
                                                      period)
        elif minutes < 30:
            min_str = words_dict.get(minutes).capitalize()
            min_num = "" if minutes == 1 else "s"
            time_words = "{0} minute{1} past {2} {3}.".format(min_str,
                                                              min_num,
                                                              words_dict.get(hours),
                                                              period)
        else:
            min_str = words_dict.get(60 - minutes).capitalize()
            min_num = "" if 60 - minutes == 1 else "s"
            time_words = '{0} minute{1} to {2} {3}.'.format(min_str,
                                                            min_num,
                                                            hours2,
                                                            period)
        return time_words
    

0 个答案:

没有答案