FP: SQL, LINQ, F # and STL (2)

Source: Internet
Author: User
Document directory
  • Aggregate (count)
  • Aggregation (count)
  • Aggregate (SUM)
  • Aggregation (SUM)
  • Aggregate (min)
  • Aggregation (minimum)
  • Aggregate (max)
  • Aggregation (maximum)
  • Aggregate (average)
  • Aggregation (average)
  • Aggregate (simple)
  • Simple aggregation
  • Aggregate (SEED)
  • Aggregation with seeds
Aggregate (count) aggregation (count)
  • The Count operation is used to count the number of members that meet certain conditions in the original set. The result value is an integer.
    The following table describes how to implement the Count operation in each language:

      Count
    SQL (select) Count Function
    C # (LINQ) Count Extension Method
    F # (list) Implemented by the list. Filter and list. Length functions.
    C ++ (STL) Count, count_if, and other algorithm Functions
    Haskell (Prelude) Implemented jointly by the filter and length Functions

    The following sample code is used to count the number of odd members in numbers of an integer set.

  • SQL (select)
  • Table (input): Numbers <br/> Field Data <br/> num 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 </P> <p> select count (*) as oddnumbers into result from numbers where num mod 2 = 1 </P> <p> table (output ): result <br/> Field Data <br/> oddnumbers 5

  • C # (LINQ)Int [] numbers = {5, 4, 1, 3, 9, 8, 6, 7, 2, 0}; <br/> int oddnumbers = numbers. count (n => N % 2 = 1); <br/> // oddnumbers = 5
  • F # (list)
  • Let numbers = [5; 4; 1; 3; 9; 8; 6; 7; 2; 0] <br/> let oddnumbers = numbers |> list. filter (fun num-> num % 2 = 1) |> list. length <br/> // oddnumbers = 5

  • STL (C ++)
  • Int numbers [] = {5, 4, 1, 3, 9, 8, 6, 7, 2, 0}; <br/> int oddnumbers = count_if (numbers, numbers + 10, <br/> [] (INT num) {return num % 2 = 1 ;}); <br/> // oddnumbers = 5

  • Haskell (Prelude)
  • Prelude> let numbers = [5, 4, 1, 3, 9, 8, 6, 7, 2, 0] <br/> prelude> let count f = length. filter F <br/> prelude> let oddnumbers = count (/n-> n'mod' 2 = 1) Numbers <br/> prelude> oddnumbers <br/> 5

Aggregate (SUM) aggregation (SUM)
  • The sum operation is used to calculate the sum of members in the original set. The result type is the same as that in the original set.
    The following table describes how to implement the sum operation in each language:

      Sum
    SQL (select) Sum Function
    C # (LINQ) Sum Extension Method
    F # (list) List. Sum, list. sumby, and other functions
    C ++ (STL) Accumulate algorithm Function
    Haskell (Prelude) Sum Function

    The following sample code function is used to calculate the total number of members in the Integer Set numbers.

  • SQL (select)
  • Table (input): Numbers <br/> Field Data <br/> num 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 </P> <p> select sum (Num) as numsum into result from numbers </P> <p> table (output ): result <br/> Field Data <br/> numsum 45

  • C # (LINQ)Int [] numbers = {5, 4, 1, 3, 9, 8, 6, 7, 2, 0}; <br/> double numsum = numbers. sum (); <br/> // numsum = 45
  • F # (list)
  • Let numbers = [5; 4; 1; 3; 9; 8; 6; 7; 2; 0] <br/> let numsum = List. sum numbers <br/> // numsum = 45

  • STL (C ++)
  • Int numbers [] = {5, 4, 1, 3, 9, 8, 6, 7, 2, 0}; <br/> int numsum = accumulate (numbers, numbers + 10, 0, <br/> [] (int acc, int num) {return ACC + num ;}); <br/> // numsum = 45

  • Haskell (Prelude)
  • Prelude> let numbers = [5, 4, 1, 3, 9, 8, 6, 7, 2, 0] <br/> prelude> let numsum = sum numbers <br/> prelude> numsum <br/> 45

