Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update jbuilder to dune and drop the Makefile for now #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
.merlin
Session.vim
_build
pkg/META
node_modules
11 changes: 3 additions & 8 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
1.2.2 (20-Jan-2019):
* Solved issue with the pseudo imperative -= infix operator
Thanks to @takanuva for reporting this.
Issue here: https://github.com/pdonadeo/ocaml-lens/issues/11
## [Unreleased]

1.2.1 (30-Jun-2018):
* Build system changed from oasis/ocamlbuild to jbuilder.
### Added

1.0.0 (30-Mar-2015):
* Initial public release.
- Port https://github.com/pdonadeo/ocaml-lens to Bucklescript, keeps the ppx
16 changes: 0 additions & 16 deletions Makefile

This file was deleted.

52 changes: 31 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,37 @@
Functional Lenses
=================
# WARNING

This repository is an attempt to adapt `lens-ocaml` to BuckleScript (including the ppx) and is still under development.

The main steps are:

- [x] Update jbuilder to dune
- [ ] Use Esy
- [ ] Use ocaml-format and "clean" the code
- [ ] Make `lens` targets JavaScript with BuckleScript
- [ ] Release on NPM 🎊

# Functional Lenses

This package provides some basic types and functions for using lenses in OCaml.
Functional lenses are based on F# implementation in [FSharpX](https://github.com/fsharp/fsharpx). See [src/FSharpx.Extras/Lens.fs](https://github.com/fsharp/fsharpx/blob/master/src/FSharpx.Extras/Lens.fs) for the original implementation. Written by Alessandro Strada.
Functional lenses are based on F# implementation in [FSharpX](https://github.com/fsharp/fsharpx). See [src/FSharpx.Extras/Lens.fs](https://github.com/fsharp/fsharpx/blob/master/src/FSharpx.Extras/Lens.fs) for the original implementation. Written by Alessandro Strada.

See also:
* <http://bugsquash.blogspot.com/2011/11/lenses-in-f.html> Lenses in F#
* <http://stackoverflow.com/questions/8179485/updating-nested-immutable-data-structures> Stackoverflow question about Updating nested immutable data structures
* <http://stackoverflow.com/questions/5767129/lenses-fclabels-data-accessor-which-library-for-structure-access-and-mutatio> Haskell libraries for structure access and mutation
* <http://www.youtube.com/watch?v=efv0SQNde5Q> Functional lenses for Scala by Edward Kmett on YouTube
* <http://patternsinfp.wordpress.com/2011/01/31/lenses-are-the-coalgebras-for-the-costate-comonad/> Lenses are the coalgebras for the costate comonad by Jeremy Gibbons

Examples
========
- <http://bugsquash.blogspot.com/2011/11/lenses-in-f.html> Lenses in F#
- <http://stackoverflow.com/questions/8179485/updating-nested-immutable-data-structures> Stackoverflow question about Updating nested immutable data structures
- <http://stackoverflow.com/questions/5767129/lenses-fclabels-data-accessor-which-library-for-structure-access-and-mutatio> Haskell libraries for structure access and mutation
- <http://www.youtube.com/watch?v=efv0SQNde5Q> Functional lenses for Scala by Edward Kmett on YouTube
- <http://patternsinfp.wordpress.com/2011/01/31/lenses-are-the-coalgebras-for-the-costate-comonad/> Lenses are the coalgebras for the costate comonad by Jeremy Gibbons

# Examples

First load `Lens` in utop.

utop # #use "lens.ml";;

Given a couple of records

``` ocaml
```ocaml
type car = {
make : string;
model: string;
Expand All @@ -42,7 +53,7 @@ Given a couple of records

Create a new nested record

``` ocaml
```ocaml
let scifi_novel = {
name = "Metro 2033";
author = "Dmitry Glukhovsky";
Expand All @@ -60,7 +71,7 @@ Create a new nested record

Now to construct a few lenses to access some things

``` ocaml
```ocaml
let car_lens = {
get = (fun x -> x.car);
set = (fun v x -> { x with car = v })
Expand All @@ -80,27 +91,26 @@ Now to construct a few lenses to access some things

Using these lenses we can modify the mileage without having to unpack the record

``` ocaml
```ocaml
let a = compose mileage_lens (compose car_lens editor_lens) in
_set 10 scifi_novel a;;
```

Or using the `Infix` module we can do the same thing, only shorter.

``` ocaml
```ocaml
_set 10 scifi_novel (editor_lens |-- car_lens |-- mileage_lens);;

(* or *)

((editor_lens |-- car_lens |-- mileage_lens) ^= 10) @@ scifi_novel;;
```

Ppx syntax extension
--------------------
## Ppx syntax extension

Lenses can be generated using the 'lens.ppx_deriving' plugin for [ppx_deriving](https://github.com/whitequark/ppx_deriving)

``` ocaml
```ocaml
#require "lens.ppx_deriving";;

type car = {
Expand All @@ -116,7 +126,7 @@ val car_mileage: (car, int) Lens.t

The `prefix` option can be used to prefix each lens name with `lens`.

``` ocaml
```ocaml
#require "lens.ppx_deriving";;

type car = {
Expand All @@ -132,7 +142,7 @@ val lens_car_mileage: (car, int) Lens.t

The `submodule` option groups all the lenses in a sub-module `Lens`.

``` ocaml
```ocaml
#require "lens.ppx_deriving";;

type car = {
Expand All @@ -150,7 +160,7 @@ end

When the `prefix` and `submodule` options are combined, this is the module name which is prefixed.

``` ocaml
```ocaml
#require "lens.ppx_deriving";;

type car = {
Expand Down
1 change: 1 addition & 0 deletions dune-project
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(lang dune 1.5)
7 changes: 0 additions & 7 deletions jbuild-workspace.dev

This file was deleted.

12 changes: 6 additions & 6 deletions lens.opam
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ opam-version: "1.2"
name: "lens"
version: "1.2.2"
available: [ ocaml-version >= "4.04.1" ]
maintainer: "Paolo Donadeo <p.donadeo@gmail.com>"
authors: [ "Alessandro Strada <[email protected]>" ]
maintainer: "COMBRIAT Kevin<japan.combriat.kevin@gmail.com>"
authors: [ "Alessandro Strada <[email protected]>" "Paolo Donadeo <[email protected]>" ]
license: "BSD-3-clause"
homepage: "https://github.com/pdonadeo/ocaml-lens"
dev-repo: "https://github.com/pdonadeo/ocaml-lens.git"
bug-reports: "https://github.com/pdonadeo/ocaml-lens/issues"
build: [
["jbuilder" "subst" "-n" name] {pinned}
["jbuilder" "build" "-p" name "-j" jobs]
["dune" "subst" "-n" name] {pinned}
["dune" "build" "-p" name "-j" jobs]
]
build-test: [["jbuilder" "runtest" "-p" name "-j" jobs]]
build-test: [["dune" "runtest" "-p" name "-j" jobs]]
depends: [
"ppx_deriving" {build}
"ppx_tools" {build}
"ppxfind" {build}
"jbuilder" {build}
"dune" {build}
"ounit" {test}
]
3 changes: 3 additions & 0 deletions lens/src/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(library
(name lens)
(public_name lens))
3 changes: 1 addition & 2 deletions src/lens.ml → lens/src/lens.ml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ let ignore = {

let id = {
get = (fun a -> a);
set = (fun b a -> b)
set = (fun b _ -> b)
}

let first = {
Expand Down Expand Up @@ -185,4 +185,3 @@ struct
let (@=!) l v = modify_state l (fun a -> a @ v)

end

File renamed without changes.
8 changes: 8 additions & 0 deletions lens/tests/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
(executable
(name lens_tests)
(libraries lens oUnit))

(alias
(name runtest)
; (deps (lens_tests.exe))
(action (run ${<})))
3 changes: 1 addition & 2 deletions src_test/test_lens.ml → lens/tests/lens_tests.ml
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
open OUnit2

let suite = "Test lens" >::: [
Test_regression.suite;
Test_deriving_lens.suite;
Regression_tests.suite;
]

let _ =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ open OUnit2


(* https://github.com/pdonadeo/ocaml-lens/issues/11 *)
let issue_11 ctxt =
let issue_11 _ =
let open Lens.Infix in
let xs = [1; 2; 3; 4; 5] in
let l = Lens.for_list 4 in
Expand Down
7 changes: 7 additions & 0 deletions lens_ppx_deriving/src/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
(library
(name lens_ppx_deriving)
(public_name lens.ppx_deriving)
(libraries ppx_deriving.api)
(preprocess (action (run ppxfind -legacy ppx_tools.metaquot --as-pp %{input-file})))
(ppx_runtime_libraries lens)
(kind ppx_deriver))
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ let lens_name ~deriver_options record_type_decl field_name =
then Ppx_deriving.mangle_type_decl (`PrefixSuffix (deriver,field_name)) record_type_decl
else Ppx_deriving.mangle_type_decl (`Suffix field_name) record_type_decl

let module_name ~deriver_options { ptype_name = { txt = name } } =
let module_name ~deriver_options { ptype_name = { txt = name; _ }; _ } =
if deriver_options.prefix
then (String.capitalize_ascii name) ^ "Lens"
else "Lens"
Expand All @@ -88,11 +88,11 @@ let wrap_in_submodule_struct ~deriver_options record loc expressions =
{pstr_desc = define_module loc module_name expressions; pstr_loc = loc}
else {pstr_desc = Pstr_value (Nonrecursive, expressions); pstr_loc = loc}

let str_of_type ~options ~path ({ ptype_loc = loc } as type_decl) =
let str_of_type ~options ~path:_ ({ ptype_loc = loc; _ } as type_decl) =
let deriver_options = parse_options options in
match type_decl.ptype_kind with
| Ptype_record labels -> labels
|> List.map (fun { pld_name = { txt = name; loc } } ->
|> List.map (fun { pld_name = { txt = name; _ }; _ } ->
name, [%expr Lens.{
get = (fun r -> [%e Exp.field (evar "r") (mknoloc (Lident name))] );
set = (fun v r -> [%e updated_record "r" name "v"]);
Expand All @@ -107,11 +107,11 @@ let str_of_type ~options ~path ({ ptype_loc = loc } as type_decl) =
let type_named name =
Typ.mk (Ptyp_constr (mknoloc (Lident name), []))

let sig_of_type ~options ~path ({ ptype_loc = loc; ptype_name = { txt = record_name } } as type_decl) =
let sig_of_type ~options ~path:_ ({ ptype_loc = loc; ptype_name = { txt = record_name; _ }; _ } as type_decl) =
let deriver_options = parse_options options in
match type_decl.ptype_kind with
| Ptype_record labels -> labels
|> List.map (fun { pld_name = { txt = name; loc }; pld_type } ->
|> List.map (fun { pld_name = { txt = name; _ }; pld_type; _ } ->
let lens_type = [%type: ([%t type_named record_name], [%t pld_type]) Lens.t] in
Sig.value (Val.mk (mknoloc (lens_name ~deriver_options type_decl name)) lens_type)
)
Expand Down
9 changes: 9 additions & 0 deletions lens_ppx_deriving/tests/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
(executable
(name lens_ppx_deriving_tests)
(libraries lens oUnit)
(preprocess (pps lens.ppx_deriving)))

(alias
(name runtest)
; (deps (lens_ppx_deriving_tests.exe))
(action (run ${<})))
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ end = struct
} [@@deriving lens]
end

let test_basic ctxt = M.(
let test_basic _ = M.(
let car = { make = "Citroën"; model = "2CV"; mileage = 1948 } in

assert_equal (get car_model car) "2CV";
Expand All @@ -40,7 +40,7 @@ end = struct
} [@@deriving lens]
end

let test_with_type_named_t ctxt = M_with_type_named_t.(
let test_with_type_named_t _ = M_with_type_named_t.(
let car = { make = "Citroën"; model = "2CV"; mileage = 1948 } in

assert_equal (get model car) "2CV";
Expand All @@ -63,7 +63,7 @@ end = struct
} [@@deriving lens { prefix = true }]
end

let test_prefix ctxt = M_with_prefix.(
let test_prefix _ = M_with_prefix.(
let car = { make = "Citroën"; model = "2CV"; mileage = 1948 } in

assert_equal (get lens_car_model car) "2CV";
Expand All @@ -86,7 +86,7 @@ end = struct
} [@@deriving lens { submodule = true }]
end

let test_module ctxt = M_with_module.(
let test_module _ = M_with_module.(
let car = { make = "Citroën"; model = "2CV"; mileage = 1948 } in

assert_equal (get Lens.model car) "2CV";
Expand All @@ -109,7 +109,7 @@ end = struct
} [@@deriving lens { submodule = true; prefix = true }]
end

let test_module_and_prefix ctxt = M_with_module_and_prefix.(
let test_module_and_prefix _ = M_with_module_and_prefix.(
let car = { make = "Citroën"; model = "2CV"; mileage = 1948 } in

assert_equal (get CarLens.model car) "2CV";
Expand All @@ -128,7 +128,7 @@ end = struct
end
end

let test_within_a_module ctxt = M_within_a_module.(
let test_within_a_module _ = M_within_a_module.(
let car = Car.({ make = "Citroën"; model = "2CV"; mileage = 1948 }) in

assert_equal (get Car.Lens.model car) "2CV";
Expand All @@ -145,3 +145,11 @@ let suite = "Test deriving(lens)" >::: [
"test_module_and_prefix" >:: test_module_and_prefix;
"test_within_a_module" >:: test_within_a_module;
]


let suite = "Test lens" >::: [
suite;
]

let _ =
run_test_tt_main suite
2 changes: 0 additions & 2 deletions pkg/pkg.ml

This file was deleted.

17 changes: 0 additions & 17 deletions src/jbuild

This file was deleted.

Loading