Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6733363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:45:35+00:00 2026-05-26T10:45:35+00:00

I am implementing A* for http://aichallenge.org/specification.php and was wondering a slick way to select

  • 0

I am implementing A* for http://aichallenge.org/specification.php and was wondering a slick way to select the minimum of a collection based of the schartzian transform.

Basically, I have a collection of suitable squares to move to, and I want to move to the square with the lowest cost.

Basically I will be selecting the square from my neighbors who has the lowest cost in a loop.

The only way I can think to do this is with something like

next_spot = spot.neighbors.sort_by |a,b| { a.cost(dest) <=> b.cost(dest) }.first

But I would really like something higher performance, because I don’t really want to sort the collection, I just want the one with the minimum transform value

Note, I could write something more verbose and “C-style” looping and keeping track of a previous minimum, but I was hoping for something clear and compact.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T10:45:35+00:00Added an answer on May 26, 2026 at 10:45 am

    Why not use min_by?

    next_spot = spot.neighbors.min_by { |x| x.cost(dest) }
    

    And if a “_by” version of an Enumerable-ish method doesn’t exist you can get old school and do the Schwartzian Transform by hand with this sort of pseudo-Ruby pattern:

    a.map { |x| [ expensive(x), x ] }.       # Do the expensive part once and cache it
      op  { |x| something_with x.first... }. # Do what you really came to do
      map { |x| x.last }                     # Unwrap the caching
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having trouble understanding the instructions of implementing Fannkuch. Instructions: http://www.haskell.org/haskellwiki/Shootout/Fannkuch After step Count
BOSH (Bidirectional-streams Over Synchronous HTTP) is a sneaky way of implementing 2-way client-server communication
I am having problem with implementing custom session handler in php. The code: http://pastebin.com/9QV9f22Q
I am implementing an HTTP Module in ASP.NET to identify geographical information based on
I am implementing a Windows-based web server handling multiple specific HTTP requests from clients
I'm following this tutorial here: http://brenelz.com/blog/implementing-paging-using-php-and-jquery/ Here is what I have: http://eataustineat.com/testingfolder/ I've encountered
I'm implementing algorithms in http://portal.acm.org/citation.cfm?id=1813708 that utilize suffix arrays to find longest common substrings.
When implementing SuperBoxSelect (http://www.sencha.com/forum/showthread.php?69307-3.x-Ext.ux.form.SuperBoxSelect), I've realized that it currently does not support shift +
Hey all i am implementing the gameloop found here: http://obviam.net/index.php/the-android-game-loop/ My question is why
I'm implementing an API using authentication based on this article: http://www.thebuzzmedia.com/designing-a-secure-rest-api-without-oauth-authentication/ And this related

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.