Skip to content

refactor: WithDefault -> Or #87

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

Merged
merged 1 commit into from
Jul 25, 2023
Merged
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
10 changes: 8 additions & 2 deletions src/Core__Null.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function compare(a, b, cmp) {
return Core__Option.compare(a === null ? undefined : Caml_option.some(a), b === null ? undefined : Caml_option.some(b), cmp);
}

function getWithDefault(value, $$default) {
function getOr(value, $$default) {
if (value !== null) {
return value;
} else {
Expand All @@ -47,7 +47,7 @@ function map(value, f) {
}
}

function mapWithDefault(value, $$default, f) {
function mapOr(value, $$default, f) {
if (value !== null) {
return Curry._1(f, value);
} else {
Expand All @@ -63,13 +63,19 @@ function flatMap(value, f) {
}
}

var getWithDefault = getOr;

var mapWithDefault = mapOr;

export {
equal ,
compare ,
fromOption ,
getOr ,
getWithDefault ,
getExn ,
map ,
mapOr ,
mapWithDefault ,
flatMap ,
}
Expand Down
8 changes: 6 additions & 2 deletions src/Core__Null.res
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ let equal = (a, b, eq) => Core__Option.equal(a->toOption, b->toOption, eq)

let compare = (a, b, cmp) => Core__Option.compare(a->toOption, b->toOption, cmp)

let getWithDefault = (value, default) =>
let getOr = (value, default) =>
switch value->toOption {
| Some(x) => x
| None => default
}

let getWithDefault = getOr

let getExn: t<'a> => 'a = value =>
switch value->toOption {
| Some(x) => x
Expand All @@ -38,12 +40,14 @@ let map = (value, f) =>
| None => null
}

let mapWithDefault = (value, default, f) =>
let mapOr = (value, default, f) =>
switch value->toOption {
| Some(x) => f(x)
| None => default
}

let mapWithDefault = mapOr

let flatMap = (value, f) =>
switch value->toOption {
| Some(x) => f(x)
Expand Down
20 changes: 13 additions & 7 deletions src/Core__Null.resi
Original file line number Diff line number Diff line change
Expand Up @@ -77,22 +77,25 @@ Console.log(asNull == null) // Logs `true` to the console.
let fromOption: option<'a> => t<'a>

/**
`getWithDefault(value, default)` returns `value` if not `null`, otherwise return
`getOr(value, default)` returns `value` if not `null`, otherwise return
`default`.

## Examples

```rescript
Null.getWithDefault(null, "Banana") // Banana
Null.getWithDefault(Nulalble.make("Apple"), "Banana") // Apple
Null.getOr(null, "Banana") // Banana
Null.getOr(Nulalble.make("Apple"), "Banana") // Apple

let greet = (firstName: option<string>) =>
"Greetings " ++ firstName->Null.getWithDefault("Anonymous")
"Greetings " ++ firstName->Null.getOr("Anonymous")

Null.make("Jane")->greet // "Greetings Jane"
null->greet // "Greetings Anonymous"
```
*/
let getOr: (t<'a>, 'a) => 'a

@deprecated("Use getOr instead")
let getWithDefault: (t<'a>, 'a) => 'a

/**
Expand Down Expand Up @@ -139,19 +142,22 @@ Null.map(null, x => x * x) // null
let map: (t<'a>, 'a => 'b) => t<'b>

/**
`mapWithDefault(value, default, f)` returns `f(value)` if `value` is not `null`,
`mapOr(value, default, f)` returns `f(value)` if `value` is not `null`,
otherwise returns `default`.

## Examples

```rescript
let someValue = Null.make(3)
someValue->Null.mapWithDefault(0, x => x + 5) // 8
someValue->Null.mapOr(0, x => x + 5) // 8

let noneValue = null
noneValue->Null.mapWithDefault(0, x => x + 5) // 0
noneValue->Null.mapOr(0, x => x + 5) // 0
```
*/
let mapOr: (t<'a>, 'b, 'a => 'b) => 'b

@deprecated("Use mapOr instead")
let mapWithDefault: (t<'a>, 'b, 'a => 'b) => 'b

/**
Expand Down
10 changes: 8 additions & 2 deletions src/Core__Nullable.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function compare(a, b, cmp) {
return Core__Option.compare((a == null) ? undefined : Caml_option.some(a), (b == null) ? undefined : Caml_option.some(b), cmp);
}

function getWithDefault(value, $$default) {
function getOr(value, $$default) {
if (value == null) {
return $$default;
} else {
Expand All @@ -46,7 +46,7 @@ function map(value, f) {
}
}

function mapWithDefault(value, $$default, f) {
function mapOr(value, $$default, f) {
if (value == null) {
return $$default;
} else {
Expand All @@ -62,13 +62,19 @@ function flatMap(value, f) {
}
}

var getWithDefault = getOr;

var mapWithDefault = mapOr;

export {
equal ,
compare ,
fromOption ,
getOr ,
getWithDefault ,
getExn ,
map ,
mapOr ,
mapWithDefault ,
flatMap ,
}
Expand Down
8 changes: 6 additions & 2 deletions src/Core__Nullable.res
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ let equal = (a, b, eq) => Core__Option.equal(a->toOption, b->toOption, eq)

let compare = (a, b, cmp) => Core__Option.compare(a->toOption, b->toOption, cmp)

let getWithDefault = (value, default) =>
let getOr = (value, default) =>
switch value->toOption {
| Some(x) => x
| None => default
}

let getWithDefault = getOr

let getExn: t<'a> => 'a = value =>
switch value->toOption {
| Some(x) => x
Expand All @@ -38,12 +40,14 @@ let map = (value, f) =>
| None => Obj.magic(value)
}

let mapWithDefault = (value, default, f) =>
let mapOr = (value, default, f) =>
switch value->toOption {
| Some(x) => f(x)
| None => default
}

let mapWithDefault = mapOr

let flatMap = (value, f) =>
switch value->toOption {
| Some(x) => f(x)
Expand Down
20 changes: 13 additions & 7 deletions src/Core__Nullable.resi
Original file line number Diff line number Diff line change
Expand Up @@ -87,22 +87,25 @@ let asNullable = optString->Nullable.fromOption // Nullable.t<string>
let fromOption: option<'a> => t<'a>

/**
`getWithDefault(value, default)` returns `value` if not `null` or `undefined`,
`getOr(value, default)` returns `value` if not `null` or `undefined`,
otherwise return `default`.

## Examples

```rescript
Nullable.getWithDefault(Nullable.null, "Banana") // Banana
Nullable.getWithDefault(Nulalble.make("Apple"), "Banana") // Apple
Nullable.getOr(Nullable.null, "Banana") // Banana
Nullable.getOr(Nulalble.make("Apple"), "Banana") // Apple

let greet = (firstName: option<string>) =>
"Greetings " ++ firstName->Nullable.getWithDefault("Anonymous")
"Greetings " ++ firstName->Nullable.getOr("Anonymous")

Nullable.make("Jane")->greet // "Greetings Jane"
Nullable.null->greet // "Greetings Anonymous"
```
*/
let getOr: (t<'a>, 'a) => 'a

@deprecated("Use getOr instead")
let getWithDefault: (t<'a>, 'a) => 'a

/**
Expand Down Expand Up @@ -149,19 +152,22 @@ Nullable.map(undefined, x => x * x) // undefined
let map: (t<'a>, 'a => 'b) => t<'b>

/**
`mapWithDefault(value, default, f)` returns `f(value)` if `value` is not `null`
`mapOr(value, default, f)` returns `f(value)` if `value` is not `null`
or `undefined`, otherwise returns `default`.

## Examples

```rescript
let someValue = Nullable.make(3)
someValue->Nullable.mapWithDefault(0, x => x + 5) // 8
someValue->Nullable.mapOr(0, x => x + 5) // 8

let noneValue = Nullable.null
noneValue->Nullable.mapWithDefault(0, x => x + 5) // 0
noneValue->Nullable.mapOr(0, x => x + 5) // 0
```
*/
let mapOr: (t<'a>, 'b, 'a => 'b) => 'b

@deprecated("Use mapOr instead")
let mapWithDefault: (t<'a>, 'b, 'a => 'b) => 'b

/**
Expand Down
10 changes: 8 additions & 2 deletions src/Core__Option.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function getExn(x) {
};
}

function mapWithDefault(opt, $$default, f) {
function mapOr(opt, $$default, f) {
var f$1 = Curry.__1(f);
if (opt !== undefined) {
return f$1(Caml_option.valFromOption(opt));
Expand All @@ -54,7 +54,7 @@ function flatMap(opt, f) {

}

function getWithDefault(opt, $$default) {
function getOr(opt, $$default) {
if (opt !== undefined) {
return Caml_option.valFromOption(opt);
} else {
Expand Down Expand Up @@ -104,13 +104,19 @@ function compare(a, b, cmp) {
}
}

var mapWithDefault = mapOr;

var getWithDefault = getOr;

export {
filter ,
forEach ,
getExn ,
mapOr ,
mapWithDefault ,
map ,
flatMap ,
getOr ,
getWithDefault ,
orElse ,
isSome ,
Expand Down
10 changes: 7 additions & 3 deletions src/Core__Option.res
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,15 @@ let getExn = x =>

external getUnsafe: option<'a> => 'a = "%identity"

let mapWithDefaultU = (opt, default, f) =>
let mapOrU = (opt, default, f) =>
switch opt {
| Some(x) => f(. x)
| None => default
}

let mapWithDefault = (opt, default, f) => mapWithDefaultU(opt, default, (. x) => f(x))
let mapOr = (opt, default, f) => mapOrU(opt, default, (. x) => f(x))

let mapWithDefault = mapOr

let mapU = (opt, f) =>
switch opt {
Expand All @@ -70,12 +72,14 @@ let flatMapU = (opt, f) =>

let flatMap = (opt, f) => flatMapU(opt, (. x) => f(x))

let getWithDefault = (opt, default) =>
let getOr = (opt, default) =>
switch opt {
| Some(x) => x
| None => default
}

let getWithDefault = getOr

let orElse = (opt, other) =>
switch opt {
| Some(_) as some => some
Expand Down
20 changes: 13 additions & 7 deletions src/Core__Option.resi
Original file line number Diff line number Diff line change
Expand Up @@ -96,18 +96,21 @@ Option.getUnsafe(None: option<int>) // Returns `undefined`, which is not a valid
external getUnsafe: option<'a> => 'a = "%identity"

/**
`mapWithDefault(opt, default, f)` returns `f(value)` if `opt` is `Some(value)`, otherwise `default`.
`mapOr(opt, default, f)` returns `f(value)` if `opt` is `Some(value)`, otherwise `default`.

## Examples

```rescript
let someValue = Some(3)
someValue->Option.mapWithDefault(0, x => x + 5) // 8
someValue->Option.mapOr(0, x => x + 5) // 8

let noneValue = None
noneValue->Option.mapWithDefault(0, x => x + 5) // 0
noneValue->Option.mapOr(0, x => x + 5) // 0
```
*/
let mapOr: (option<'a>, 'b, 'a => 'b) => 'b

@deprecated("Use mapOr instead")
let mapWithDefault: (option<'a>, 'b, 'a => 'b) => 'b

/**
Expand Down Expand Up @@ -143,21 +146,24 @@ Option.flatMap(None, addIfAboveOne) // None
let flatMap: (option<'a>, 'a => option<'b>) => option<'b>

/**
`getWithDefault(opt, default)` returns `value` if `opt` is `Some(value)`, otherwise `default`.
`getOr(opt, default)` returns `value` if `opt` is `Some(value)`, otherwise `default`.

## Examples

```rescript
Option.getWithDefault(None, "Banana") // Banana
Option.getWithDefault(Some("Apple"), "Banana") // Apple
Option.getOr(None, "Banana") // Banana
Option.getOr(Some("Apple"), "Banana") // Apple

let greet = (firstName: option<string>) =>
"Greetings " ++ firstName->Option.getWithDefault("Anonymous")
"Greetings " ++ firstName->Option.getOr("Anonymous")

Some("Jane")->greet // "Greetings Jane"
None->greet // "Greetings Anonymous"
```
*/
let getOr: (option<'a>, 'a) => 'a

@deprecated("Use getOr instead")
let getWithDefault: (option<'a>, 'a) => 'a

/**
Expand Down
Loading