Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

looping through F# record like Javascript object

Tags:

f#

In javascript, I can access every property of an object with a simple for loop as follows

var myObj = {x:1, y:2};
var i, sum=0;
for(i in myObj) sum = sum + myObj[i];

I am wondering if I can do similar thing with F#.

type MyObj = {x:int; y:int}
let myObj = {x=1; y=2}
let allValues:seq<int> = allPropertyValuesIn myObj //How do I implement allPropertyValuesIn 
let sum = allValues |> Seq.fold (+) 0

Thank you for your input

Edit to clarify why I want to do such thing
I am working on an XML file generator. The input is rows read from Database, and the xsd is predefined.

Lets say I have a "Product" Element needs to be generated and depending on the business rule, there could be 200 Children element under product some are required, some are optional. Following the advise from this excellent blog, I have had my first (very rough) design for product record:

1.    type Product{ Price:Money; Name:Name; FactoryLocation:Address option ... }
2.    let product = {Price = Money(1.5); Name = Name ("Joe Tooth Paste"); ... }
3.    let child1 = createEl ("Price", product.Price)
   ..
203.  let allChildren = child1
                        ::child2
                        ::child3
                        ..
                        ::[]
404.  let prodctEl = createElWithCildren ("Product", allChildren)

This is very tedious and un-succinct. There HAS to be a better way to do such thing in F#. I am not very kin on the reflection idea either.

Are there any other approaches or I am just doing it wrong?

like image 397
Wei Ma Avatar asked Jun 07 '13 05:06

Wei Ma


People also ask

What is looping through a string?

For loops are used when you know you want to visit every character. For loops with strings usually start at 0 and use the string's length() for the ending condition to step through the string character by character. String s = "example"; // loop through the string from 0 to length for(int i=0; i < s.

Can you loop through a function in Python?

To loop through a set of code a specified number of times, we can use the range() function, The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and ends at a specified number.

How do you do a backslash in F string?

We can use any quotation marks {single or double or triple} in the f-string. We have to use the escape character to print quotation marks. The f-string expression doesn't allow us to use the backslash. We have to place it outside the { }.

Can you for loop through a dictionary?

You can loop through a dictionary by using a for loop. When looping through a dictionary, the return value are the keys of the dictionary, but there are methods to return the values as well.


1 Answers

Try this:

open Microsoft.FSharp.Reflection

type MyObj = {x:int; y:int}
let myObj = {x=1; y=2}
let allValues = FSharpType.GetRecordFields (myObj.GetType())
let sum =
    allValues
    |> Seq.fold
        (fun s t -> s + int(t.GetValue(myObj).ToString()))
        0
printfn "%d" sum

However, as John Palmer admonishes, there are not very many good reasons for doing something like this.

like image 151
Shredderroy Avatar answered Oct 28 '22 03:10

Shredderroy