Grouping a list into a list of lists in F# - f#

Okay so I'm lost on this one. I have a list of objects. Each object as a non-unique ID in it. I want to group on this ID but for the life of me I can't figure out how to do this.
This is what I have
type fooObject = {
Id : int
Info : string
}
let fooObjects: fooObject list
The data might look something like this
[ { Id = 1 ; Data = "foo" } ; { Id = 1 ; Data = "also foo" } ; { Id = 2 ; Data = "Not foo" } ]
I would like something like
let fooObjectsGroupedById : fooObject list list
So the final result would look like this
[ [{ Id = 1 ; Data = "foo" } ; { Id = 1 ; Data = "also foo" } ] ; [{ Id = 2 ; Data = "Not foo" }]]

Use List.groupBy:
let groupById fooObjects =
List.groupBy (fun foo -> foo.Id) fooObjects
|> List.map snd

Related

How to do CosmosDB JOIN to get an array of Column values

I have following data in cosmosDb
[
{
accountId : "12453"
tag : "abc"
},
{
accountId : "12453"
tag : "bcd"
},
{
accountId : "34567"
tag : "qwe"
},
{
accountId : "34567"
tag : "xcx"
}
]
And desired output is something like this
[
{
accountId : "123453"
tag : {
"abc",
"bcd"
},
{
accountId : "34567"
tag : {
"qwe",
"xcx"
}
]
I tried Join, array and group by in multiple ways but didn't work.
Below query gives similar output but rather than count I am looking for an array of tags
select c.accountId, count(c.tag) from c where c.accountId = "12453" group BY c.accountId
Also tried below query
select c.accountId, ARRAY(select c.tag from c IN f where f.accountId = "12453") as tags FROM f
This doesn't return any tag values and I get below output but I need distinct accountId and when I try DISTINCT on accountId, it gives an error.
[
{
accountId : "12,
tag : []
}
........
]
Can someone please help with correct query syntax
As M0 B said, this is not supported. Cosmos DB can't combine arrays across documents. You need to handle this on your client side.

F# extracting tuple from list of tuples

I have a list of clients, which consists of a string * int * string * int * string list. I've made a function where I give a string, an int and a string list. Given some rules, I extract the correct tuple from the list.
An example of three clients:
let client1 = "Jon", 37514986, "Male", 1980, ["Cars"; "Boats"; "Airplanes"]
let client2 = "Jonna", 31852654, "Female", 1990, ["Makeup"; "Sewing"; "Netflix"]
let client3 = "Jenna", 33658912, "Female", 1970, ["Robe Swinging"; "Llamas"; "Music"]
let file1 = [client1; client2; client3]
//Response must be client(s) with different sex, age diff <= 10 and least one common interest
let request (sex:string) (yob:int) (interests:string list) =
Set.fold (fun x (nm,ph,sx,yb,toi) -> if sex<>sx &&
yb-yob < 10
then (nm,ph,sx,yb,toi) else x) ("",0,"",0,[]) (Set.ofList file1)
request "Male" 1976 ["Paper"; "Llamas"; "Space"] //Expected ("Jenna", 33658912, "Female", 1970, ["Robe Swinging"; "Llamas"; "Music"])
So, I guess you can call it kind of a dating bureau. In the above example, I request all clients, which do not share the same sex as me and do not have an age difference larger than 10. I'm not using interests as of now, as I'm still working on that, but it should compare the given interests and the interets of a client and see if there's at least one similarity.
But my current problem is that it stops and returns at the first compatible client, but what if there's more? How do I make it continue and build up a set of clients?
I was trying to do something with Set, hence the Set.ofList, but I'm somehow not feeling I get any of the benefits out of using a Set as it is right now.
I always get confused when using n-tuples (here: quintuple) with n > 3 or non-unique types. Records are much easier to understand:
type Sex = Male | Female
type Client = { name: string; id: int; sex: Sex; YearOfBirth: int; interests: Set<string> }
constructing values is a bit more verbose then:
let client1 = { name = "Jon"; id = 37514986; sex = Male; YearOfBirth = 1980; interests = ["Cars"; "Boats"; "Airplanes"] |> Set.ofList }
let client2 = { name = "Jonna"; id = 31852654; sex = Female; YearOfBirth = 1990; interests = ["Makeup"; "Sewing"; "Netflix"] |> Set.ofList }
let client3 = { name = "Jenna"; id = 33658912; sex = Female; YearOfBirth = 1970; interests = ["Robe Swinging"; "Llamas"; "Music"] |> Set.ofList }
let file1 = [client1; client2; client3]
If you really need to list many values in code, create a helper function (or constructor) mapping tuples to the record.
When filtering the list, you can then match on just the values you need (note that name is not used):
let request sex yob interests =
file1
|> List.filter (fun { sex = s; YearOfBirth = y; interests = i } ->
sex <> s && abs(yob-y)<= 10 && i |> Set.intersect interests |> (not << Set.isEmpty))
request Male 1976 (["Paper"; "Llamas"; "Space"] |> Set.ofList)
val it : Client list =
[{name = "Jenna";
id = 33658912;
sex = Female;
YearOfBirth = 1970;
interests = set ["Llamas"; "Music"; "Robe Swinging"];}]

Handle Tree Format Array in Table View -objective c

I have a Json response with tree structure array. Now the problem is i have to display that array in table view. The array format is like
details = {
id = 002;
name = Rohit;
fid = 123;
"Friends" = (
{
id = 003;
name = "Sham";
fid = 2355;
"Friends" = (
{
id = 252;
name = Ram;
Fid = 8568;
"Friends" = (
{
id = 7545;
name = "Rahul";
Fid = 874;
"Friends" = (
);
},
{
id = 77554;
name = "LCMff";
pid = 45425;
"Friends"= (
);
},
{
id = 4545;
name = peter;
fid = 4548;
"Friends"= (
{
id = 785612;
name = "john";
fid = 45;
"Friends" = (
);
}
);
},
},
Above is the example. Now i have to display Friends list in table view on click of name.Also i have to display the name in header like bread crumb. Help me out to resolve this.

How to print index of selected option in Elm?

I have a <select> HTML element with 3 options and a <p> element. In the <p> element I want to print index of the currently selected item in <select>. E.g. if I select the first option, it should print 0, if I select the second option, it should print 1, and so on. How do I proceed from the minimal code, which is given below?
import Html as H exposing (Html)
import Maybe
import Signal as S exposing (Address, (<~))
type alias Model = { selected : Maybe Int }
model = { selected = Nothing }
type Action = NoOp | Select Int
update action model =
case action of
NoOp -> model
Select n -> { model | selected <- Just n }
view address model =
H.div []
[ H.select [] [ H.option [] [ H.text "0" ]
, H.option [] [ H.text "1" ]
, H.option [] [ H.text "2" ]
]
, H.p [] [ H.text <| Maybe.withDefault ""
<| Maybe.map toString model.selected ]
]
actions = Signal.mailbox NoOp
main = view actions.address <~ S.foldp update model actions.signal
There's a lot of different events in elm-html 2.0.0, but nothing relevant to the <select> HTML element. So you definitely need a custom event handler, which you can create using on. It has a type:
on : String -> Decoder a -> (a -> Message a) -> Attribute
The event that is triggered every time you select an option inside the <select> is called “change”. What you need is targetSelectedIndex from elm-community/html-extra which ustilizes a selectedIndex property.
The final code would look like this:
Updated to Elm-0.18
import Html exposing (..)
import Html.Events exposing (on, onClick)
import Html.Attributes exposing (..)
import Json.Decode as Json
import Html.Events.Extra exposing (targetSelectedIndex)
type alias Model =
{ selected : Maybe Int }
model : Model
model =
{ selected = Nothing }
type Msg
= NoOp
| Select (Maybe Int)
update : Msg -> Model -> Model
update msg model =
case msg of
NoOp ->
model
Select s ->
{ model | selected = s }
view : Model -> Html Msg
view model =
let
selectEvent =
on "change"
(Json.map Select targetSelectedIndex)
in
div []
[ select [ size 3, selectEvent ]
[ option [] [ text "1" ]
, option [] [ text "2" ]
, option [] [ text "3" ]
]
, p []
[ text <|
Maybe.withDefault "" <|
Maybe.map toString model.selected
]
]
main : Program Never Model Msg
main =
beginnerProgram { model = model, view = view, update = update }
You can run it in browser here https://runelm.io/c/xum

Retrieve table name

Is there a way to retrieve the name of a table? For example, from this table I want extract only the keys name "Mimic", "Molibdeno", "Tamarindo", "Wrenna"
UnitScanDB = {
profiles = {
Mimic = {
...
},
Molibdeno = {
...
},
Tamanrindo = {
...
},
Wrenna = {
...
}
}
}
You can iterate over the inner table using pairs:
for k in pairs(UnitScanDB.profiles) do
-- handle k
end
Using the above loop, you could, for example, copy all of the keys into a new table:
local names = {}
for k in pairs(UnitScanDB.profiles) do
table.insert(names, k)
end
You can simply access those keys by their name, like so:
mimic = UnitScanDB.profiles.Mimic
molibdeno = UnitScanDB.profiles.Molibdeno

Resources