Giter VIP home page Giter VIP logo

Comments (6)

giacomocavalieri avatar giacomocavalieri commented on August 11, 2024 1

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.

giacomocavalieri avatar giacomocavalieri commented on August 11, 2024

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.

lpil avatar lpil commented on August 11, 2024

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.

chuckwondo avatar chuckwondo commented on August 11, 2024

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.

lpil avatar lpil commented on August 11, 2024

What's the advantage here of making it a higher order function over working with 3 orders?

from stdlib.

lpil avatar lpil commented on August 11, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.