Aggregate (min) aggregation (minimum)
  • The minimum operation is used to find the smallest member in the original set. The result type is the same as that in the original set.
    The following table describes how to implement the minimum operation in each language:

      Min
    SQL (select) Min Function
    C # (LINQ) Min Extension Method
    F # (list) List. Min, list. minby, and other functions
    C ++ (STL) Min_element Algorithm functions
    Haskell (Prelude) Minimum, minimumby, and other functions

    The following sample code function is used to find the smallest member in the Integer Set numbers.

  • SQL (select)
  • Table (input): Numbers <br/> Field Data <br/> num 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 </P> <p> select Min (Num) as minnum into result from numbers </P> <p> table (output ): result <br/> Field Data <br/> minnum 0

  • C # (LINQ)Int [] numbers = {5, 4, 1, 3, 9, 8, 6, 7, 2, 0}; <br/> int minnum = numbers. min (); <br/> // minnum = 0
  • F # (list)
  • Let numbers = [5; 4; 1; 3; 9; 8; 6; 7; 2; 0] <br/> let minnum = List. min numbers <br/> // minnum = 0

  • STL (C ++)
  • Int numbers [] = {5, 4, 1, 3, 9, 8, 6, 7, 2, 0}; <br/> int minnum = * min_element (numbers, numbers + 10); <br/> // minnum = 0

  • Haskell (Prelude)
  • Prelude> let numbers = [5, 4, 1, 3, 9, 8, 6, 7, 2, 0] <br/> prelude> let minnum = minimum numbers <br/> prelude> minnum <br/> 0

Aggregate (max) aggregation (maximum)
  • The maximum operation is used to find the largest member in the original set. The result type is the same as that in the original set.
    The following table describes how to implement the maximum operation in each language:

      Max
    SQL (select) Max Functions
    C # (LINQ) Max Extension Method
    F # (list) List. Max, list. maxby, and other functions
    C ++ (STL) Max_element Algorithm functions
    Haskell (Prelude) Maximum, maximumby, and other functions

    The following code function is used to find the largest member in numbers, an integer set.

  • SQL (select)
  • Table (input): Numbers <br/> Field Data <br/> num 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 </P> <p> select max (Num) as maxnum into result from numbers </P> <p> table (output ): result <br/> Field Data <br/> maxnum 9

  • C # (LINQ)Int [] numbers = {5, 4, 1, 3, 9, 8, 6, 7, 2, 0}; <br/> int maxnum = numbers. max (); <br/> // maxnum = 9
  • F # (list)
  • Let numbers = [5; 4; 1; 3; 9; 8; 6; 7; 2; 0] <br/> let maxnum = List. max numbers <br/> // maxnum = 9

  • STL (C ++)
  • Int numbers [] = {5, 4, 1, 3, 9, 8, 6, 7, 2, 0}; <br/> int maxnum = * max_element (numbers, numbers + 10); <br/> // maxnum = 9

  • Haskell (Prelude)
  • Prelude> let numbers = [5, 4, 1, 3, 9, 8, 6, 7, 2, 0] <br/> prelude> let maxnum = maximum numbers <br/> prelude> maxnum <br/> 9

Aggregate (average) aggregation (average)
  • The average operation is used to calculate the average value of each member in the original set. The result type is the same as that of the original set.
    The implementation of the average operation in each language is shown in the following table:

      Average
    SQL (select) AVG Functions
    C # (LINQ) Average Extension Method
    F # (list) List. Average, list. averageby, and other functions
    C ++ (STL) No corresponding algorithm function, which is indirectly implemented by the accumulate algorithm Function
    Haskell (Prelude) Implemented jointly by the sum and length Functions

    The following sample code function is used to calculate the average value of each member in numbers of an integer set.

  • SQL (select)
  • Table (input): Numbers <br/> Field Data <br/> num 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 </P> <p> select AVG (Num) as averagenum into result from numbers </P> <p> table (output ): result <br/> Field Data <br/> averagenum 4.5.

  • C # (LINQ)Int [] numbers = {5, 4, 1, 3, 9, 8, 6, 7, 2, 0}; <br/> double averagenum = numbers. average (); <br/> // averagenum = 4.5
  • F # (list)
  • Let numbers = [5 .; 4 .; 1 .; 3 .; 9 .; 8 .; 6 .; 7 .; 2 .; 0.] <br/> let averagenum = List. average Numbers <br/> // averagenum = 4.5

  • STL (C ++)
  • Int numbers [] = {5, 4, 1, 3, 9, 8, 6, 7, 2, 0}; <br/> double averagenum = accumulate (numbers, numbers + 10, 0, <br/> [] (int acc, int num) {return ACC + num;})/10 .; <br/> // averagenum = 4.5

  • Haskell (Prelude)
  • Prelude> let numbers = [5, 4, 1, 3, 9, 8, 6, 7, 2, 0] <br/> prelude> let averagenum = fromintegral (sum numbers) /fromintegral (length numbers) <br/> prelude> averagenum <br/> 4.5

