text stringlengths 0 601k |
|---|
let todo_of_json json = todo_of_json ( Ezjsonm . value json ) |
let json_of_todo { title ; url ; completed ; order ; _ } : Ezjsonm . t = let open Ezjsonm in dict [ " title " , ( string title ) ; " url " , ( string url ) ; " completed " , ( bool completed ) ; " order " , ( int order ) ] |
let json_of_todos ( todos : todo list ) : Ezjsonm . t = ` A ( List . map todos ~ f ( : fun json -> Ezjsonm . value ( json_of_todo json ) ) ) |
let get_todos = get " / todos " begin fun _ -> let todos = TodoStorage . fold ( fun _ value acc -> value :: acc ) ! stored_todos [ ] ; in ` Json ( json_of_todos todos ) |> respond ' end |
let get_todo = get " / todos /: id " begin fun req -> let todo = TodoStorage . find ( int_of_string ( param req " id " ) ) ! stored_todos ; in ` Json ( json_of_todo todo ) |> respond ' end |
let patch_todo = App . patch " / todos /: id " begin fun request -> App . json_of_body_exn request >>| fun json -> let todo = TodoStorage . find ( int_of_string ( param request " id " ) ) ! stored_todos in let updated = updated_todo todo ( Ezjsonm . value json ) in stored_todos := TodoStorage . add todo . id updated ! ... |
let delete_todo = App . delete " / todos /: id " begin fun request -> stored_todos := TodoStorage . remove ( int_of_string ( param request " id " ) ) ! stored_todos ; respond ' ( ` String " OK " ) end |
let post_todos = post " / todos " begin fun request -> App . json_of_body_exn request >>| fun json -> let id = ( Random . int 100000 ) in let todo = todo_of_json json id in stored_todos := TodoStorage . add id todo ! stored_todos ; ` Json ( json_of_todo todo ) |> respond end |
let delete_todos = delete " / todos " begin fun _ -> stored_todos := TodoStorage . empty ; respond ' ( ` String " OK " ) end |
let accept_options = App . options " " ** begin fun _ -> respond ' ( ` String " OK " ) end |
let _ = App . empty |> middleware allow_cors |> accept_options |> get_todos |> get_todo |> patch_todo |> delete_todo |> delete_todos |> post_todos |> App . run_command |
let ( . ) <> f g x = f ( g x ) |
type action = | Add | Set of int * TodoList . status | Clear | UpdatePending of string |
let string_of_action = function | Add -> " Add " | Set ( i , TodoList . TODO ) -> sprintf " Set ( % d , TODO ) " i | Set ( i , TodoList . DONE ) -> sprintf " Set ( % d , DONE ) " i | Clear -> " Clear " | UpdatePending s -> sprintf " UpdatePending % S " s |
let render_item idx { TodoList . label = l ; status } = let id = sprintf " checkbox % d " idx in let open Html in let open TodoList in let checkbox ~ id ~ onchange ~ state = let attrs = [ A . type_ " checkbox " ; A . id id ; E . onchange ( fun ~ value -> onchange ) ] in let attrs = if state then A . checked true :: att... |
let render_items = let open Html in function | [ ] -> em ( text " Items will appear here " ) | items -> ul ~ attrs [ : A . class_ " no - bullet " ] begin of_list ( List . mapi ( fun i -> li . <> render_item i ) items ) end |
let render { TodoList . items ; pending_item } = let open Html in let div ~ classes html = div ~ attrs [ : A . class_ ( String . concat " " classes ) ] html in let text_input ~ classes ~ onenter ~ oninput ~ placeholder value = input ~ attrs [ : A . class_ ( String . concat " " classes ) ; A . type_ " text " ; A . value... |
let update = let open TodoList in function | Add -> add_pending_item | Set ( idx , status ) -> set idx status | Clear -> clear | UpdatePending text -> update_pending text |
module ActionFilter = struct type t = action let relevant = function | UpdatePending _ -> false | _ -> true end |
module Storage = struct let storage = match Js_browser . Window . local_storage Js_browser . window with | None -> failwith " Storage is not supported by this browser " | Some v -> v let key = " ocaml - vdom - todo - state " let find ( ) = Js_browser . Storage . get_item storage key let set v = Js_browser . Storage . s... |
module Model = struct type visibility = Completed | Active | All [ @@ deriving json ] type task = { description : string ; backup : string ; completed : bool ; editing : bool ; id : int ; } [ @@ deriving json ] type t = { tasks : task list ; field : string ; uid : int ; visibility : visibility ; } [ @@ deriving json ] ... |
module Action = struct type action = | Update_field of string | Editing_task of ( int * bool ) | Update_task of ( int * string ) | Add | Delete of int | Delete_complete | Check of ( int * bool ) | Check_all of bool | Change_visibility of Model . visibility | Escape of int | Nop end |
module Controller = struct let update m a = let open Action in let open Model in let m = match a with | Add -> let uid = m . uid + 1 in let tasks = let v = String . trim m . field in if v = " " then m . tasks else ( new_task v m . uid ) :: m . tasks in { m with uid = uid ; field = " " ; tasks = tasks } | Update_field f... |
module View = struct open Model open Action let task_input m = Vdom . ( input ~ a [ : class_ " new - todo " ; str_prop " placeholder " " What needs to be done " ? ; autofocus ; value m . field ; oninput ( fun s -> Update_field s ) ; onkeydown ( fun e -> match e . which with | 13 -> Add | _ -> Nop ) ] [ ] ) let task_ent... |
let app = let m = Model . from_json @@ Storage . init @@ Model . to_json Model . empty in let m = match Js_browser . Location . get_hash ( ) with | " " -> m | hash -> match hash with | " " #/ -> { m with Model . visibility = Model . All } | " #/ active " -> { m with Model . visibility = Model . Active } | " #/ complete... |
let run ( ) = Vdom_blit . run app |> Vdom_blit . dom |> Js_browser . Element . append_child ( Js_browser . Document . body Js_browser . document ) |
let ( ) = Js_browser . Window . set_onload Js_browser . window run |
module type S = sig type todo = { id : int ; content : string ; } type error = | Database_error val migrate : unit -> ( unit , error ) result Lwt . t val rollback : unit -> ( unit , error ) result Lwt . t val get_all : unit -> ( todo list , error ) result Lwt . t val add : string -> ( unit , error ) result Lwt . t val ... |
module Make ( Db : module type of Db ) = struct let pool = Db . create_pool ( ) type todo = { id : int ; content : string ; } type error = | Database_error module Q = struct let create_todos_table = Caqti_request . exec Caqti_type . unit { | CREATE TABLE todos ( id SERIAL NOT NULL PRIMARY KEY , content VARCHAR ) } | le... |
type rounding_mode = FE_DOWNWARD | FE_TONEAREST | FE_TOWARDZERO | FE_UPWARD |
let hex_of_float f = format_float " % a " f |
let test1 ( mant : int64 ) ( exp : int ) = let expected = ldexp ( Int64 . to_float mant ) exp in let actual = Z . to_float ( Z . shift_left ( Z . of_int64 mant ) exp ) in if actual = expected then true else begin printf " % Ld * 2 ^% d : expected % s , got % s \ n " mant exp ( hex_of_float expected ) ( hex_of_float act... |
let rnd64 ( ) = let m1 = Random . bits ( ) in let m2 = Random . bits ( ) in let m3 = Random . bits ( ) in Int64 . ( logor ( of_int m1 ) ( logor ( shift_left ( of_int m2 ) 30 ) ( shift_left ( of_int m3 ) 60 ) ) ) |
let testN numrounds = printf " ( % d tests ) . . . " %! numrounds ; let errors = ref 0 in for i = 1 to numrounds do let m = Random . int64 Int64 . max_int in if not ( test1 m 0 ) then incr errors ; if not ( test1 ( Int64 . neg m ) 0 ) then incr errors done ; for i = 1 to numrounds do let m = rnd64 ( ) in let exp = Rand... |
let testQ1 ( mant1 : int64 ) ( exp1 : int ) ( mant2 : int64 ) ( exp2 : int ) = let expected = ldexp ( Int64 . to_float mant1 ) exp1 . / ldexp ( Int64 . to_float mant2 ) exp2 in let actual = Q . to_float ( Q . make ( Z . shift_left ( Z . of_int64 mant1 ) exp1 ) ( Z . shift_left ( Z . of_int64 mant2 ) exp2 ) ) in if comp... |
let testQN numrounds = printf " ( % d tests ) . . . " %! numrounds ; let errors = ref 0 in if not ( testQ1 0L 0 1L 0 ) then incr errors ; if not ( testQ1 1L 0 0L 0 ) then incr errors ; if not ( testQ1 ( - 1L ) 0 0L 0 ) then incr errors ; if not ( testQ1 0L 0 0L 0 ) then incr errors ; for i = 1 to numrounds do let m1 = ... |
let _ = let numrounds = if Array . length Sys . argv >= 2 then int_of_string Sys . argv . ( 1 ) else 100_000 in printf " Default rounding mode ( Z ) " ; testN numrounds ; printf " Default rounding mode ( Q ) " ; testQN numrounds ; if setround FE_TOWARDZERO then begin printf " Round toward zero ( Z ) " ; testN numrounds... |
let may x name f = match x with None -> [ ] | Some a -> [ TkToken name ; TkToken ( f a ) ] |
let cbool x = if x then " 1 " else " 0 " |
let id x = x |
let togl_options_optionals f = fun ? accum ? accumalphasize ? accumbluesize ? accumgreensize ? accumredsize ? alpha ? alphasize ? auxbuffers ? bluesize ? depth ? depthsize ? double ? greensize ? height ? overlay ? privatecmap ? redsize ? rgba ? stencil ? stencilsize ? stereo ? width -> f ( may accum " - accum " cbool |
type font = [ ` Fixed_8x13 | ` Fixed_9x15 | ` Times_10 | ` Times_24 | ` Helvetica_10 | ` Helvetica_12 | ` Helvetica_18 | ` Xfont of string ] = " ml_Togl_LoadBitmapFont " = " ml_Togl_UnloadBitmapFont " = " ml_Togl_GetOverlayTransparentValue " = " ml_Togl_DumpToEpsFile " |
type widget = w Widget . widget |
let togl_table = Hashtbl . create 7 |
let wrap f ( w : widget ) = let togl = try Hashtbl . find togl_table w with Not_found -> raise ( TkError " Unreferenced togl widget " ) in f togl |
let render = wrap _post_redisplay |
let swap_buffers = wrap _swap_buffers |
let height = wrap _height |
let width = wrap _width |
let load_bitmap_font = wrap _load_bitmap_font |
let unload_bitmap_font = wrap _unload_bitmap_font |
let use_layer = wrap _use_layer |
let show_overlay = wrap _show_overlay |
let hide_overlay = wrap _hide_overlay |
let overlay_redisplay = wrap _post_overlay_redisplay |
let exists_overlay = wrap _exists_overlay |
let get_overlay_transparent_value = wrap _get_overlay_transparent_value |
let make_current togl = ignore ( tkEval [ | TkToken ( Widget . name togl ) ; TkToken " makecurrent " ] ) | |
let null_func _ = ( ) |
let display_table = Hashtbl . create 7 |
let cb_of_togl table togl = try let key = _ident togl in let cb = Hashtbl . find table key in ignore ( tkEval [ | TkToken key ; TkToken " makecurrent " ] ) ; | cb ( ) with Not_found -> ( ) |
let callback_table = [ | null_func ; cb_of_togl display_table ; cb_of_togl reshape_table ; null_func ; null_func ; cb_of_togl overlay_table ; null_func ] | |
let ( ) = Callback . register " togl_callbacks " callback_table ; Callback . register " togl_prerr " ( fun msg -> prerr_string msg ; flush stderr ) |
let callback_func table ( w : widget ) ~ cb = let key = Widget . name w in ( try Hashtbl . remove table key with Not_found -> ( ) ) ; Hashtbl . add table key cb |
let display_func = callback_func display_table |
let reshape_func w ~ cb = make_current w ; cb ( ) ; callback_func reshape_table w ~ cb |
let overlay_display_func = callback_func overlay_table |
let dump_to_eps_file ~ filename ( ? rgba = false ) ? render togl = let render = match render with Some f -> f | None -> in callback_table . ( render_id ) <- ( fun _ -> render ( ) ) ; _dump_to_eps_file togl filename rgba |
let dump_to_eps_file ~ filename ? rgba ? render = wrap ( dump_to_eps_file ~ filename ? rgba ? render ) |
let rec timer_func ~ ms ~ cb = ignore ( Timer . add ~ ms ~ callback ( : fun ( ) -> cb ( ) ; timer_func ~ ms ~ cb ) ) |
let configure ? height ? width w = let options = may height " - height " cint @ may width " - width " cint in tkEval [ | TkToken ( Widget . name w ) ; TkTokenList options ] | |
let ready = ref false |
let init_togl ( ) = init ( ) ; _create_func ( ) ; _display_func ( ) ; _reshape_func ( ) ; _overlay_display_func ( ) ; _destroy_func ( ) ; ready := true |
let create ? name = togl_options_optionals ( fun options parent -> prerr_endline " Before init " ; if not ! ready then init_togl ( ) ; prerr_endline " After init " ; let w : widget = let togl = ref None in callback_table . ( create_id ) <- callback_table . ( destroy_id ) <- ( fun t -> prerr_endline " Before create " ; ... |
module Make ( Loc : Sig . Loc ) module Loc = Loc ; open Sig ; type t = camlp4_token ; type token = t ; value to_string = fun [ KEYWORD s -> sprintf " KEYWORD % S " s | SYMBOL s -> sprintf " SYMBOL % S " s | LIDENT s -> sprintf " LIDENT % S " s | UIDENT s -> sprintf " UIDENT % S " s | INT _ s -> sprintf " INT % s " s | ... |
module Eval = struct value valch x = Char . code x - Char . code ' 0 ' ; value valch_hex x = let d = Char . code x in if d >= 97 then d - 87 else if d >= 65 then d - 55 else d - 48 ; value rec skip_indent = parser [ [ : ` ' ' | ' \ t ' ; s ] : -> skip_indent s | [ : ] : -> ( ) ] ; value skip_opt_linefeed = parser [ [ :... |
let niter = ref 0 |
let token = ref 0 |
let process ( n , ins , outs , nprocs ) = let buf = String . make 1 ' . ' in while buf <> " " - do Unix . read ins . ( n ) buf 0 1 ; if n = 0 then begin decr niter ; if ! niter <= 0 then buf . [ 0 ] <- ' ' ; - end ; let next = if n + 1 >= nprocs then 0 else n + 1 in Unix . write outs . ( next ) buf 0 1 done |
let main ( ) = let nprocs = try int_of_string Sys . argv . ( 1 ) with _ -> 100 in let iter = try int_of_string Sys . argv . ( 2 ) with _ -> 1000 in let ins = Array . make nprocs Unix . stdin in let outs = Array . make nprocs Unix . stdout in let threads = Array . make nprocs ( Thread . self ( ) ) in for n = 0 to nprocs... |
type domain = [ ` Absolute of string list | ` Sub of string list ] |
type name = { domain : domain ; name : string } |
let pp_print_domains : domain Formatf . t = fun fmt -> function | ` Absolute ( _a0 : string list ) -> Format . fprintf fmt " . % s " ( String . concat " . " _a0 ) | ` Sub _a0 -> Format . fprintf fmt " % s " ( String . concat " . " _a0 ) |
let pp_print_name fmt ( x : name ) = Format . fprintf fmt " % a . % a " pp_print_domains x . domain Format . pp_print_string x . name |
let string_of_name = Formatf . to_string pp_print_name |
type quot = { loc : loc ; txt : string ; name : name ; meta : string option ; shift : int ; retract : int ; } |
type txt = { loc : loc ; txt : string ; } |
type op = { loc : loc ; txt : string ; level : int ; } |
type ant = { loc : loc ; txt : string ; cxt : string option ; kind : string ; shift : int ; retract : int ; } |
let ant_expand p ( x : ant ) = let content = String . sub x . txt x . shift ( String . length x . txt - x . retract - x . shift ) in let loc = Location_util . join { x . loc with loc_start = { x . loc . loc_start with pos_cnum = x . loc . loc_start . pos_cnum + x . shift } } in p loc content |
let mk_ant ? c ( x : ant ) = match c with | None -> ` Ant ( x . loc , x ) | Some _ -> ` Ant ( x . loc , { x with cxt = c } ) |
let pp_print_ant fmt ( x : ant ) = Format . fprintf fmt " cxt :% S ; content :% S " ( match x . cxt with None " " ->| Some s -> s ) x . txt |
let pp_print_quot : Format . formatter -> quot -> unit = fun fmt { name ; meta ; shift ; txt ; loc ; retract } -> Format . fprintf fmt " [ @< 1 { > name =% a ; ; @ loc =% a ; @ meta =% a ; ; @ shift =% a ; @ retract =% a ; ; @ txt =% a } ] " @ pp_print_name name ( Formatf . pp_print_option Formatf . pp_print_string ) m... |
type ' a expand_fun = Locf . t -> string option -> string -> ' a |
type quotation = [ ` Quot of quot ] |
type dir_quotation = [ ` DirQuotation of quot ] |
type line = { loc : loc ; txt : string ; line : int ; name : string option ; } |
type space_token = [ ` Comment of txt | ` Blank of txt | ` Newline of txt | ` LINE_DIRECTIVE of line ] |
type t = [ ` Key of txt | ` Lid of txt | ` Uid of txt | ` Int of txt | ` Int32 of txt | ` Int64 of txt | ` Nativeint of txt | ` Flo of txt | ` Chr of txt | ` Label of txt | ` Optlabel of txt | ` Str of txt | ` EOI of txt | ` Pre of txt | ` Inf of op | ` Quot of quot | ` DirQuotation of quot | ` Ant of ant ] |
type tag = [ ` Key | ` Lid | ` Uid | ` Int | ` Int32 | ` Int64 | ` Nativeint | ` Flo | ` Chr | ` Label | ` Optlabel | ` Str | ` EOI | ` Pre | ` Inf | ` Quot | ` DirQuotation | ` Ant ] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.