Comments (6)
I think it'd be easier to use one that works directly with an order:
pub fn break_tie(in order: Order, with comparison: Order) -> Order {
case order {
Lt | Gt -> order
Eq -> comparison
}
}
pub fn lazy_break_tie(in order: Order, with comparison: fn() -> Order) -> Order {
case order {
Lt | Gt -> order
Eq -> comparison()
}
}
That could then be used like this:
// Lazy version that plays nicely with `use`
use <- order.lazy_break_tie(in: string.compare(a.name, b.name))
use <- order.lazy_break_tie(in: int.compare(a.age, b.age))
int.compare(a.cuteness, b.cuteness)
// Non lazy version
string.compare(a.name, b.name)
|> order.break_tie(with: int.compare(a.age, b.age))
|> order.break_tie(with: int.compare(a.cuteness, b.cuteness))
I'm not really sure what a good name would be here, I'm just going with break_tie
out of habit :)
from stdlib.
Hello! I remember feeling the need for something similar in the past, I called it break_ties
, you can achieve something similar using bool.guard
:
fn compare_cat2(a: Cat, b: Cat) -> Order {
let name_order = string.compare(a.name, b.name)
use <- bool.guard(when: name_order != Eq, return: name_order)
// ^ if the name comparison is not `Eq`, we return it immediately
// otherwise we go on and compare the rest.
let age_order = int.compare(a.age, b.age)
use <- bool.guard(when: age_order != Eq, return: age_order)
// ^ if the age comparison is not `Eq`, we return it immediately
// otherwise we go on and compare the rest
int.compare(a.cuteness, b.cuteness)
// ^ if everything else is equal, the comparison on `cuteness`
// will decide the outcome of the comparison
}
from stdlib.
Sounds useful! A lazy variant would be useful too.
I don't feel very strongly about the name in either direction. What other options do we have? Just see if there's anything better we can go with before this
from stdlib.
How about a lazy implementation named then
to mirror Option.then
and Result.then
?
pub fn then(order: Order, compare: fn(a, a) -> Order, a1: a, to a2: a) -> Order {
case order {
Eq -> compare(a1, a2)
_ -> order
}
}
or, using bool.guard
:
pub fn then(order: Order, compare: fn(a, a) -> Order, a1: a, to a2: a) -> Order {
use <- bool.guard(when: order != Eq, return: order)
compare(a1, a2)
}
Then we might compare cats like so:
pub fn compare_cat(a: Cat, b: Cat) -> Order {
string.compare(a.name, b.name)
|> order.then(int.compare, a.age, to: b.age)
|> order.then(int.compare, a.cuteness, to: b.cuteness)
}
As an aside, I see that string.compare
does not name its 2nd parameter, but int.compare
names it with
. Here, I've named it to
(as I personally tend to use the phrase "compare to", not "compare with", but that's a personal preference). In any case all, of the compare
functions should be consistent with each other.
from stdlib.
What's the advantage here of making it a higher order function over working with 3 orders?
from stdlib.
break_tie
sounds nice and descriptive to me.
I wonder if there's another word that means this. Perhaps in the context of a competition or tournament. I can't think of anything right now
from stdlib.
Related Issues (20)
- Map equality in JavaScript target is faulty and not commutative HOT 1
- Extend `float.parse` to parse strings with no decimal point HOT 1
- Change `{thing}.filter` label for the predicate argument HOT 2
- Proposal: Use Javascript Iterators and Generators HOT 9
- `Maps are not ordered` seems to be inaccurate for the erlang target! HOT 1
- int.random behaviour HOT 3
- uri.parse does not follow RFC 3986 on JS HOT 6
- floating point modulo HOT 5
- File objects all hash to the same value HOT 1
- dynamic crashes with a TypeError when decoding null as field HOT 1
- `bit_array` module exposes a function that should be private HOT 1
- [erlang runtime] wrong output of `io.debug` and `string.inspect` for bit arrays HOT 2
- add .prettierrc and .editorconfig HOT 3
- System related functions HOT 1
- List/Dict inconsistency HOT 1
- Float to string with fixed precision HOT 3
- Is there a way to read value from standard io? HOT 1
- Adding a GCD function? HOT 1
- byte_size fails on bytes_builder with :size specifier HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from stdlib.