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 6916903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:39:46+00:00 2026-05-27T09:39:46+00:00

I came across this bit of code: n = args[0] as Long [*n..1, n].any{

  • 0

I came across this bit of code:

n = args[0] as Long
[*n..1, n].any{ println ' '*it + '*'*(n - ~n - it*2) }

It’s used for printing a tree form of structure. Like this:

    *
   ***
  *****
 *******
    *

(for n=4)

  1. How does the code [*n..1,n] produce [4, 3, 2, 1, 4]?

  2. How does any method works here? The Doc doesn’t help me much. What is a predictive that can be passed to any(as mentioned in Doc‘s)?

Whats the use of any and how its handled in this case?

  • 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-27T09:39:47+00:00Added an answer on May 27, 2026 at 9:39 am

    Q1a: * “unpacks” an array. .. creates a range. [] creates a collection.
    Q1b: *n..1 unpacks [4,3,2,1] into its individual parts.
    Q1c: [4,3,2,1,n] == [4,3,2,1,4]

    Q2: I don’t know why any was used here; each works just as well, and makes more sense in context. any does loop over the connection, so the println side-effect functions as intended.

    Normally any would be used to determine if any collection elements met a criteria, for example:

    [*n..1,n].any { it > 10 } // Returns false, no elements are > 10
    [*n..1,n].any { it == 3 } // Returns true, because at least one element is 3
    

    The last statement of the closure is used to determine if each item meets the criteria. println returns null, so any will return false. The value is unused and discarded.

    The only reason I can think of that someone might have used any is to avoid seeing the return value of each in the console. each returns the original collection.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I came across this bit of vba code posted in another SO question. Is
I thought I understood variable scope until I came across this bit of code:
Hi I came across this bit of C++ code and am trying to learn
I recently came across this genius bit of code on HTTP POST from PHP,
So I came across this bit of php code: <?php $long_url = http://example.com; $bit_ly
I came across this bit of code in an example from the Boost documentation:
I came across this strange bit of CSS tonight... display: inline !ie; Now I've
I was doing a bit research and I came across this comment. I have
Came across this code: <?php require_once 'HTTP/Session/Container/DB.php'; $s = new HTTP_Session_Container_DB('mysql://user:password@localhost/db'); ini_get('session.auto_start') or session_start();
I came across this Linq to Sql code in an application I am maintaining:

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.