Aggregate (simple) simple aggregation
  • Simple aggregation operation.
    The implementation of simple aggregation in various languages is shown in the following table:

      Aggregate (simple)
    SQL (select) No corresponding function
    C # (LINQ) Aggregate Extension Method
    F # (list) List. Reduce, list. ceceback, and other functions
    C ++ (STL) Accumulate algorithm Function
    Haskell (Prelude) Foldl1, foldr1, and other functions

    The following sample code function is used to calculate the product of all members in the double-precision floating point number set doubles.

  • SQL (select)
  • C # (LINQ)Double [] doubles = {1.7, 2.3, 1.9, 4.1, 2.9}; <br/> double Product = doubles. aggregate (runningproduct, nextfactor) => runningproduct * nextfactor); <br/> // Product = 88.330809999999985
  • F # (list)
  • Let doubles = [1.7; 2.3; 1.9; 4.1; 2.9] <br/> let Product = List. reduce (fun runningproduct nextfactor-> runningproduct * nextfactor) doubles <br/> // Product = 88.33081

  • C ++ (STL)
  • Double Doubles [] = {1.7, 2.3, 1.9, 4.1}; <br/> double Product = accumulate (doubles + 1, doubles + 5, doubles [0], <br/> [] (double runningproduct, double nextfactor) {return runningproduct * nextfactor;}); <br/> // Product = 88.330809999999985

  • Haskell (Prelude)
  • Prelude> let doubles = [1.7, 2.3, 1.9, 4.1, 2.9] <br/> prelude> let productofdoubles = foldl1 (/runningproduct nextfactor-> runningproduct * nextfactor) doubles <br/> prelude> productofdoubles <br/> 88.33080999999999

Aggregate (SEED) aggregation with seeds
  • Aggregation with seeds.
    The following table describes how to implement the aggregation operation with seeds in each language:

      Aggregate (simple)
    SQL (select) No corresponding function
    C # (LINQ) Aggregate Extension Method
    F # (list) List. Fold, list. foldback, and other functions
    C ++ (STL) Accumulate algorithm Function
    Haskell (Prelude) Functions such as foldl and foldr

    The following sample code function is used to calculate the account balance by deducting the withdrawal amount from the account (ignoring the withdrawal items greater than the deposit.

  • SQL (select)
  • C # (LINQ)Double startbalance = 100.0; <br/> int [] attemptedwithdrawals = {20, 10, 40, 50, 10, 70, 30 }; <br/> double endbalance = <br/> attemptedwithdrawals. aggregate (startbalance, <br/> (balance, nextwithdrawal) => <br/> (nextwithdrawal <= balance )? (Balance-nextwithdrawal): Balance). <br/> // endbalance = 20.0
  • F # (list)
  • Let startbalance = 100.0 <br/> let attemptedwithdrawals = [20; 10; 40; 50; 10; 70; 30] <br/> let endbalance = List. fold (fun balance-> <br/> let nextwithdrawal = float nextwithdrawal2 <br/> If nextwithdrawal <= balance then balance-nextwithdrawal <br/> else balance) startbalance attemptedwithdrawals <br/> // endbalance = 20.0

  • C ++ (STL)
  • Double startbalance = 100.0; <br/> int attemptedwithdrawals [] = {20, 10, 40, 50, 10, 70, 30 }; <br/> double endbalance = accumulate (attemptedwithdrawals, attemptedwithdrawals + 7, <br/> startbalance, [] (double balance, int nextwithdrawal) {<br/> return (nextwithdrawal <= balance )? (Balance-nextwithdrawal): Balance) ;}); <br/> // endbalance = 20.000000000000000

  • Haskell (Prelude)
  • Prelude> letstartbalance = 100.0 <br/> prelude> let attemptedwithdrawals = [20, 10, 40, 50, 10, 70, 30] <br/> prelude> let endbalance = foldl (/balance-> let nextwithdrawal = fromintegral balance in if nextwithdrawal <= balance then balance-nextwithdrawal else balance) startbalance attemptedwithdrawals <br/> prelude> endbalance <br/> 20.0

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.