This is quick, although not necessarily time-optimal:

1
2
3
4
5
6
7
let products = [
    ("type1", "prod1", "pn1");
    ("type1", "break", "pn2");
    ("type2", "break", "pn3")]
    

products |> List.filter (fun (t, p, _) -> p = "break" && (List.exists (fun (tt, pp, _) -> tt = t && pp <> "break") products))
By on 6/1/2010 10:51 AM ()

so let me modify this a bit (first time f# for me)

Ok after staring at this for 20 minutes, I understand how the list is made, but what I would like to do is mark the ones in the resulting list.

So would I do something like this?

let MyShortList = products

products |> List.filter (fun (t, p, r) -> p = "break" && (List.exists (fun (tt, pp, r) -> tt = t && pp <> "break") products))

MyShortLIst.filter -> List

MyShortLIst[2] = "mark";

In order for this to be correct pn2 should be changed to "mark" to mark the record.

So I would right

By on 6/1/2010 11:50 AM ()

Fox, sorry for being cryptic.

What I used to represent the data is a list of tuples. Tuples (like all functional data structures) can't be modified, so you can't assign a field a new value.

You have several options:

1) Map the list to a NEW list with new tuples where the matching ones are 'marked'.

2) Use arrays instead of tuples (which can take updates).

3) Rethink your application. Do you NEED to mark those tuples retaining the original ones? Or are you gonna filter them out later anyway? If your next operation is going to use only the marked ones, you may as well filter now (think functionally ;) ).

A solution for 1) however can be:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
let products = [
    ("type1", "prod1", "pn1");
    ("type1", "break", "pn2");
    ("type2", "break", "pn3")]


let isBreak allprods (t, p, _) =
    p = "break" && (List.exists (fun (tt, pp, _) -> tt = t && pp <> "break") allprods)
    

let mark allprods (t, p, n) =
    if isBreak allprods (t, p, n) then (t, p, "mark") else (t, p, n)


let markedProducts = products |> List.map (mark products)
By on 6/1/2010 12:05 PM ()

I am trying to mark them so that I can filter them out (exclude them).

So I guess I could create the list with opposite logic and filter them out that way.

That way I would have a list of the items that will be used.

By on 6/1/2010 12:12 PM ()

I knew that ;)

So this filters them OUT (i'm composing isBreak with not):

1
2
3
4
5
6
7
8
9
10
11
let products = [
    ("type1", "prod1", "pn1");
    ("type1", "break", "pn2");
    ("type2", "break", "pn3")]


let isBreak allprods (t, p, _) =
    p = "break" && (List.exists (fun (tt, pp, _) -> tt = t && pp <> "break") allprods)


let goodProducts = products |> List.filter ((isBreak products) >> not)
By on 6/1/2010 12:20 PM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper