Skip to content

Commit

Permalink
Rewrite simple option pattern matching with Option.fold.
Browse files Browse the repository at this point in the history
Turn simple pattern matching involding option types into
one-liner using Option.fold.
  • Loading branch information
zoj613 committed Aug 29, 2024
1 parent 0c0dc1f commit 840d5e8
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 31 deletions.
10 changes: 4 additions & 6 deletions examples/inmemory_zipstore.ml
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,11 @@ end = struct
| Ok s -> s

let get_partial_values t key ranges =
get t key >>= fun data ->
get t key >>| fun data ->
let size = String.length data in
ranges |> Lwt_list.map_p @@ fun (offset, len) ->
Deferred.return
(match len with
| None -> String.sub data offset (size - offset)
| Some l -> String.sub data offset l)
ranges |> List.map @@ fun (ofs, len) ->
let f v = String.sub data ofs v in
Option.fold ~none:(f (size - ofs)) ~some:f len

let list t =
Deferred.return @@ Zipc.fold
Expand Down
7 changes: 3 additions & 4 deletions examples/readonly_zipstore.ml
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,9 @@ end = struct
let get_partial_values t key ranges =
get t key >>= fun data ->
let size = String.length data in
ranges |> Eio.Fiber.List.map @@ fun (offset, len) ->
match len with
| None -> String.sub data offset (size - offset)
| Some l -> String.sub data offset l
ranges |> Eio.Fiber.List.map @@ fun (ofs, len) ->
let f v = String.sub data ofs v in
Option.fold ~none:(f (size - ofs)) ~some:f len

let list t =
Zip.entries t |> Eio.Fiber.List.filter_map @@ function
Expand Down
11 changes: 4 additions & 7 deletions zarr-sync/src/storage.ml
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,10 @@ module FilesystemStore = struct
(fun ic ->
let size = In_channel.length ic |> Int64.to_int in
List.map
(fun (rs, len) ->
let len' =
match len with
| Some l -> l
| None -> size - rs in
In_channel.seek ic @@ Int64.of_int rs;
Option.get @@ In_channel.really_input_string ic len') ranges)
(fun (ofs, len) ->
In_channel.seek ic @@ Int64.of_int ofs;
let l = Option.fold ~none:(size - ofs) ~some:Fun.id len in
Option.get @@ In_channel.really_input_string ic l) ranges)

let set t key value =
let filename = key_to_fspath t key in
Expand Down
8 changes: 3 additions & 5 deletions zarr/src/storage/memory.ml
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,12 @@ module Make (Deferred : Types.Deferred) = struct
if not success then erase_prefix t pre else Deferred.return_unit

let get_partial_values t key ranges =
get t key >>= fun v ->
get t key >>| fun v ->
let size = String.length v in
Deferred.return @@
List.fold_left
(fun acc (ofs, len) ->
match len with
| None -> String.sub v ofs (size - ofs) :: acc
| Some l -> String.sub v ofs l :: acc) [] @@ List.rev ranges
let f x = String.sub v ofs x :: acc in
Option.fold ~none:(f (size - ofs)) ~some:f len) [] @@ List.rev ranges

let rec set_partial_values t key ?(append=false) rv =
let f = if append then fun acc (_, v) -> acc ^ v else
Expand Down
5 changes: 2 additions & 3 deletions zarr/src/storage/storage.ml
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,8 @@ module Make (Io : Types.IO) = struct
| false ->
let k = GroupNode.to_metakey node in
set t k GroupMetadata.(update_attributes default attrs |> encode) >>= fun () ->
match GroupNode.parent node with
| None -> Deferred.return_unit
| Some n -> create_group t n
GroupNode.parent node
|> Option.fold ~none:Deferred.return_unit ~some:(create_group t)

let create_array
?(sep=`Slash) ?(dimension_names=[]) ?(attributes=`Null)
Expand Down
8 changes: 2 additions & 6 deletions zarr/src/util.ml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ module ArrayMap = struct
end)

let add_to_list k v map =
update k (function
| None -> Some [v]
| Some l -> Some (v :: l)) map
update k (Option.fold ~none:(Some [v]) ~some:(fun l -> Some (v :: l))) map
end

module Result_syntax = struct
Expand Down Expand Up @@ -106,6 +104,4 @@ let rec create_parent_dir fn perm =
end

let sanitize_dir dir =
match Filename.chop_suffix_opt ~suffix:"/" dir with
| None -> dir
| Some d -> d
Option.fold ~none:dir ~some:Fun.id @@ Filename.chop_suffix_opt ~suffix:"/" dir

0 comments on commit 840d5e8

Please sign in to comment.