zoukankan      html  css  js  c++  java
  • 返回元组

    返回元组


    首先,我们讨论为什么应该避免使用元组。假设函数返回元组,用户就必须引用  FSharp.Core.dll;另外,须要使用元组的代码C# 中看并不好。

    考虑以下的样例,我们定义了函数 hourAndMinute,它从结构 DateTime 中返回时、分。




    #light
    module Strangelights.DemoModule
    open System


    // returns the hour and minute from the give date as a tuple
    let hourAndMinute (time: DateTime) = time.Hour, time.Minute


    // returns the hour from the given date
    let hour (time: DateTime) = time.Hour
    // returns the minutes from the given date
    let minute (time: DateTime) = time.Minute


    为了从 C# 中调用这个函数。还要跟着完毕以下的样例。

    假设你使用 Visual Studio。须要在 F# 解决方式中创建一个 C# 项目,即,选择 文件-加入-新建项目,再选择 C# 控制台项目,如图 14-1 所看到的。


     
    图 14-1 怎样新建 C# 项目
    接下来,须要在 C# 项目中加入对 F# 项目的引用,然后,加入以下的 C# 类到刚创建的项目中。


    // !!! C# Source !!!
    using System;
    using Strangelights;
    using Microsoft.FSharp.Core;


    static class PrintClass {
      internal static void HourMinute() {
        // call the "hourAndMinute" function and collect the
        // tuple that's returned
        Tuple<int, int> t = DemoModule.hourAndMinute(DateTime.Now);
        // print the tuple's contents
        Console.WriteLine("Hour {0} Minute {1}", t.Item1, t.Item2);
      }
    }


    演示样例的执行结果例如以下:


    Hour 16 Minute 1


    尽管前面演示样例中的 C# 并不太难看,可是,假设把这个函数分成两个,一个返回小时。一个返回分钟。可能会更好。

  • 相关阅读:
    数学+高精度 ZOJ 2313 Chinese Girls' Amusement
    最短路(Bellman_Ford) POJ 1860 Currency Exchange
    贪心 Gym 100502E Opening Ceremony
    概率 Gym 100502D Dice Game
    判断 Gym 100502K Train Passengers
    BFS POJ 3278 Catch That Cow
    DFS POJ 2362 Square
    DFS ZOJ 1002/HDOJ 1045 Fire Net
    组合数学(全排列)+DFS CSU 1563 Lexicography
    stack UVA 442 Matrix Chain Multiplication
  • 原文地址:https://www.cnblogs.com/mfmdaoyou/p/7068585.html
Copyright © 2011-2022 走看看