Skip to content

Instantly share code, notes, and snippets.

@mikehadlow
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikehadlow/bbf02c529d0b8823cd80 to your computer and use it in GitHub Desktop.
Save mikehadlow/bbf02c529d0b8823cd80 to your computer and use it in GitHub Desktop.
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
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