Last active
August 29, 2015 14:21
-
-
Save mikehadlow/bbf02c529d0b8823cd80 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let private Main () = | |
// traverse that works for 'get' only | |
let traverse ((f1, f2): PLens<'a, 'b>): Lens<'a list, 'b list> = | |
(fun x -> x |> List.map f1 |> List.choose id), | |
(fun _ -> fun x -> x) // this doesn't work | |
let resourceMapLens = | |
Json.ObjectPLens | |
>??> mapPLens "links" | |
<??> Json.ArrayPIso | |
>?-> traverse (Json.ObjectPLens >??> mapPLens "href" <??> Json.StringPIso) | |
let json = | |
@"{ | |
""links"": [ | |
{ ""href"": ""https://follow-the-links/"", ""rel"": ""purchase:baskets"" }, | |
{ ""href"": ""https://follow-some-more/"", ""rel"": ""purchase:transactions"" } | |
] | |
}" | |
match Json.parse json |> Json.getLensPartial resourceMapLens with | |
| Value value, _ -> | |
let result = sprintf "%A" value | |
sprintf "%A" result | |
| Error error, _ -> error |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let resourceMapLens = | |
Json.ObjectPLens | |
>??> mapPLens "links" | |
<??> Json.ArrayPIso | |
>??> traverse // so apply the following lens to everything in the array | |
<??> Json.ObjectPIso | |
>??> mapPLens "href" | |
<??> Json.StringPIso | |
let json = | |
@"{ | |
""links"": [ | |
{ ""href"": ""https://follow-the-links/"", ""rel"": ""purchase:baskets"" }, | |
{ ""href"": ""https://follow-some-more/"", ""rel"": ""purchase:transactions"" } | |
] | |
}" | |
match Json.parse json |> Json.getLensPartial resourceMapLens with | |
| Value value, _ -> // value should be a string list | |
| Error error, _ -> // handle not found |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment