Releases: rolandpeelen/melange-pancake
Melange
Update to OCaml
v1.4.0 Version 1.4.0
Add Lens.effect
One often has the need to do side-effecty stuff, doing something like
map >> ignore
. The effect function is here to make that pattern
easier to deal with
Add Windows Support
v1.2.7 Add Windows Support
Update PPXLib version
This release updates the PPXLib version so it can be compiled with latest ocaml
Add License (BSD 3-clause)
v1.2.2 Release version v1.2.2
Fix Stackoverflow
As it turns out this got compiled to a while loop in Bucklescript,
let rec findBy = fn =>
fun
| [] => None
| [x, ..._] when fn(x) => Some(x)
| [_, ...xs] => findBy(fn, xs);
But this didn't.
let rec replaceBy = (e, fn) =>
fun
| [] => []
| [x, ...xs] when fn(x) => [e, ...xs]
| [x, ...xs] => [x, ...replaceBy(e, fn, xs)];
In the Array version we simply map as slicing the array is O(n)
anyway. For the linked list, I figured we could be more clever and simple join the rest of the list back onto itself when we've found the element. There were some attempts at cleverness, using an accumulator, but those ended up just being slower, as the accumulator would have to append to keep the list ordered, which is slow. Prepending and then reversing the list is another O(n)
operation, meaning in the worst case scenario we would run 0(n*2)
.
This is now just a map.
FindBy / FindByLens for Array / List
This release adds a lens for finding elements in an Array
or List
.
Either directly, if you can provide the full item to find
Lens.set(
Lens.List.find(5) >>- Lens.Option.orExn,
200,
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
)
|> expect
|> toEqual([0, 1, 2, 3, 4, 200, 6, 7, 8, 9, 10])
Or by providing an additional lens to find something by a record.
[@pancake]
type user = {
id: int,
username: string,
};
let users = [
{id: 0, username: "0"},
{id: 1, username: "1"},
{id: 2, username: "2"},
{id: 3, username: "3"},
];
Lens.view(Lens.List.findByLens(0, userIdLens), users)
|> expect
|> toEqual(Some({id: 0, username: "0"}))
Checkout the tests for lists, arrays, or this arbitrarily complicated nested thing to see some more examples (and how they compose into Lens.Option
to add fallback values, or throw exceptions when they're not found
v1.1.0 -- List / Array / Option / Result / Tests
We now support some extra helpers;
List & Array
Lens.List.atOrElse
Lens.Array.atOrElse
Takes an integer, and a default. When viewing, it will try to view the element. If it's out of bounds, it will return the default. When setting, it will set the element if it's in bounds, and return the original array if it's out of bounds. You can supply negative integers to wrap around (ie. -1
is the last element)
Lens.List.atOrExn
Lens.Array.atOrExn
Same asatOrElse
, but will throw when out of bounds
Option & Result
-
Lens.Option.orElse
-
Lens.Result.orElse
These will take a default value, when it is eitherNone
orError(...)
, they will return the default when viewing. Setting will always be wrapped in eitherSome(...)
orOk(...)
. -
Lens.Option.orExn
-
Lens.Result.orExn
Same asorElse
, but will throw when out of bounds
We've also made sure these are covered by tests properly...
v1.0.5
This release uses the pre-build variant, as build by CI