Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Book paging

  • Loading branch information...
commit 870ef0c5a7ffb9d3616ef3ae427e348906ad145f 1 parent 11dcff7
Victor Nicollet authored
1  ohmPaging/ohmPaging.ml
@@ -6,3 +6,4 @@ let slice ~count list =
6 6 | list, h :: _ -> list, Some h
7 7 with _ -> list, None
8 8
  9 +module Book = OhmPaging_book
34 ohmPaging/ohmPaging.mli
... ... @@ -1,3 +1,37 @@
1 1 (* Ohm is © 2012 Victor Nicollet *)
2 2
3 3 val slice : count:int -> 'a list -> 'a list * 'a option
  4 +
  5 +module Book : sig
  6 +
  7 + type ('key,'data) t
  8 +
  9 + val make : ('key * 'data) list -> ('key, 'data) t
  10 +
  11 + type ('key,'data) page = <
  12 + first : bool ;
  13 + last : bool ;
  14 + key : 'key ;
  15 + data : 'data ;
  16 + pos : int ;
  17 + sel : bool ;
  18 + url : string option ;
  19 + prev : 'key option ;
  20 + next : 'key option ;
  21 + >
  22 +
  23 + val full_list : ?url:('key -> string option) -> ('key,'data) t -> ('key,'data) page list
  24 +
  25 + val list :
  26 + ?url:('key -> string option)
  27 + -> 'key
  28 + -> ('key,'data) t
  29 + -> ('key,'data) page list option
  30 +
  31 + val prev_next :
  32 + ?url:('key -> string option)
  33 + -> 'key
  34 + -> ('key,'data) t
  35 + -> < prev : ('key,'data) page option ; next : ('key,'data) page option > option
  36 +
  37 +end
104 ohmPaging/ohmPaging_book.ml
... ... @@ -0,0 +1,104 @@
  1 +(* Ohm is © 2012 Victor Nicollet *)
  2 +
  3 +open BatPervasives
  4 +
  5 +type ('key,'data) page = <
  6 + first : bool ;
  7 + last : bool ;
  8 + key : 'key ;
  9 + data : 'data ;
  10 + pos : int ;
  11 + sel : bool ;
  12 + url : string option ;
  13 + prev : 'key option ;
  14 + next : 'key option ;
  15 +>
  16 +
  17 +type ('key,'data) node = <
  18 + prev : ('key,'data) page option ;
  19 + next : ('key,'data) page option ;
  20 + page : ('key,'data) page
  21 +>
  22 +
  23 +type ('key,'data) prev_next = <
  24 + prev : ('key,'data) page option ;
  25 + next : ('key,'data) page option ;
  26 +>
  27 +
  28 +type ('key,'data) t = ('key,'data) node list * ('key, ('key,'data) node) BatPMap.t
  29 +
  30 +let no_url _ = None
  31 +
  32 +let selected mkurl key page = object
  33 + method first = page # first
  34 + method last = page # last
  35 + method key = page # key
  36 + method data = page # data
  37 + method pos = page # pos
  38 + method sel = page # key = key
  39 + method prev = page # prev
  40 + method next = page # next
  41 + method url = mkurl (page # key)
  42 +end
  43 +
  44 +let with_url mkurl page = object
  45 + method first = page # first
  46 + method last = page # last
  47 + method key = page # key
  48 + method data = page # data
  49 + method pos = page # pos
  50 + method sel = page # sel
  51 + method prev = page # prev
  52 + method next = page # next
  53 + method url = mkurl (page # key)
  54 +end
  55 +
  56 +let make list =
  57 + let array = Array.of_list list in
  58 + let n = Array.length array in
  59 +
  60 + let linked = Array.init n (fun i ->
  61 + let key, data = array.(i) in
  62 + (object
  63 + method first = i = 0
  64 + method last = i = n - 1
  65 + method key = key
  66 + method data = data
  67 + method pos = i
  68 + method sel = false
  69 + method prev = if i > 0 then Some (fst array.(i-1)) else None
  70 + method next = if i < n - 1 then Some (fst array.(i+1)) else None
  71 + method url = None
  72 + end)
  73 + ) in
  74 +
  75 + let nodes = Array.init n (fun i ->
  76 + let page = linked.(i) in
  77 + (object
  78 + method prev = if i > 0 then Some linked.(i-1) else None
  79 + method next = if i < n - 1 then Some linked.(i+1) else None
  80 + method page = page
  81 + end)
  82 + ) in
  83 +
  84 + Array.to_list nodes,
  85 + Array.fold_left (fun map item -> BatPMap.add (item # page # key) item map) BatPMap.empty nodes
  86 +
  87 +let full_list ?(url=no_url) (t,_) =
  88 + List.map (#page |- with_url url) t
  89 +
  90 +let list ?(url=no_url) key (t,m) =
  91 + if BatPMap.mem key m then
  92 + Some (List.map (#page |- selected url key) t)
  93 + else
  94 + None
  95 +
  96 +let prev_next ?(url=no_url) key (_,m) =
  97 + try
  98 + let node = BatPMap.find key m in
  99 + Some (object
  100 + method prev = BatOption.map (with_url url) (node # prev)
  101 + method next = BatOption.map (with_url url) (node # next)
  102 + end)
  103 + with Not_found ->
  104 + None

0 comments on commit 870ef0c

Please sign in to comment.
Something went wrong with that request. Please try again.