-
-
Save dbuenzli/f98b020f634a6853732a to your computer and use it in GitHub Desktop.
List of strings combinators
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
| (*--------------------------------------------------------------------------- | |
| Copyright (c) 2015 Daniel C. Bünzli. All rights reserved. | |
| Distributed under the BSD3 license, see license at the end of the file. | |
| %%NAME%% release %%VERSION%% | |
| ---------------------------------------------------------------------------*) | |
| module L : sig | |
| type t | |
| val empty : t | |
| val s : string -> t | |
| val ( % ) : t -> string -> t | |
| val on : bool -> t -> t | |
| val cond : bool -> t -> t -> t | |
| val ( %% ) : t -> t -> t | |
| val line : t -> string | |
| end = struct | |
| type t = A of string | S of t list | |
| let empty = S [] | |
| let s a = A a | |
| let ( % ) l a = match l with | |
| | A _ -> S [A a; l] | |
| | S s -> S (A a :: s) | |
| let ( %% ) l0 l1 = match l0 with | |
| | S s0 -> S (l1 :: s0) | |
| | A _ as a0 -> S [l1; a0] | |
| let on bool l = if bool then l else S [] | |
| let cond bool l l' = if bool then l else l' | |
| let flatten line = | |
| let rec loop acc current todo = match acc, current, todo with | |
| | acc, A a :: l, todo -> loop (a :: acc) l todo | |
| | acc, S s :: l, todo -> loop acc s (l :: todo) | |
| | acc, [], current :: todo -> loop acc current todo | |
| | acc, [], [] -> acc | |
| in | |
| loop [] [line] [] | |
| let line line = String.concat " " (flatten line) | |
| end | |
| let () = | |
| let l = L.(s "ls" % "-al" % "/") in | |
| print_endline (L.line l) | |
| let () = | |
| let profile = true in | |
| let profile = L.(on profile @@ s "-p") in | |
| let other = true in | |
| let other = L.(on other @@ s "-a" %% profile) in | |
| let l = L.(s "ls" % "-l" %% (on true @@ s "hey" % "true") %% other % "ho") in | |
| print_endline (L.line l) | |
| (*--------------------------------------------------------------------------- | |
| Copyright (c) 2015 Daniel C. Bünzli. | |
| All rights reserved. | |
| Redistribution and use in source and binary forms, with or without | |
| modification, are permitted provided that the following conditions | |
| are met: | |
| 1. Redistributions of source code must retain the above copyright | |
| notice, this list of conditions and the following disclaimer. | |
| 2. Redistributions in binary form must reproduce the above | |
| copyright notice, this list of conditions and the following | |
| disclaimer in the documentation and/or other materials provided | |
| with the distribution. | |
| 3. Neither the name of Daniel C. Bünzli nor the names of | |
| contributors may be used to endorse or promote products derived | |
| from this software without specific prior written permission. | |
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
| OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
| LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
| DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
| THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| ---------------------------------------------------------------------------*) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment