pax_global_header00006660000000000000000000000064134106661330014515gustar00rootroot0000000000000052 comment=dc3a9d7803aabd853656a14bcd88dc346f403d16 reactiveData-0.2.2/000077500000000000000000000000001341066613300141125ustar00rootroot00000000000000reactiveData-0.2.2/.gitignore000066400000000000000000000000741341066613300161030ustar00rootroot00000000000000_build *~ \#*# .#* reactiveData.install api.docdir doc/html reactiveData-0.2.2/.merlin000066400000000000000000000000701341066613300153760ustar00rootroot00000000000000S src/* B _build/* PKG react FLG -warn A-4-40-42-44-48 reactiveData-0.2.2/CHANGES000066400000000000000000000010531341066613300151040ustar00rootroot00000000000000===== 0.2.2 ===== * additional functions in RList and RMap * do not use a generic hashtable as it does not work with functional values ===== 0.2.1 ===== * Add ?eq parameter to S.signal ===== 0.2 ===== * Add `from_signal`, which converts React signals to ReactiveData containers. `from_signal` uses a diffing algorithm to detect what changes, thus minimizing the updates needed downstream. * Optimize common cases of `merge`. * Provide documentation and make the naming more consistent. ===== 0.1 ====== * First public release reactiveData-0.2.2/LICENSE000066400000000000000000000654371341066613300151360ustar00rootroot00000000000000This program is released under the LGPL version 2.1 (see the text below) with the additional exemption that compiling, linking, and/or using OpenSSL is allowed. As a special exception to the GNU Library General Public License, you may also link, statically or dynamically, a "work that uses the Library" with a publicly distributed version of the Library to produce an executable file containing portions of the Library, and distribute that executable file under terms of your choice, without any of the additional requirements listed in clause 6 of the GNU Library General Public License. By "a publicly distributed version of the Library", we mean either the unmodified Library, or a modified version of the Library that is distributed under the conditions defined in clause 3 of the GNU Library General Public License. This exception does not however invalidate any other reasons why the executable file might be covered by the GNU Library General Public License. GNU LESSER GENERAL PUBLIC LICENSE Version 2.1, February 1999 Copyright (C) 1991, 1999 Free Software Foundation, Inc. 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. [This is the first released version of the Lesser GPL. It also counts as the successor of the GNU Library Public License, version 2, hence the version number 2.1.] Preamble The licenses for most software are designed to take away your freedom to share and change it. By contrast, the GNU General Public Licenses are intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. This license, the Lesser General Public License, applies to some specially designated software packages--typically libraries--of the Free Software Foundation and other authors who decide to use it. You can use it too, but we suggest you first think carefully about whether this license or the ordinary General Public License is the better strategy to use in any particular case, based on the explanations below. When we speak of free software, we are referring to freedom of use, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for this service if you wish); that you receive source code or can get it if you want it; that you can change the software and use pieces of it in new free programs; and that you are informed that you can do these things. To protect your rights, we need to make restrictions that forbid distributors to deny you these rights or to ask you to surrender these rights. These restrictions translate to certain responsibilities for you if you distribute copies of the library or if you modify it. For example, if you distribute copies of the library, whether gratis or for a fee, you must give the recipients all the rights that we gave you. You must make sure that they, too, receive or can get the source code. If you link other code with the library, you must provide complete object files to the recipients, so that they can relink them with the library after making changes to the library and recompiling it. And you must show them these terms so they know their rights. We protect your rights with a two-step method: (1) we copyright the library, and (2) we offer you this license, which gives you legal permission to copy, distribute and/or modify the library. To protect each distributor, we want to make it very clear that there is no warranty for the free library. Also, if the library is modified by someone else and passed on, the recipients should know that what they have is not the original version, so that the original author's reputation will not be affected by problems that might be introduced by others. Finally, software patents pose a constant threat to the existence of any free program. We wish to make sure that a company cannot effectively restrict the users of a free program by obtaining a restrictive license from a patent holder. Therefore, we insist that any patent license obtained for a version of the library must be consistent with the full freedom of use specified in this license. Most GNU software, including some libraries, is covered by the ordinary GNU General Public License. This license, the GNU Lesser General Public License, applies to certain designated libraries, and is quite different from the ordinary General Public License. We use this license for certain libraries in order to permit linking those libraries into non-free programs. When a program is linked with a library, whether statically or using a shared library, the combination of the two is legally speaking a combined work, a derivative of the original library. The ordinary General Public License therefore permits such linking only if the entire combination fits its criteria of freedom. The Lesser General Public License permits more lax criteria for linking other code with the library. We call this license the "Lesser" General Public License because it does Less to protect the user's freedom than the ordinary General Public License. It also provides other free software developers Less of an advantage over competing non-free programs. These disadvantages are the reason we use the ordinary General Public License for many libraries. However, the Lesser license provides advantages in certain special circumstances. For example, on rare occasions, there may be a special need to encourage the widest possible use of a certain library, so that it becomes a de-facto standard. To achieve this, non-free programs must be allowed to use the library. A more frequent case is that a free library does the same job as widely used non-free libraries. In this case, there is little to gain by limiting the free library to free software only, so we use the Lesser General Public License. In other cases, permission to use a particular library in non-free programs enables a greater number of people to use a large body of free software. For example, permission to use the GNU C Library in non-free programs enables many more people to use the whole GNU operating system, as well as its variant, the GNU/Linux operating system. Although the Lesser General Public License is Less protective of the users' freedom, it does ensure that the user of a program that is linked with the Library has the freedom and the wherewithal to run that program using a modified version of the Library. The precise terms and conditions for copying, distribution and modification follow. Pay close attention to the difference between a "work based on the library" and a "work that uses the library". The former contains code derived from the library, whereas the latter must be combined with the library in order to run. GNU LESSER GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License Agreement applies to any software library or other program which contains a notice placed by the copyright holder or other authorized party saying it may be distributed under the terms of this Lesser General Public License (also called "this License"). Each licensee is addressed as "you". A "library" means a collection of software functions and/or data prepared so as to be conveniently linked with application programs (which use some of those functions and data) to form executables. The "Library", below, refers to any such software library or work which has been distributed under these terms. A "work based on the Library" means either the Library or any derivative work under copyright law: that is to say, a work containing the Library or a portion of it, either verbatim or with modifications and/or translated straightforwardly into another language. (Hereinafter, translation is included without limitation in the term "modification".) "Source code" for a work means the preferred form of the work for making modifications to it. For a library, complete source code means all the source code for all modules it contains, plus any associated interface definition files, plus the scripts used to control compilation and installation of the library. Activities other than copying, distribution and modification are not covered by this License; they are outside its scope. The act of running a program using the Library is not restricted, and output from such a program is covered only if its contents constitute a work based on the Library (independent of the use of the Library in a tool for writing it). Whether that is true depends on what the Library does and what the program that uses the Library does. 1. You may copy and distribute verbatim copies of the Library's complete source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this License and to the absence of any warranty; and distribute a copy of this License along with the Library. You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. 2. You may modify your copy or copies of the Library or any portion of it, thus forming a work based on the Library, and copy and distribute such modifications or work under the terms of Section 1 above, provided that you also meet all of these conditions: a) The modified work must itself be a software library. b) You must cause the files modified to carry prominent notices stating that you changed the files and the date of any change. c) You must cause the whole of the work to be licensed at no charge to all third parties under the terms of this License. d) If a facility in the modified Library refers to a function or a table of data to be supplied by an application program that uses the facility, other than as an argument passed when the facility is invoked, then you must make a good faith effort to ensure that, in the event an application does not supply such function or table, the facility still operates, and performs whatever part of its purpose remains meaningful. (For example, a function in a library to compute square roots has a purpose that is entirely well-defined independent of the application. Therefore, Subsection 2d requires that any application-supplied function or table used by this function must be optional: if the application does not supply it, the square root function must still compute square roots.) These requirements apply to the modified work as a whole. If identifiable sections of that work are not derived from the Library, and can be reasonably considered independent and separate works in themselves, then this License, and its terms, do not apply to those sections when you distribute them as separate works. But when you distribute the same sections as part of a whole which is a work based on the Library, the distribution of the whole must be on the terms of this License, whose permissions for other licensees extend to the entire whole, and thus to each and every part regardless of who wrote it. Thus, it is not the intent of this section to claim rights or contest your rights to work written entirely by you; rather, the intent is to exercise the right to control the distribution of derivative or collective works based on the Library. In addition, mere aggregation of another work not based on the Library with the Library (or with a work based on the Library) on a volume of a storage or distribution medium does not bring the other work under the scope of this License. 3. You may opt to apply the terms of the ordinary GNU General Public License instead of this License to a given copy of the Library. To do this, you must alter all the notices that refer to this License, so that they refer to the ordinary GNU General Public License, version 2, instead of to this License. (If a newer version than version 2 of the ordinary GNU General Public License has appeared, then you can specify that version instead if you wish.) Do not make any other change in these notices. Once this change is made in a given copy, it is irreversible for that copy, so the ordinary GNU General Public License applies to all subsequent copies and derivative works made from that copy. This option is useful when you wish to copy part of the code of the Library into a program that is not a library. 4. You may copy and distribute the Library (or a portion or derivative of it, under Section 2) in object code or executable form under the terms of Sections 1 and 2 above provided that you accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange. If distribution of object code is made by offering access to copy from a designated place, then offering equivalent access to copy the source code from the same place satisfies the requirement to distribute the source code, even though third parties are not compelled to copy the source along with the object code. 5. A program that contains no derivative of any portion of the Library, but is designed to work with the Library by being compiled or linked with it, is called a "work that uses the Library". Such a work, in isolation, is not a derivative work of the Library, and therefore falls outside the scope of this License. However, linking a "work that uses the Library" with the Library creates an executable that is a derivative of the Library (because it contains portions of the Library), rather than a "work that uses the library". The executable is therefore covered by this License. Section 6 states terms for distribution of such executables. When a "work that uses the Library" uses material from a header file that is part of the Library, the object code for the work may be a derivative work of the Library even though the source code is not. Whether this is true is especially significant if the work can be linked without the Library, or if the work is itself a library. The threshold for this to be true is not precisely defined by law. If such an object file uses only numerical parameters, data structure layouts and accessors, and small macros and small inline functions (ten lines or less in length), then the use of the object file is unrestricted, regardless of whether it is legally a derivative work. (Executables containing this object code plus portions of the Library will still fall under Section 6.) Otherwise, if the work is a derivative of the Library, you may distribute the object code for the work under the terms of Section 6. Any executables containing that work also fall under Section 6, whether or not they are linked directly with the Library itself. 6. As an exception to the Sections above, you may also combine or link a "work that uses the Library" with the Library to produce a work containing portions of the Library, and distribute that work under terms of your choice, provided that the terms permit modification of the work for the customer's own use and reverse engineering for debugging such modifications. You must give prominent notice with each copy of the work that the Library is used in it and that the Library and its use are covered by this License. You must supply a copy of this License. If the work during execution displays copyright notices, you must include the copyright notice for the Library among them, as well as a reference directing the user to the copy of this License. Also, you must do one of these things: a) Accompany the work with the complete corresponding machine-readable source code for the Library including whatever changes were used in the work (which must be distributed under Sections 1 and 2 above); and, if the work is an executable linked with the Library, with the complete machine-readable "work that uses the Library", as object code and/or source code, so that the user can modify the Library and then relink to produce a modified executable containing the modified Library. (It is understood that the user who changes the contents of definitions files in the Library will not necessarily be able to recompile the application to use the modified definitions.) b) Use a suitable shared library mechanism for linking with the Library. A suitable mechanism is one that (1) uses at run time a copy of the library already present on the user's computer system, rather than copying library functions into the executable, and (2) will operate properly with a modified version of the library, if the user installs one, as long as the modified version is interface-compatible with the version that the work was made with. c) Accompany the work with a written offer, valid for at least three years, to give the same user the materials specified in Subsection 6a, above, for a charge no more than the cost of performing this distribution. d) If distribution of the work is made by offering access to copy from a designated place, offer equivalent access to copy the above specified materials from the same place. e) Verify that the user has already received a copy of these materials or that you have already sent this user a copy. For an executable, the required form of the "work that uses the Library" must include any data and utility programs needed for reproducing the executable from it. However, as a special exception, the materials to be distributed need not include anything that is normally distributed (in either source or binary form) with the major components (compiler, kernel, and so on) of the operating system on which the executable runs, unless that component itself accompanies the executable. It may happen that this requirement contradicts the license restrictions of other proprietary libraries that do not normally accompany the operating system. Such a contradiction means you cannot use both them and the Library together in an executable that you distribute. 7. You may place library facilities that are a work based on the Library side-by-side in a single library together with other library facilities not covered by this License, and distribute such a combined library, provided that the separate distribution of the work based on the Library and of the other library facilities is otherwise permitted, and provided that you do these two things: a) Accompany the combined library with a copy of the same work based on the Library, uncombined with any other library facilities. This must be distributed under the terms of the Sections above. b) Give prominent notice with the combined library of the fact that part of it is a work based on the Library, and explaining where to find the accompanying uncombined form of the same work. 8. You may not copy, modify, sublicense, link with, or distribute the Library except as expressly provided under this License. Any attempt otherwise to copy, modify, sublicense, link with, or distribute the Library is void, and will automatically terminate your rights under this License. However, parties who have received copies, or rights, from you under this License will not have their licenses terminated so long as such parties remain in full compliance. 9. You are not required to accept this License, since you have not signed it. However, nothing else grants you permission to modify or distribute the Library or its derivative works. These actions are prohibited by law if you do not accept this License. Therefore, by modifying or distributing the Library (or any work based on the Library), you indicate your acceptance of this License to do so, and all its terms and conditions for copying, distributing or modifying the Library or works based on it. 10. Each time you redistribute the Library (or any work based on the Library), the recipient automatically receives a license from the original licensor to copy, distribute, link with or modify the Library subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. You are not responsible for enforcing compliance by third parties with this License. 11. If, as a consequence of a court judgment or allegation of patent infringement or for any other reason (not limited to patent issues), conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot distribute so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not distribute the Library at all. For example, if a patent license would not permit royalty-free redistribution of the Library by all those who receive copies directly or indirectly through you, then the only way you could satisfy both it and this License would be to refrain entirely from distribution of the Library. If any portion of this section is held invalid or unenforceable under any particular circumstance, the balance of the section is intended to apply, and the section as a whole is intended to apply in other circumstances. It is not the purpose of this section to induce you to infringe any patents or other property right claims or to contest validity of any such claims; this section has the sole purpose of protecting the integrity of the free software distribution system which is implemented by public license practices. Many people have made generous contributions to the wide range of software distributed through that system in reliance on consistent application of that system; it is up to the author/donor to decide if he or she is willing to distribute software through any other system and a licensee cannot impose that choice. This section is intended to make thoroughly clear what is believed to be a consequence of the rest of this License. 12. If the distribution and/or use of the Library is restricted in certain countries either by patents or by copyrighted interfaces, the original copyright holder who places the Library under this License may add an explicit geographical distribution limitation excluding those countries, so that distribution is permitted only in or among countries not thus excluded. In such case, this License incorporates the limitation as if written in the body of this License. 13. The Free Software Foundation may publish revised and/or new versions of the Lesser General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Library specifies a version number of this License which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Library does not specify a license version number, you may choose any version ever published by the Free Software Foundation. 14. If you wish to incorporate parts of the Library into other free programs whose distribution conditions are incompatible with these, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS How to Apply These Terms to Your New Libraries If you develop a new library, and you want it to be of the greatest possible use to the public, we recommend making it free software that everyone can redistribute and change. You can do so by permitting redistribution under these terms (or, alternatively, under the terms of the ordinary General Public License). To apply these terms, attach the following notices to the library. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA Also add information on how to contact you by electronic and paper mail. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the library, if necessary. Here is a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the library `Frob' (a library for tweaking knobs) written by James Random Hacker. , 1 April 1990 Ty Coon, President of Vice That's all there is to it! reactiveData-0.2.2/Makefile000066400000000000000000000023541341066613300155560ustar00rootroot00000000000000.PHONY: test build: ocaml pkg/build.ml native=true native-dynlink=true clean: ocamlbuild -clean doc: build ocamlbuild -use-ocamlfind src/api.docdir/index.html NAME=reactiveData TMP=/tmp TMP_REPO=$(TMP)/$(NAME) $(TMP_REPO)/.git: cd $(TMP) && \ git clone https://github.com/ocsigen/$(NAME).git && \ cd $(NAME) && \ git checkout gh-pages gh-pages: $(TMP_REPO)/.git doc cd $(TMP_REPO) && git checkout gh-pages rm -f $(TMP_REPO)/dev/* cp _build/src/api.docdir/* $(TMP_REPO)/dev/ cd $(TMP_REPO) && git add dev cd $(TMP_REPO) && git commit -a -m "Doc updates" cd $(TMP_REPO) && git push origin gh-pages release: $(TMP_REPO)/.git @if [ -z "$(VERSION)" ]; then echo "Usage: make release VERSION=1.0.0"; exit 1; fi cd $(TMP_REPO) && cp -r dev $(VERSION) cd $(TMP_REPO) && git add $(VERSION) cd $(TMP_REPO) && git commit -m "Doc updates $(VERSION)" cd $(TMP_REPO) && git push origin gh-pages sed -i "s/= dev =/= $(VERSION) =/" CHANGES git add CHANGES git commit -m "Version $(VERSION)." git tag -a $(VERSION) -m "Version $(VERSION)." prepare: @if [ -z "$(VERSION)" ]; then echo "Usage: make prepare VERSION=1.0.0"; exit 1; fi opam publish prepare "$(NAME).$(VERSION)" \ "https://github.com/ocsigen/$(NAME)/archive/$(VERSION).tar.gz" reactiveData-0.2.2/README.md000066400000000000000000000004521341066613300153720ustar00rootroot00000000000000**Reactive Data** *Functional reactive programming with incremental changes in data structures* ReactiveData is an OCaml module for functional reactive programming (FRP) based on React. It adds support to incremental changes in data structures by reasoning on patches instead of absolute values. reactiveData-0.2.2/RELEASE.md000066400000000000000000000004271341066613300155170ustar00rootroot00000000000000* To update dev documentation ``` make gh-pages ``` * To release ``` make release VERSION=1.0.0 git push && git push --tags make prepare VERSION=1.0.0 ``` Then check that everything is fine and follow the instructions. Don't forget to change the doc field inside the opam file. reactiveData-0.2.2/_tags000066400000000000000000000002551341066613300151340ustar00rootroot00000000000000: package(react) true: warn(A-4-40-42-44-48) true: warn_error(+1..49), warn_error(-45-3) true: strict_sequence, safe_string, short_paths true: bin_annot reactiveData-0.2.2/opam000066400000000000000000000020621341066613300147710ustar00rootroot00000000000000opam-version: "2.0" version: "0.2.2" synopsis: "Declarative events and signals for OCaml" description: "React is an OCaml module for functional reactive programming (FRP). It provides support to program with time varying values : declarative events and signals. React doesn't define any primitive event or signal, it lets the client chooses the concrete timeline." maintainer: "Hugo Heuzard " authors: ["Hugo Heuzard "] homepage: "https://github.com/ocsigen/reactiveData" dev-repo: "git+https://github.com/ocsigen/reactiveData.git" bug-reports: "https://github.com/ocsigen/reactiveData/issues" doc:"http://ocsigen.github.io/reactiveData/dev/" tags: [ "reactive" "declarative" "signal" "event" "frp" ] license: "LGPL-3.0 with OCaml linking exception" depends: [ "ocaml" {>= "4.06.1"} "ocamlfind" "react" {>= "1.2.1" < "1.3"} ] build: [ [ "ocaml" "pkg/build.ml" "native=%{ocaml:native}%" "native-dynlink=%{ocaml:native}%" ] ["ocamlbuild" "-use-ocamlfind" "src/api.docdir/index.html"] {with-doc} ] reactiveData-0.2.2/pkg/000077500000000000000000000000001341066613300146735ustar00rootroot00000000000000reactiveData-0.2.2/pkg/META000066400000000000000000000004371341066613300153500ustar00rootroot00000000000000version = "0.2.1" description = "Declarative events and signals for OCaml" requires = "react" archive(byte) = "reactiveData.cma" archive(byte, plugin) = "reactiveData.cma" archive(native) = "reactiveData.cmxa" archive(native, plugin) = "reactiveData.cmxs" exists_if = "reactiveData.cma" reactiveData-0.2.2/pkg/build.ml000066400000000000000000000004001341066613300163160ustar00rootroot00000000000000#!/usr/bin/env ocaml #directory "pkg";; #use "topkg.ml";; let () = Pkg.describe "reactiveData" ~builder:`OCamlbuild [ Pkg.lib "pkg/META"; Pkg.lib ~exts:Exts.module_library "src/reactiveData"; Pkg.doc "README.md"; Pkg.doc "CHANGES"; ] reactiveData-0.2.2/pkg/topkg.ml000066400000000000000000000255321341066613300163600ustar00rootroot00000000000000(*--------------------------------------------------------------------------- Copyright (c) 2014 Daniel C. Bünzli. All rights reserved. Distributed under the BSD3 license, see license at the end of the file. %%NAME%% release %%VERSION%% ---------------------------------------------------------------------------*) (* Public api *) (** Build environment access *) module type Env = sig val bool : string -> bool (** [bool key] declares [key] as being a boolean key in the environment. Specifing key=(true|false) on the command line becomes mandatory. *) val native : bool (** [native] is [bool "native"]. *) val native_dynlink : bool (** [native_dylink] is [bool "native-dynlink"] *) end (** Exts defines sets of file extensions. *) module type Exts = sig val interface : string list (** [interface] is [[".mli"; ".cmi"; ".cmti"]] *) val interface_opt : string list (** [interface_opt] is [".cmx" :: interface] *) val library : string list (** [library] is [[".cma"; ".cmxa"; ".cmxs"; ".a"]] *) val module_library : string list (** [module_library] is [(interface_opt @ library)]. *) end (** Package description. *) module type Pkg = sig type builder = [ `OCamlbuild | `Other of string * string ] (** The type for build tools. Either [`OCamlbuild] or an [`Other (tool, bdir)] tool [tool] that generates its build artefacts in [bdir]. *) type moves (** The type for install moves. *) type field = ?cond:bool -> ?exts:string list -> ?dst:string -> string -> moves (** The type for field install functions. A call [field cond exts dst path] generates install moves as follows: {ul {- If [cond] is [false] (defaults to [true]), no move is generated.} {- If [exts] is present, generates a move for each path in the list [List.map (fun e -> path ^ e) exts].} {- If [dst] is present this path is used as the move destination (allows to install in subdirectories). If absent [dst] is [Filename.basename path].} *) val lib : field val bin : ?auto:bool -> field (** If [auto] is true (defaults to false) generates [path ^ ".native"] if {!Env.native} is [true] and [path ^ ".byte"] if {!Env.native} is [false]. *) val sbin : ?auto:bool -> field (** See {!bin}. *) val toplevel : field val share : field val share_root : field val etc : field val doc : field val misc : field val stublibs : field val man : field val describe : string -> builder:builder -> moves list -> unit (** [describe name builder moves] describes a package named [name] with builder [builder] and install moves [moves]. *) end (* Implementation *) module Topkg : sig val cmd : [`Build | `Explain | `Help ] val env : (string * bool) list val err_parse : string -> 'a val err_mdef : string -> 'a val err_miss : string -> 'a val err_file : string -> string -> 'a val warn_unused : string -> unit end = struct (* Parses the command line. The actual cmd execution occurs in the call to Pkg.describe. *) let err fmt = let k _ = exit 1 in Format.kfprintf k Format.err_formatter ("%s: " ^^ fmt ^^ "@.") Sys.argv.(0) let err_parse a = err "argument `%s' is not of the form key=(true|false)" a let err_mdef a = err "bool `%s' is defined more than once" a let err_miss a = err "argument `%s=(true|false)' is missing" a let err_file f e = err "%s: %s" f e let warn_unused k = Format.eprintf "%s: warning: environment key `%s` unused.@." Sys.argv.(0) k let cmd, env = let rec parse_env acc = function (* not t.r. *) | arg :: args -> begin try (* String.cut ... *) let len = String.length arg in let eq = String.index arg '=' in let bool = bool_of_string (String.sub arg (eq + 1) (len - eq - 1)) in let key = String.sub arg 0 eq in if key = "" then raise Exit else try ignore (List.assoc key acc); err_mdef key with | Not_found -> parse_env ((key, bool) :: acc) args with | Invalid_argument _ | Not_found | Exit -> err_parse arg end | [] -> acc in match List.tl (Array.to_list Sys.argv) with | "explain" :: args -> `Explain, parse_env [] args | ("help" | "-h" | "--help" | "-help") :: args -> `Help, parse_env [] args | args -> `Build, parse_env [] args end module Env : sig include Env val get : unit -> (string * bool) list end = struct let env = ref [] let get () = !env let add_bool key b = env := (key, b) :: !env let bool key = let b = try List.assoc key Topkg.env with | Not_found -> if Topkg.cmd = `Build then Topkg.err_miss key else true in add_bool key b; b let native = bool "native" let native_dynlink = bool "native-dynlink" end module Exts : Exts = struct let interface = [".mli"; ".cmi"; ".cmti"] let interface_opt = ".cmx" :: interface let library = [".cma"; ".cmxa"; ".cmxs"; ".a"] let module_library = (interface_opt @ library) end module Pkg : Pkg = struct type builder = [ `OCamlbuild | `Other of string * string ] type moves = (string * (string * string)) list type field = ?cond:bool -> ?exts:string list -> ?dst:string -> string -> moves let str = Printf.sprintf let warn_unused () = let keys = List.map fst Topkg.env in let keys_used = List.map fst (Env.get ()) in let unused = List.find_all (fun k -> not (List.mem k keys_used)) keys in List.iter Topkg.warn_unused unused let has_suffix = Filename.check_suffix let build_strings ?(exec_sep = " ") btool bdir mvs = let no_build = [ ".cmti"; ".cmt" ] in let install = Buffer.create 1871 in let exec = Buffer.create 1871 in let rec add_mvs current = function | (field, (src, dst)) :: mvs when field = current -> if List.exists (has_suffix src) no_build then Buffer.add_string install (str "\n \"?%s/%s\" {\"%s\"}" bdir src dst) else begin Buffer.add_string exec (str "%s%s" exec_sep src); Buffer.add_string install (str "\n \"%s/%s\" {\"%s\"}" bdir src dst); end; add_mvs current mvs | (((field, _) :: _) as mvs) -> if current <> "" (* first *) then Buffer.add_string install " ]\n"; Buffer.add_string install (str "%s: [" field); add_mvs field mvs | [] -> () in Buffer.add_string exec btool; add_mvs "" mvs; Buffer.add_string install " ]\n"; Buffer.contents install, Buffer.contents exec let pr = Format.printf let pr_explanation btool bdir pkg mvs = let env = Env.get () in let install, exec = build_strings ~exec_sep:" \\\n " btool bdir mvs in pr "@["; pr "Package name: %s@," pkg; pr "Build tool: %s@," btool; pr "Build directory: %s@," bdir; pr "Environment:@, "; List.iter (fun (k,v) -> pr "%s=%b@, " k v) (List.sort compare env); pr "@,Build invocation:@,"; pr " %s@,@," exec; pr "Install file:@,"; pr "%s@," install; pr "@]"; () let pr_help () = pr "Usage example:@\n %s" Sys.argv.(0); List.iter (fun (k,v) -> pr " %s=%b" k v) (List.sort compare (Env.get ())); pr "@." let build btool bdir pkg mvs = let install, exec = build_strings btool bdir mvs in let e = Sys.command exec in if e <> 0 then exit e else let install_file = pkg ^ ".install" in try let oc = open_out install_file in output_string oc install; flush oc; close_out oc with Sys_error e -> Topkg.err_file install_file e let mvs ?(drop_exts = []) field ?(cond = true) ?(exts = []) ?dst src = if not cond then [] else let mv src dst = (field, (src, dst)) in let expand exts s d = List.map (fun e -> mv (s ^ e) (d ^ e)) exts in let dst = match dst with None -> Filename.basename src | Some dst -> dst in let files = if exts = [] then [mv src dst] else expand exts src dst in let keep (_, (src, _)) = not (List.exists (has_suffix src) drop_exts) in List.find_all keep files let lib = let drop_exts = if Env.native && not Env.native_dynlink then [ ".cmxs" ] else if not Env.native then [ ".a"; ".cmx"; ".cmxa"; ".cmxs" ] else [] in mvs ~drop_exts "lib" let share = mvs "share" let share_root = mvs "share_root" let etc = mvs "etc" let toplevel = mvs "toplevel" let doc = mvs "doc" let misc = mvs "misc" let stublibs = mvs "stublibs" let man = mvs "man" let bin_drops = if not Env.native then [ ".native" ] else [] let bin_mvs field ?(auto = false) ?cond ?exts ?dst src = let src, dst = if not auto then src, dst else let dst = match dst with | None -> Some (Filename.basename src) | Some _ as dst -> dst in let src = if Env.native then src ^ ".native" else src ^ ".byte" in src, dst in mvs ~drop_exts:bin_drops field ?cond ?dst src let bin = bin_mvs "bin" let sbin = bin_mvs "sbin" let describe pkg ~builder mvs = let mvs = List.sort compare (List.flatten mvs) in let btool, bdir = match builder with | `OCamlbuild -> "ocamlbuild -use-ocamlfind -classic-display", "_build" | `Other (btool, bdir) -> btool, bdir in match Topkg.cmd with | `Explain -> pr_explanation btool bdir pkg mvs | `Help -> pr_help () | `Build -> warn_unused (); build btool bdir pkg mvs end (*--------------------------------------------------------------------------- Copyright (c) 2014 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. ---------------------------------------------------------------------------*) reactiveData-0.2.2/src/000077500000000000000000000000001341066613300147015ustar00rootroot00000000000000reactiveData-0.2.2/src/api.odocl000066400000000000000000000000141341066613300164670ustar00rootroot00000000000000reactiveDatareactiveData-0.2.2/src/reactiveData.ml000066400000000000000000000556161341066613300176440ustar00rootroot00000000000000(* ReactiveData * https://github.com/hhugo/reactiveData * Copyright (C) 2014 Hugo Heuzard * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, with linking exception; * either version 2.1 of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) module type DATA = sig type 'a data type 'a patch val merge : 'a patch -> 'a data -> 'a data val map_patch : ('a -> 'b) -> 'a patch -> 'b patch val map_data : ('a -> 'b) -> 'a data -> 'b data val empty : 'a data val equal : ('a -> 'a -> bool) -> 'a data -> 'a data -> bool val diff : eq:('a -> 'a -> bool) -> 'a data -> 'a data -> 'a patch end module type S = sig type 'a t type 'a data type 'a patch type 'a msg = Patch of 'a patch | Set of 'a data type 'a handle val empty : 'a t val create : 'a data -> 'a t * 'a handle val from_event : 'a data -> 'a msg React.E.t -> 'a t val from_signal : ?eq:('a -> 'a -> bool) -> 'a data React.S.t -> 'a t val const : 'a data -> 'a t val patch : 'a handle -> 'a patch -> unit val set : 'a handle -> 'a data -> unit val map_msg : ('a -> 'b) -> 'a msg -> 'b msg val map : ('a -> 'b) -> 'a t -> 'b t val value : 'a t -> 'a data val fold : ('a -> 'b msg -> 'a) -> 'b t -> 'a -> 'a React.signal val signal : ?eq:('a -> 'a -> bool) -> 'a t -> 'a data React.S.t val event : 'a t -> 'a msg React.E.t end module Make(D : DATA) : S with type 'a data = 'a D.data and type 'a patch = 'a D.patch = struct type 'a data = 'a D.data type 'a patch = 'a D.patch let merge = D.merge let map_patch = D.map_patch let map_data = D.map_data type 'a msg = | Patch of 'a patch | Set of 'a data type 'a handle = ?step:React.step -> 'a msg -> unit type 'a mut = { current : 'a data ref; event : 'a msg React.E.t; } type 'a t = | Const of 'a data | Mut of 'a mut let empty = Const D.empty let create l = let initial_event,send = React.E.create () in let current = ref l in let event = React.E.map (fun msg -> begin match msg with | Set l -> current := l; | Patch p -> current := merge p !current end; msg) initial_event in Mut {current;event},send let from_event l initial_event = let current = ref l in let event = React.E.map (fun msg -> begin match msg with | Set l -> current := l; | Patch p -> current := merge p !current end; msg) initial_event in Mut {current;event} let const x = Const x let map_msg (f : 'a -> 'b) : 'a msg -> 'b msg = function | Set l -> Set (map_data f l) | Patch p -> Patch (map_patch f p) let map f s = match s with | Const x -> Const (map_data f x) | Mut s -> let current = ref (map_data f !(s.current)) in let event = React.E.map (fun msg -> let msg = map_msg f msg in begin match msg with | Set l -> current := l; | Patch p -> current := merge p !current end; msg) s.event in Mut {current ;event} let value s = match s with | Const c -> c | Mut s -> !(s.current) let event s = match s with | Const _ -> React.E.never | Mut s -> s.event let patch (s : 'a handle) p = s (Patch p) let set (s : 'a handle) p = s (Set p) let fold f s acc = match s with | Const c -> React.S.const (f acc (Set c)) | Mut s -> let acc = f acc (Set (!(s.current))) in React.S.fold f acc s.event let signal ?(eq = (=)) (s : 'a t) : 'a data React.S.t = match s with | Const c -> React.S.const c | Mut s -> React.S.fold ~eq:(D.equal eq) (fun l msg -> match msg with | Set l -> l | Patch p -> merge p l) (!(s.current)) s.event let from_signal ?(eq = (=)) s = let f d' d = Patch (D.diff ~eq d d') in from_event (React.S.value s) (React.S.diff f s) end module DataList = struct type 'a data = 'a list type 'a p = | I of int * 'a | R of int | U of int * 'a | X of int * int type 'a patch = 'a p list let empty = [] let map_data = List.map let map_patch f = function | I (i,x) -> I (i, f x) | R i -> R i | X (i,j) -> X (i,j) | U (i,x) -> U (i,f x) let map_patch f = List.map (map_patch f) let merge_p op l = match op with | I (i',x) -> let i = if i' < 0 then List.length l + 1 + i' else i' in let rec aux acc n l = match n,l with | 0,l -> List.rev_append acc (x::l) | _,[] -> failwith "ReactiveData.Rlist.merge" | n,x::xs -> aux (x::acc) (pred n) xs in aux [] i l | R i' -> let i = if i' < 0 then List.length l + i' else i' in let rec aux acc n l = match n,l with | 0,_::l -> List.rev_append acc l | _,[] -> failwith "ReactiveData.Rlist.merge" | n,x::xs -> aux (x::acc) (pred n) xs in aux [] i l | U (i',x) -> let i = if i' < 0 then List.length l + i' else i' in let a = Array.of_list l in a.(i) <- x; Array.to_list a | X (i',offset) -> let a = Array.of_list l in let len = Array.length a in let i = if i' < 0 then len + i' else i' in let v = a.(i) in if offset > 0 then begin if (i + offset >= len) then failwith "ReactiveData.Rlist.merge"; for j = i to i + offset - 1 do a.(j) <- a.(j + 1) done; a.(i+offset) <- v end else begin if (i + offset < 0) then failwith "ReactiveData.Rlist.merge"; for j = i downto i + offset + 1 do a.(j) <- a.(j - 1) done; a.(i+offset) <- v end; Array.to_list a (* accumulates into acc i unmodified elements from l *) let rec linear_merge_fwd ~acc i l = assert (i >= 0); if i > 0 then match l with | h :: l -> let acc = h :: acc in linear_merge_fwd ~acc (i - 1) l | [] -> invalid_arg "invalid index" else l, acc let rec linear_merge ~acc i0 p l = let l, acc = match p with | (I (i, _) | R i | U (i, _)) :: _ when i > i0 -> linear_merge_fwd ~acc (i - i0) l | _ -> l, acc in match p, l with | I (i, x) :: p, _ -> linear_merge ~acc i p (x :: l) | R i :: p, _ :: l -> linear_merge ~acc i p l | R _ :: _, [] -> invalid_arg "merge: invalid index" | U (i, x) :: p, _ :: l -> linear_merge ~acc i p (x :: l) | U (_, _) :: _, [] -> invalid_arg "merge: invalid index" | [], l -> List.rev_append acc l | X (_, _) :: _, _ -> failwith "linear_merge: X not supported" let rec linear_mergeable ~n p = assert (n >= 0); match p with | (I (i, _) | R i | U (i, _)) :: p when i >= n -> (* negative i's ruled out (among others) *) linear_mergeable ~n:i p | _ :: _ -> false | [] -> true let merge p l = if linear_mergeable ~n:0 p then linear_merge ~acc:[] 0 p l else List.fold_left (fun l x -> merge_p x l) l p let rec equal f l1 l2 = match l1, l2 with | x1 :: l1, x2 :: l2 when f x1 x2 -> equal f l1 l2 | [], [] -> true | _ :: _ , _ :: _ | _ :: _ , [] | [] , _ :: _ -> false let mem (type u) l = let module H = Hashtbl.Make (struct type t = u let hash = Hashtbl.hash let equal = (==) end) in let h = H.create 16 in List.iter (fun x -> H.add h x ()) l; H.mem h let fold_diff ?(eq = (=)) ~acc ~remove ~add lx ly = let memx = mem lx and memy = mem ly in let rec f ~acc ~left lx ly n = match lx, ly with (* trailing elements to be removed *) | _ :: lx, [] -> let acc = remove acc n in f ~acc ~left lx [] n (* trailing elements to be added *) | [], y :: ly -> let acc = add acc n y in f ~acc ~left [] ly (n + 1) (* done! *) | [], [] -> acc (* same *) | x :: lx, y :: ly when eq x y -> f ~acc ~left lx ly (n + 1) (* x needs to be removed for sure *) | x :: lx, _ :: _ when not (memy x) -> let acc = remove acc n in f ~acc ~left lx ly n (* y needs to be added for sure *) | _ :: _, y :: ly when not (memx y) -> let acc = add acc n y in f ~acc ~left lx ly (n + 1) (* no more certainty, ~left decides what to recur on *) | _ :: lx, _ :: _ when left -> let acc = remove acc n in f ~acc ~left:false lx ly n | _ :: _, y :: ly -> let acc = add acc n y in f ~acc ~left:true lx ly (n + 1) in f ~acc ~left:true lx ly 0 let rec list_rev ?(acc = []) = function | h :: t -> let acc = h :: acc in list_rev ~acc t | [] -> acc let diff ~eq x y = let add acc i v = I (i, v) :: acc and remove acc i = R i :: acc and acc = [] in list_rev (fold_diff ~eq ~acc ~add ~remove x y) end module RList = struct include Make (DataList) module D = DataList type 'a p = 'a D.p = | I of int * 'a | R of int | U of int * 'a | X of int * int let cons x s = patch s [D.I (0,x)] let snoc x s = patch s [D.I (-1,x)] let insert x i s = patch s [D.I (i,x)] let update x i s = patch s [D.U (i,x)] let move i j s = patch s [D.X (i,j)] let remove i s = patch s [D.R i] let index ?(eq = (=)) l x = let rec f n = function | hd :: _ when eq hd x -> n | _ :: tl -> f (n + 1) tl | [] -> raise Not_found in f 0 l let update_eq ?eq (data, handle) x y = let i = index ?eq (value data) x in update y i handle let remove_last (data, handle) = remove (List.length (value data) - 1) handle let remove_eq ?eq (data, handle) x = let i = index ?eq (value data) x in remove i handle let singleton x = const [x] let singleton_s s = let first = ref true in let e,send = React.E.create () in let result = from_event [] e in let _ = React.S.map (fun x -> if !first then begin first:=false; send (Patch [I(0,x)]) end else send (Patch [U(0,x)])) s in result let concat : 'a t -> 'a t -> 'a t = fun x y -> let v1 = value x and v2 = value y in let size1 = ref 0 and size2 = ref 0 in let size_with_patch sizex : 'a D.p -> unit = function | (D.I _) -> incr sizex | (D.R _) -> decr sizex | (D.X _ | D.U _) -> () in let size_with_set sizex l = sizex:=List.length l in size_with_set size1 v1; size_with_set size2 v2; let update_patch1 = List.map (fun p -> let m = match p with | D.I (pos,x) -> let i = if pos < 0 then pos - !size2 else pos in D.I (i, x) | D.R pos -> D.R (if pos < 0 then pos - !size2 else pos) | D.U (pos,x) -> D.U ((if pos < 0 then pos - !size2 else pos), x) | D.X (i,j) -> D.X ((if i < 0 then i - !size2 else i),j) in size_with_patch size1 m; m) in let update_patch2 = List.map (fun p -> let m = match p with | D.I (pos,x) -> D.I ((if pos < 0 then pos else !size1 + pos), x) | D.R pos -> D.R (if pos < 0 then pos else !size1 + pos) | D.U (pos,x) -> D.U ((if pos < 0 then pos else !size1 + pos), x) | D.X (i,j) -> D.X ((if i < 0 then i else !size1 + i),j) in size_with_patch size2 m; m) in let tuple_ev = React.E.merge (fun acc x -> match acc,x with | (None,p2),`E1 x -> Some x,p2 | (p1,None),`E2 x -> p1, Some x | _ -> assert false) (None,None) [React.E.map (fun e -> `E1 e) (event x); React.E.map (fun e -> `E2 e) (event y)] in let merged_ev = React.E.map (fun p -> match p with | Some (Set p1), Some (Set p2) -> size_with_set size1 p1; size_with_set size2 p2; Set (p1 @ p2) | Some (Set p1), None -> size_with_set size1 p1; Set (p1 @ value y) | None, Some (Set p2) -> size_with_set size2 p2; Set (value x @ p2 ) | Some (Patch p1), Some (Patch p2) -> let p1 = update_patch1 p1 in let p2 = update_patch2 p2 in Patch (p1 @ p2) | Some (Patch p1), None -> Patch (update_patch1 p1) | None, Some (Patch p2) -> Patch (update_patch2 p2) | Some (Patch _), Some (Set s2) -> let s1 = value x in size_with_set size1 s1; size_with_set size2 s2; Set(s1 @ s2) | Some (Set s1), Some (Patch _) -> size_with_set size1 s1; let s2 = value y in size_with_set size2 s2; Set(s1 @ s2) | None,None -> assert false ) tuple_ev in from_event (v1 @ v2) merged_ev let inverse : 'a . 'a p -> 'a p = function | I (i,x) -> I(-i-1, x) | U (i,x) -> U(-i-1, x) | R i -> R (-i-1) | X (i,j) -> X (-i-1,-j) let rev t = let e = React.E.map (function | Set l -> Set (List.rev l) | Patch p -> Patch (List.map inverse p)) (event t) in from_event (List.rev (value t)) e let filter pred l = let module IntMap = Map.Make( struct let compare = Pervasives.compare type t = int end ) in let index = ref IntMap.empty in let size = ref 0 in let filter_list l = let rec aux (l: 'a list) res their_i my_i = match l with | [] -> res | x::xs -> if pred x then begin index := IntMap.add their_i (my_i + 1) !index; aux xs (x::res) (their_i + 1) (my_i + 1) end else begin aux xs res (their_i + 1) my_i end in size := List.length l; index := IntMap.empty; List.rev (aux l [] 0 (-1)) in let normalise i = if i < 0 then !size + 1 + i else i in let update_index_insert insert_pos_full_list visible = let insert_pos_full_list = normalise insert_pos_full_list in let left_alone, displaced, updatables = IntMap.split insert_pos_full_list !index in let updatables = match displaced with | None -> updatables | Some displaced_in_filtered -> IntMap.add insert_pos_full_list displaced_in_filtered updatables in let update_j j_full_list j_filtered_list = let new_j_filtered = if visible then j_filtered_list + 1 else j_filtered_list in index := IntMap.add (j_full_list + 1) new_j_filtered !index in let () = IntMap.iter update_j updatables in let insert_pos_filtered = if IntMap.is_empty left_alone then 0 else (snd (IntMap.max_binding left_alone)) + 1 in if visible then index := IntMap.add insert_pos_full_list insert_pos_filtered !index; incr size; insert_pos_filtered in let update_index_remove remove_pos_full_list = let was_visible = IntMap.mem remove_pos_full_list !index in let _,_,updatables = IntMap.split remove_pos_full_list !index in let update_j j_full_list j_filtered_list = let new_j = if was_visible then j_filtered_list else j_filtered_list - 1 in index := IntMap.add (j_full_list - 1) new_j !index in if not (IntMap.is_empty !index) then let last_i, _ = IntMap.max_binding !index in index := IntMap.remove last_i !index else (); decr size; IntMap.iter update_j updatables in let update_index_update_delete update_pos_full_list = let _, _, updatables = IntMap.split update_pos_full_list !index in let update_j j_full_list j_filtered_list = index := IntMap.add j_full_list (j_filtered_list - 1) !index in index := IntMap.remove update_pos_full_list !index; IntMap.iter update_j updatables in let update_index_update_insert update_pos_full_list = let left_alone, none, updatables = IntMap.split update_pos_full_list !index in assert (none = None); let update_j j_full_list j_filtered_list = index := IntMap.add j_full_list (j_filtered_list + 1) !index in let new_pos_filtered_list = let previous_pos_filtered = try (snd (IntMap.max_binding left_alone)) with Not_found -> (-1) in previous_pos_filtered + 1 in index := IntMap.add update_pos_full_list new_pos_filtered_list !index; IntMap.iter update_j updatables; new_pos_filtered_list in let update_index_move from_full_list to_full_list to_filtered = let was_visible = match to_filtered with | Some _ -> true | None -> false in let forward = from_full_list < to_full_list in if forward then for i_full = from_full_list + 1 to to_full_list do let delta = if was_visible then (-1) else 0 in try let i_filtered = IntMap.find i_full !index in let new_val = i_filtered + delta in index := IntMap.add (i_full - 1) new_val !index with | Not_found -> () done else for i_full = from_full_list - 1 downto to_full_list do try let delta = if was_visible then 1 else 0 in let i_filtered = IntMap.find i_full !index in let new_val = i_filtered + delta in index := IntMap.add (i_full + 1) new_val !index with | Not_found -> () done; match to_filtered with | Some to_filtered -> index := IntMap.add to_full_list to_filtered !index | None -> index := IntMap.remove to_full_list !index in let convert_p = function | I (i, x) -> if pred x then let my_i = update_index_insert i true in [I (my_i, x)] else begin ignore (update_index_insert i false); [] end | R i -> let i = normalise i in let ret = try let j = IntMap.find i !index in [R j] with | Not_found -> [] in let () = update_index_remove i in ret | U (i, x) -> let i = normalise i in begin try let old_j = IntMap.find i !index in if pred x then [U (old_j, x)] else (update_index_update_delete i; [R old_j]) with | Not_found -> if pred x then let new_j = update_index_update_insert i in [I (new_j, x)] else [] end | X (origin_full, offset_full) -> let origin_full = normalise origin_full in let dest_full = origin_full + offset_full in try let origin_filtered = IntMap.find origin_full !index in let dest_filtered = try IntMap.find dest_full !index with | Not_found -> let small_ones, _, _ = IntMap.split origin_full !index in if IntMap.is_empty small_ones then 0 else snd (IntMap.max_binding small_ones) + 1 in update_index_move origin_full dest_full (Some dest_filtered); if dest_filtered != origin_filtered then [X (origin_filtered, dest_filtered - origin_filtered)] else [] with | Not_found -> (* moving an element that was filtered out *) update_index_move origin_full dest_full None; [] in let filter_e = function | Set l -> Set (filter_list l) | Patch p -> Patch (List.concat (List.map convert_p p)) in let e = React.E.map filter_e (event l) in from_event (filter_list (value l)) e module IntSet = Set.Make (struct type t = int let compare = Pervasives.compare end) let for_all fn data = let maybe_update acc i v = if fn v then acc else IntSet.add i acc in let init = let rec fold i acc = function | v :: tl -> fold (i + 1) (maybe_update acc i v) tl | [] -> acc in fold 0 IntSet.empty in let update_idx_after i f acc = IntSet.map (fun i' -> if i' >= i then f i' 1 else i') acc in let f = fun acc -> function | Set x -> init x | Patch updates -> List.fold_left (fun acc -> function | X (i, i') -> if IntSet.mem i acc = IntSet.mem i' acc then acc else if IntSet.mem i acc then IntSet.add i' (IntSet.remove i acc) else IntSet.add i (IntSet.remove i' acc) | R i -> update_idx_after i (-) (IntSet.remove i acc) | I (i, v) -> let acc = update_idx_after i (+) acc in maybe_update acc i v | U (i, v) -> maybe_update (IntSet.remove i acc) i v) acc updates in React.S.fold f (init (value data)) (event data) |> React.S.map IntSet.is_empty end module RMap(M : Map.S) = struct module Data = struct type 'a data = 'a M.t type 'a p = [`Add of (M.key * 'a) | `Del of M.key] type 'a patch = 'a p list let merge_p p s = match p with | `Add (k,a) -> M.add k a s | `Del k -> M.remove k s let merge p acc = List.fold_left (fun acc p -> merge_p p acc) acc p let map_p f = function | `Add (k,a) -> `Add (k,f a) | `Del k -> `Del k let map_patch f = List.map (map_p f) let map_data f d = M.map f d let empty = M.empty let equal f = M.equal f let diff ~eq x y = let m = let g _key v w = match v, w with | Some v, Some w when eq v w -> None | Some _, Some w -> Some (`U w) | Some _, None -> Some `D | None, Some v -> Some (`A v) | None, None -> None in M.merge g x y and g key x acc = match x with | `U v -> `Del key :: `Add (key, v) :: acc | `D -> `Del key :: acc | `A v -> `Add (key, v) :: acc and acc = [] in List.rev (M.fold g m acc) end include Make(Data) let filter pred m = let convert_p = function | `Add (k,v) -> if pred k v then [`Add (k,v)] else [] | `Del k -> [`Del k] in let filter_e = function | Set m -> Set (M.filter pred m) | Patch p -> Patch (List.concat (List.map convert_p p)) in let e = React.E.map filter_e (event m) in from_event (M.filter pred (value m)) e end reactiveData-0.2.2/src/reactiveData.mli000066400000000000000000000220201341066613300177740ustar00rootroot00000000000000(* ReactiveData * https://github.com/ocsigen/reactiveData * Copyright (C) 2014 Hugo Heuzard * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, with linking exception; * either version 2.1 of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) (** ReactiveData is a module for data-aware functional reactive programming (FRP). It adds support to incremental changes in data structures by reasoning on patches instead of absolute values. ReactiveData is based on and inter-operates with React. You are most likely interested in the sub-module [RList], which implements a superset of the signature [S]. *) (** Signature describing a reactive data structure (['a t]). Most functions in [S] are not safe to call during a React update step. *) module type S = sig (** Reactive version of the data container *) type 'a t (** Raw (non-reactive) version of the data container *) type 'a data (** Patch format *) type 'a patch (** Message format *) type 'a msg = | Patch of 'a patch (** [Patch p] triggers the application of [p] on the current contents *) | Set of 'a data (** With [Set d], [d] becomes the new content *) (** Handle that permits applying incremental updates *) type 'a handle (** Empty data structure *) val empty : 'a t (** Build a container from initial contents. The handle can be used for performing reactive updates. *) val create : 'a data -> 'a t * 'a handle (** [from_event d e] is a container whose initial value is [d], and which gets updated for every occurrence of [e] *) val from_event : 'a data -> 'a msg React.E.t -> 'a t (** Convert a React signal into a ReactiveData container. Whenever the signal changes from value [v] to value [v'], we detect the differences between [v] and [v'], and perform downstream computation (e.g., for [map]) only on the new and modified elements. *) val from_signal : ?eq:('a -> 'a -> bool) -> 'a data React.S.t -> 'a t (** Produce a constant container *) val const : 'a data -> 'a t (** [patch h p] applies [p] on the container corresponding to [h] *) val patch : 'a handle -> 'a patch -> unit (** [set h d] sets the contents of the container corresponding to [h], disregarding previous contents *) val set : 'a handle -> 'a data -> unit (** Transform a message *) val map_msg : ('a -> 'b) -> 'a msg -> 'b msg (** [map f c] applies [f] on all elements of [c], producing a new reactive container [c']. Modifying the contents of [c] leads to modifications of [c']. [f] is applied only on the new or modified elements of [c]. *) val map : ('a -> 'b) -> 'a t -> 'b t (** Return current contents *) val value : 'a t -> 'a data (** [fold f c v] accumulates the updates on [c] with [f] starting from [v]. The result is a signal of value [f m_n (f ... (f m_1 v))], where [m_1] ... [m_n] are the messages that have been applied since the beginning of [fold]. [m_1] is a pseudo-message [Set l], accounting for the contents [l] of [c] at the time when accumulation starts. *) val fold : ('a -> 'b msg -> 'a) -> 'b t -> 'a -> 'a React.signal (** Signal corresponding to contents *) val signal : ?eq:('a -> 'a -> bool) -> 'a t -> 'a data React.S.t (** Event whose occurrences correspond to container updates *) val event : 'a t -> 'a msg React.E.t end (** Reactive list data structure *) module RList : sig (** Patch operation on lists. All operations are of linear complexity. *) type 'a p = | I of int * 'a (** [I (i, v)] adds [v] at position [i] *) | R of int (** [R i] removes [i]-th element *) | U of int * 'a (** [U (i, v)] substitutes [i]-th element with [v] *) | X of int * int (** [X (i, j)] swaps the [i]-th and [j]-th elements *) (** A patch is a list of patch operations. The operations are applied in the order they appear in the list. The indices correspond to list contents after the operations that appear earlier in the list have been applied, not to the contents before the whole patch operation. A patch comprised of [I], [R], and [U] steps with increasing indices can be applied in time O(m + n), where m is the patch length and n is the current size of the list. (Arbitrary patches are slower, requiring O(m * n).) *) type 'a patch = 'a p list include S with type 'a data = 'a list and type 'a patch := 'a patch (** Add element to the beginning *) val cons : 'a -> 'a handle -> unit (** Add element to the end *) val snoc : 'a -> 'a handle -> unit (** [insert v i h] adds [v] as the [i]-th position in the container corresponding to [h]. The indices of the subsequent elements change. *) val insert : 'a -> int -> 'a handle -> unit (** [remove i h] removes the [i]-th position from the container corresponding to [h]. The indices of the subsequent elements change. *) val remove : int -> 'a handle -> unit (** [remove_last a] removes the last element of [a] *) val remove_last : ('a t * 'a handle) -> unit (** [remove_eq l x] removes the first occurence of [x] from [l] *) val remove_eq : ?eq:('a -> 'a -> bool) -> ('a t * 'a handle) -> 'a -> unit (** [update v i h] substitutes the [i]-th element of the container corresponding to [h] with [v] *) val update : 'a -> int -> 'a handle -> unit (** [update_eq l a b] substitutes the first occurence of [a] (according to [eq]) in [l] with [b] *) val update_eq : ?eq:('a -> 'a -> bool) -> ('a t * 'a handle) -> 'a -> 'a -> unit (** [move i offset h] moves the [i]-th element of the container corresponding by [offset] positions in [h], modifying the indices of other elements *) val move : int -> int -> 'a handle -> unit (** Produce container list containing a single, constant element *) val singleton : 'a -> 'a t (** Produce reactive list containing a single element that gets updated based on a signal *) val singleton_s : 'a React.S.t -> 'a t (** [concat a b] is the concatenation of [a] and [b], and it gets updated whenever [a] and [b] change *) val concat : 'a t -> 'a t -> 'a t (** [rev a] is the reversal of [a]; [rev a] gets updated along with [a] *) val rev : 'a t -> 'a t (** [filter pred l] keeps the elements of [l] matching [pred]; gets updated when [l] is. [pred] should be a pure function *) val filter : ('a -> bool) -> 'a t -> 'a t (** [for_all fn l] is a [bool React.S.t] verifying that all elements [x] of [l] satisfy [fn x] *) val for_all : ('a -> bool) -> 'a t -> bool React.S.t end (** Reactive map data structure *) module RMap(M : Map.S) : sig type 'a patch = [ `Add of M.key * 'a | `Del of M.key ] list include S with type 'a data = 'a M.t and type 'a patch := 'a patch (** [filter pred l] keeps the elements of [l] matching [pred]; gets updated when [l] is. [pred] should be a pure function *) val filter : (M.key -> 'a -> bool) -> 'a t -> 'a t end (** Signature describing a raw data container (['a data]). Given an implementation of [DATA], an incremental version of the container can be produced (via [Make]). *) module type DATA = sig (** Data container *) type 'a data (** Patch format for modifying the container *) type 'a patch (** Applicative merge operation: [merge p d] is a new container produced by applying [p] on [d]. [d] does not change. *) val merge : 'a patch -> 'a data -> 'a data (** Transform a patch *) val map_patch : ('a -> 'b) -> 'a patch -> 'b patch (** [map f d] applies [f] on all the elements of [d], producing a new container in an applicative way *) val map_data : ('a -> 'b) -> 'a data -> 'b data (** Empty container *) val empty : 'a data (** Lift an equality operator over atoms of type ['a] to an equality operator over ['a data] *) val equal : ('a -> 'a -> bool) -> 'a data -> 'a data -> bool (** [diff ?eq d1 d2] produces a patch describing the differences between [d1] and [d2]. The optional [?eq] argument is used for comparing the atoms inside [d1] and [d2]. (The default value for [eq] is [(=)].) *) val diff : eq:('a -> 'a -> bool) -> 'a data -> 'a data -> 'a patch end (** Functor for turning a plain container into an incremental one *) module Make(D : DATA) : S with type 'a data = 'a D.data and type 'a patch = 'a D.patch