Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F# Iterate through sequence and call a function for each element of sequence

Tags:

sequence

f#

I need to call a function for each element of a sequence, currently I have tried Seq.iter and Seq.map but they return unit and 'a ->'c respectively and not Json like I need.

I have tried

Seq.iter (fun _ (a,b,c,d) -> iterateThroughMySequnce a b c d ()) sequence
Seq.fold (fun _ (a,b,c,d) -> iterateThroughMySequnce a b c d ()) sequence

but i am not getting the expected return type of Json. The code is needed below where the comment says "Something like this"

Can anyone help Thanks

open Newtonsoft.Json.Linq

type Json =
    | JObj of Json seq
    | JProp of string * Json
    | JArr of Json seq
    | JVal of obj

let (!!) (o: obj) = JVal o

let rec toJson = function
    | JVal v -> new JValue(v) :> JToken
    | JProp(name, (JProp(_) as v)) -> new JProperty(name, new JObject(toJson v)) :> JToken
    | JProp(name, v) -> new JProperty(name, toJson v) :> JToken
    | JArr items -> new JArray(items |> Seq.map toJson) :> JToken
    | JObj props -> new JObject(props |> Seq.map toJson) :> JToken

let sequence = seq { yield "USD", 12.36M, 156.32M, 18.23M
                     yield "JPY", 13.36M, 564.32M, 17.23M 
                     yield "GBP", 14.36M, 516.32M, 120.23M }

let iterateThroughMySequnce a b c d () =
    JObj [JProp("CurrencyCode", !! a);
          JProp("TotalPerCurrencyBeforeExchange", !! b); 
          JProp("ExchangeRate", !! c);
          JProp("TotalPerCurrencyAfterExchange", !! d)];

let k =
    JObj [
        JProp("InvoiceNumber", !! "13456789");
        JProp("InvoiceDate", !! "21/12/2015");
        JProp("InvoiceCurrency", !! "USD");
        JProp("InvoiceProfitMargin", !! 2.3);
        JProp("InvoicePaymentCurrencyToEuroExchangeRate", !! 0.8658745M);
        JProp("InvoicePeroid", !! "01/01/2015 00:00:00 - 01/02/2015 23:59:59");
        JProp(
            "Transaction", 
                JArr [
                    //Something like this
                    Seq.iter (fun (a,b,c,d) -> iterateThroughMySequnce a b c d ()) sequence
                ])
        JProp("TransactionForPeroid", !! 254584.00M);
        JProp("InvoicingAmountWithProfitMarginApplied", !! 8452.01M);
        JProp("InvoicingAmountWithProfitMarginAppliedInEuro", !! 7851.28);
    ]

let json = toJson k 
like image 366
Alan Mulligan Avatar asked Feb 24 '15 10:02

Alan Mulligan


People also ask

What does ⟨F⟩ mean?

This sound is usually considered to be an allophone of /h/, which is pronounced in different ways depending upon its context; Japanese /h/ is pronounced as [ɸ] before /u/. In Welsh orthography, ⟨f⟩ represents /v/ while ⟨ff⟩ represents /f/. In Slavic languages, ⟨f⟩ is used primarily in words of foreign (Greek, Latin, or Germanic) origin.

What does the letter F mean in math?

In countries such as the United States, the letter "F" is defined as a failure in terms of academic evaluation. Other countries that use this system include Saudi Arabia, Venezuela, and the Netherlands. In the hexadecimal number system, the letter "F" or "f" is used to represent the hexadecimal digit fifteen (equivalent to 15 10 ).

What does F stand for in the Etruscan alphabet?

In the Etruscan alphabet, 'F' probably represented /w/, as in Greek, and the Etruscans formed the digraph 'FH' to represent /f/.

Is the letter F doubled at the end of words?

It is often doubled at the end of words. Exceptionally, it represents the voiced labiodental fricative / v / in the common word "of". F is the twelfth least frequently used letter in the English language (after C, G, Y, P, B, V, K, J, X, Q, and Z ), with a frequency of about 2.23% in words.


1 Answers

You need Seq.map, which transforms an input sequence into an output sequence (and turns each element into a new value using the specified function). Your code is almost right, but the call should not be wrapped in another list:

JProp(
    "Transaction", 
        JArr (Seq.map (fun (a,b,c,d) -> iterateThroughMySequnce a b c d ()) sequence)
)

You can make this nicer if you change your iterateThroughMySequence function to accept a tuple (and also, it should be named differently, because it is not iterating!)

let formatItemAsJson (a,b,c,d) =
  JObj [JProp("CurrencyCode", !! a);
        JProp("TotalPerCurrencyBeforeExchange", !! b); 
        JProp("ExchangeRate", !! c);
        JProp("TotalPerCurrencyAfterExchange", !! d)];

// Later in the main part of code
JProp("Transaction", JArr (Seq.map iterateThroughMySequnce sequence))

Aside, the F# Data library comes with JsonValue type (see the API reference), which implements some of what you're doing here - it lets you construct & format JSON (and also parse it).

like image 189
Tomas Petricek Avatar answered Nov 15 '22 09:11

Tomas Petricek